├── .gitignore ├── README.md ├── configuration ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ └── java │ │ ├── net │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ └── controller │ │ │ ├── ITHomeControllerTest.java │ │ │ └── ITStandaloneHomeControllerTest.java │ │ └── org │ │ └── springframework │ │ └── test │ │ └── web │ │ └── server │ │ └── samples │ │ └── context │ │ ├── GenericWebContextLoader.java │ │ └── WebContextLoader.java │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ └── ExampleApplicationContext.java │ │ │ └── controller │ │ │ └── HomeController.java │ ├── resources │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ ├── jsp │ │ │ └── index.jsp │ │ ├── layout │ │ │ └── layout.jsp │ │ └── sitemesh3.xml │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ └── jquery-1.8.2.js │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ └── controller │ └── HomeControllerTest.java ├── controllers-unittest ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ └── todo │ │ │ └── controller │ │ │ └── ITTodoControllerTest.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ └── controller │ │ │ │ └── ErrorController.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ ├── PersistenceContext.java │ │ │ └── WebAppContext.java │ │ │ └── todo │ │ │ ├── controller │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ └── TodoDTO.java │ │ │ ├── exception │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ ├── RepositoryTodoService.java │ │ │ └── TodoService.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext-web.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ ├── jsp │ │ │ ├── error │ │ │ │ ├── 404.jsp │ │ │ │ └── error.jsp │ │ │ └── todo │ │ │ │ ├── add.jsp │ │ │ │ ├── list.jsp │ │ │ │ ├── update.jsp │ │ │ │ └── view.jsp │ │ ├── layout │ │ │ └── layout.jsp │ │ └── sitemesh3.xml │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── todo.form.js │ │ ├── todo.js │ │ ├── todo.view.js │ │ └── vendor │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ └── jquery-1.8.2.js │ └── test │ ├── java │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ ├── common │ │ └── controller │ │ │ └── ErrorControllerTest.java │ │ ├── config │ │ └── TestContext.java │ │ └── todo │ │ ├── TestUtil.java │ │ ├── WebTestConstants.java │ │ ├── controller │ │ ├── StandaloneTodoControllerTest.java │ │ └── WebApplicationContextTodoControllerTest.java │ │ ├── dto │ │ └── TodoDTOBuilder.java │ │ ├── model │ │ ├── TodoBuilder.java │ │ └── TodoTest.java │ │ └── service │ │ └── RepositoryTodoServiceTest.java │ └── resources │ ├── log4j.properties │ └── testContext.xml ├── controllers ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ ├── net │ │ │ └── petrikainulainen │ │ │ │ └── spring │ │ │ │ └── testmvc │ │ │ │ └── todo │ │ │ │ └── controller │ │ │ │ └── ITTodoControllerTest.java │ │ └── org │ │ │ └── springframework │ │ │ └── test │ │ │ └── web │ │ │ └── server │ │ │ └── samples │ │ │ └── context │ │ │ ├── GenericWebContextLoader.java │ │ │ └── WebContextLoader.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ └── controller │ │ │ │ └── ErrorController.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ └── PersistenceContext.java │ │ │ └── todo │ │ │ ├── controller │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ └── TodoDTO.java │ │ │ ├── exception │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ ├── RepositoryTodoService.java │ │ │ └── TodoService.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ ├── jsp │ │ │ ├── error │ │ │ │ ├── 404.jsp │ │ │ │ └── error.jsp │ │ │ └── todo │ │ │ │ ├── add.jsp │ │ │ │ ├── list.jsp │ │ │ │ ├── update.jsp │ │ │ │ └── view.jsp │ │ ├── layout │ │ │ └── layout.jsp │ │ └── sitemesh3.xml │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── todo.form.js │ │ ├── todo.js │ │ ├── todo.view.js │ │ └── vendor │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ └── jquery-1.8.2.js │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ ├── common │ └── controller │ │ └── ErrorControllerTest.java │ └── todo │ ├── TodoTestUtil.java │ ├── config │ └── UnitTestContext.java │ ├── controller │ └── TodoControllerTest.java │ ├── model │ └── TodoTest.java │ └── service │ └── RepositoryTodoServiceTest.java ├── junit-5 ├── integration-tests │ └── repositories-jooq │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ ├── integration-test │ │ ├── java │ │ │ └── net │ │ │ │ └── petrikainulainen │ │ │ │ └── springmvctest │ │ │ │ └── junit5 │ │ │ │ ├── IdColumnReset.java │ │ │ │ ├── IntegrationTest.java │ │ │ │ └── todoitem │ │ │ │ ├── CreateTodoItemTest.java │ │ │ │ ├── FindAllTest.java │ │ │ │ ├── FindByIdTest.java │ │ │ │ ├── TodoItemTableRow.java │ │ │ │ ├── TodoItems.java │ │ │ │ └── UpdateTodoItemTest.java │ │ └── resources │ │ │ └── db │ │ │ ├── clear-database.sql │ │ │ └── init-todo-items.sql │ │ ├── main │ │ ├── java │ │ │ └── net │ │ │ │ └── petrikainulainen │ │ │ │ └── springmvctest │ │ │ │ └── junit5 │ │ │ │ ├── SpringMVCTestExampleApplication.java │ │ │ │ └── todoitem │ │ │ │ ├── CreateTodoItem.java │ │ │ │ ├── TodoItemDTO.java │ │ │ │ ├── TodoItemRepository.java │ │ │ │ ├── TodoItemStatus.java │ │ │ │ ├── TodoListItemDTO.java │ │ │ │ └── UpdateTodoItem.java │ │ └── resources │ │ │ ├── application-integrationTest.yml │ │ │ ├── application.yml │ │ │ └── db │ │ │ └── migration │ │ │ ├── V001__create_todo_item_table.sql │ │ │ ├── V002__insert_valid_todo_item_statuses.sql │ │ │ └── V003__create_tag_table.sql │ │ └── test │ │ └── java │ │ └── net │ │ └── petrikainulainen │ │ └── springmvctest │ │ └── junit5 │ │ ├── UnitTest.java │ │ └── UnitTestDemoTest.java └── unit-tests │ ├── mock-mvc-tester │ └── rest-api │ │ ├── LICENSE │ │ ├── README.md │ │ ├── pom.xml │ │ └── src │ │ ├── main │ │ ├── java │ │ │ └── net │ │ │ │ └── petrikainulainen │ │ │ │ └── springmvctest │ │ │ │ └── junit5 │ │ │ │ ├── todo │ │ │ │ ├── CreateTodoItemDTO.java │ │ │ │ ├── TagDTO.java │ │ │ │ ├── TodoItemCrudService.java │ │ │ │ ├── TodoItemDTO.java │ │ │ │ ├── TodoItemNotFoundException.java │ │ │ │ ├── TodoItemStatus.java │ │ │ │ └── TodoListItemDTO.java │ │ │ │ └── web │ │ │ │ ├── FieldErrorDTO.java │ │ │ │ ├── TodoItemCrudController.java │ │ │ │ ├── TodoItemErrorHandler.java │ │ │ │ └── ValidationErrorDTO.java │ │ └── resources │ │ │ └── log4j2.xml │ │ └── test │ │ └── java │ │ └── net │ │ └── petrikainulainen │ │ └── springmvctest │ │ └── junit5 │ │ └── web │ │ ├── TodoItemCrudControllerTest.java │ │ ├── TodoItemHttpRequestBuilder.java │ │ ├── WebTestConfig.java │ │ └── WebTestUtil.java │ ├── normal-controllers │ ├── LICENSE │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── net │ │ │ │ └── petrikainulainen │ │ │ │ └── springmvctest │ │ │ │ └── junit5 │ │ │ │ ├── todo │ │ │ │ ├── CreateTodoItemFormDTO.java │ │ │ │ ├── TagDTO.java │ │ │ │ ├── TodoItemCrudService.java │ │ │ │ ├── TodoItemDTO.java │ │ │ │ ├── TodoItemNotFoundException.java │ │ │ │ ├── TodoItemStatus.java │ │ │ │ └── TodoListItemDTO.java │ │ │ │ └── web │ │ │ │ └── TodoItemCrudController.java │ │ └── resources │ │ │ └── log4j2.xml │ │ └── test │ │ └── java │ │ └── net │ │ └── petrikainulainen │ │ └── springmvctest │ │ └── junit5 │ │ └── web │ │ ├── TodoItemCrudControllerTest.java │ │ ├── TodoItemRequestBuilder.java │ │ ├── WebTestConfig.java │ │ └── WebTestUtil.java │ ├── rest-api-no-object-mapper │ ├── LICENSE │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── net │ │ │ │ └── petrikainulainen │ │ │ │ └── springmvctest │ │ │ │ └── junit5 │ │ │ │ ├── todo │ │ │ │ ├── CreateTodoItemDTO.java │ │ │ │ ├── TagDTO.java │ │ │ │ ├── TodoItemCrudService.java │ │ │ │ ├── TodoItemDTO.java │ │ │ │ ├── TodoItemNotFoundException.java │ │ │ │ ├── TodoItemStatus.java │ │ │ │ └── TodoListItemDTO.java │ │ │ │ └── web │ │ │ │ ├── FieldErrorDTO.java │ │ │ │ ├── TodoItemCrudController.java │ │ │ │ ├── TodoItemErrorHandler.java │ │ │ │ └── ValidationErrorDTO.java │ │ └── resources │ │ │ └── log4j2.xml │ │ └── test │ │ └── java │ │ └── net │ │ └── petrikainulainen │ │ └── springmvctest │ │ └── junit5 │ │ └── web │ │ ├── TodoItemCrudControllerTest.java │ │ ├── TodoItemRequestBuilder.java │ │ ├── WebTestConfig.java │ │ └── WebTestUtil.java │ └── rest-api │ ├── LICENSE │ ├── README.md │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── springmvctest │ │ │ └── junit5 │ │ │ ├── todo │ │ │ ├── CreateTodoItemDTO.java │ │ │ ├── TagDTO.java │ │ │ ├── TodoItemCrudService.java │ │ │ ├── TodoItemDTO.java │ │ │ ├── TodoItemNotFoundException.java │ │ │ ├── TodoItemStatus.java │ │ │ └── TodoListItemDTO.java │ │ │ └── web │ │ │ ├── FieldErrorDTO.java │ │ │ ├── TodoItemCrudController.java │ │ │ ├── TodoItemErrorHandler.java │ │ │ └── ValidationErrorDTO.java │ └── resources │ │ └── log4j2.xml │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── springmvctest │ └── junit5 │ └── web │ ├── TodoItemCrudControllerTest.java │ ├── TodoItemRequestBuilder.java │ ├── WebTestConfig.java │ └── WebTestUtil.java ├── rest-jsonpath ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ ├── net │ │ │ └── petrikainulainen │ │ │ │ └── spring │ │ │ │ └── testmvc │ │ │ │ ├── IntegrationTestUtil.java │ │ │ │ ├── common │ │ │ │ └── controller │ │ │ │ │ └── ITHomeControllerTest.java │ │ │ │ ├── todo │ │ │ │ └── controller │ │ │ │ │ └── ITTodoControllerTest.java │ │ │ │ └── user │ │ │ │ ├── ITAuthenticationTest.java │ │ │ │ └── controller │ │ │ │ └── ITUserControllerTest.java │ │ └── org │ │ │ └── springframework │ │ │ └── test │ │ │ └── web │ │ │ └── server │ │ │ └── samples │ │ │ └── context │ │ │ └── SecurityRequestPostProcessors.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ ├── controller │ │ │ │ └── HomeController.java │ │ │ └── util │ │ │ │ └── LocaleContextHolderWrapper.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ └── PersistenceContext.java │ │ │ ├── security │ │ │ ├── authentication │ │ │ │ ├── RestAuthenticationEntryPoint.java │ │ │ │ ├── RestAuthenticationFailureHandler.java │ │ │ │ ├── RestAuthenticationSuccessHandler.java │ │ │ │ └── RestLogoutSuccessHandler.java │ │ │ ├── authorization │ │ │ │ └── TodoPermissionEvaluator.java │ │ │ └── util │ │ │ │ └── SecurityContextUtil.java │ │ │ ├── todo │ │ │ ├── controller │ │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ │ ├── FieldValidationErrorDTO.java │ │ │ │ ├── FormValidationErrorDTO.java │ │ │ │ └── TodoDTO.java │ │ │ ├── exception │ │ │ │ ├── FormValidationError.java │ │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ │ ├── RepositoryTodoService.java │ │ │ │ └── TodoService.java │ │ │ └── user │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ └── dto │ │ │ ├── SecurityRole.java │ │ │ └── UserDTO.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext-security.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ └── jsp │ │ │ └── index.jsp │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── app │ │ ├── app.js │ │ ├── error │ │ │ ├── error.controller.js │ │ │ ├── error.events.js │ │ │ ├── error.routing.js │ │ │ └── error.views.js │ │ ├── todo │ │ │ ├── todo.controller.js │ │ │ ├── todo.events.js │ │ │ ├── todo.i18n.js │ │ │ ├── todo.models.js │ │ │ ├── todo.routing.js │ │ │ └── todo.views.js │ │ └── user │ │ │ ├── user.controller.js │ │ │ ├── user.events.js │ │ │ ├── user.models.js │ │ │ ├── user.routing.js │ │ │ └── user.views.js │ │ └── vendor │ │ ├── Backbone.ModelBinder.js │ │ ├── backbone.js │ │ ├── backbone.marionette.js │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ ├── i18next-1.5.8.js │ │ ├── jquery-1.8.2.js │ │ ├── spin.js │ │ └── underscore.js │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ ├── common │ ├── controller │ │ └── HomeControllerTest.java │ └── util │ │ └── LocaleContextHolderWrapperTest.java │ ├── security │ ├── authentication │ │ ├── RestAuthenticationEntryPointTest.java │ │ ├── RestAuthenticationFailureHandlerTest.java │ │ ├── RestAuthenticationSuccessHandlerTest.java │ │ └── RestLogoutSuccessHandlerTest.java │ ├── authorization │ │ └── TodoPermissionEvaluatorTest.java │ └── util │ │ └── SecurityContextUtilTest.java │ ├── todo │ ├── TodoTestUtil.java │ ├── config │ │ └── UnitTestContext.java │ ├── controller │ │ └── TodoControllerTest.java │ ├── dto │ │ └── FormValidationErrorDTOTest.java │ ├── model │ │ └── TodoTest.java │ └── service │ │ └── RepositoryTodoServiceTest.java │ └── user │ └── controller │ └── UserControllerTest.java ├── rest-unittest ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ └── controller │ │ │ │ └── ITHomeControllerTest.java │ │ │ └── todo │ │ │ └── controller │ │ │ └── ITTodoControllerTest.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ └── controller │ │ │ │ └── HomeController.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ ├── PersistenceContext.java │ │ │ └── WebAppContext.java │ │ │ └── todo │ │ │ ├── controller │ │ │ ├── RestErrorHandler.java │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ ├── FieldErrorDTO.java │ │ │ ├── TodoDTO.java │ │ │ └── ValidationErrorDTO.java │ │ │ ├── exception │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ ├── RepositoryTodoService.java │ │ │ └── TodoService.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext-web.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ └── jsp │ │ │ └── index.jsp │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── app │ │ ├── app.js │ │ ├── error │ │ │ ├── error.controller.js │ │ │ ├── error.events.js │ │ │ ├── error.routing.js │ │ │ └── error.views.js │ │ └── todo │ │ │ ├── todo.controller.js │ │ │ ├── todo.events.js │ │ │ ├── todo.i18n.js │ │ │ ├── todo.models.js │ │ │ ├── todo.routing.js │ │ │ └── todo.views.js │ │ └── vendor │ │ ├── Backbone.ModelBinder.js │ │ ├── backbone.js │ │ ├── backbone.marionette.js │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ ├── i18next-1.5.8.js │ │ ├── jquery-1.8.2.js │ │ ├── spin.js │ │ └── underscore.js │ └── test │ ├── java │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ ├── common │ │ └── controller │ │ │ └── HomeControllerTest.java │ │ ├── config │ │ └── TestContext.java │ │ └── todo │ │ ├── TestUtil.java │ │ ├── controller │ │ └── TodoControllerTest.java │ │ ├── dto │ │ ├── TodoDTOBuilder.java │ │ └── ValidationErrorDTOTest.java │ │ ├── model │ │ ├── TodoBuilder.java │ │ └── TodoTest.java │ │ └── service │ │ └── RepositoryTodoServiceTest.java │ └── resources │ ├── log4j.properties │ └── testContext.xml ├── rest ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ ├── net │ │ │ └── petrikainulainen │ │ │ │ └── spring │ │ │ │ └── testmvc │ │ │ │ ├── IntegrationTestUtil.java │ │ │ │ └── todo │ │ │ │ └── controller │ │ │ │ └── ITTodoControllerTest.java │ │ └── org │ │ │ └── springframework │ │ │ └── test │ │ │ └── web │ │ │ └── server │ │ │ └── samples │ │ │ └── context │ │ │ ├── GenericWebContextLoader.java │ │ │ └── WebContextLoader.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ ├── controller │ │ │ │ └── HomeController.java │ │ │ └── util │ │ │ │ └── LocaleContextHolderWrapper.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ └── PersistenceContext.java │ │ │ └── todo │ │ │ ├── controller │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ ├── FieldValidationErrorDTO.java │ │ │ ├── FormValidationErrorDTO.java │ │ │ └── TodoDTO.java │ │ │ ├── exception │ │ │ ├── FormValidationError.java │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ ├── RepositoryTodoService.java │ │ │ └── TodoService.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ └── jsp │ │ │ └── index.jsp │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── app │ │ ├── app.js │ │ ├── error │ │ │ ├── error.controller.js │ │ │ ├── error.events.js │ │ │ ├── error.routing.js │ │ │ └── error.views.js │ │ └── todo │ │ │ ├── todo.controller.js │ │ │ ├── todo.events.js │ │ │ ├── todo.i18n.js │ │ │ ├── todo.models.js │ │ │ ├── todo.routing.js │ │ │ └── todo.views.js │ │ └── vendor │ │ ├── Backbone.ModelBinder.js │ │ ├── backbone.js │ │ ├── backbone.marionette.js │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ ├── i18next-1.5.8.js │ │ ├── jquery-1.8.2.js │ │ ├── spin.js │ │ └── underscore.js │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ └── todo │ ├── TodoTestUtil.java │ ├── config │ └── UnitTestContext.java │ ├── controller │ └── TodoControllerTest.java │ ├── dto │ └── FormValidationErrorDTOTest.java │ ├── model │ └── TodoTest.java │ └── service │ └── RepositoryTodoServiceTest.java ├── security-annotations ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ ├── net │ │ │ └── petrikainulainen │ │ │ │ └── spring │ │ │ │ └── testmvc │ │ │ │ ├── IntegrationTestUtil.java │ │ │ │ ├── common │ │ │ │ └── controller │ │ │ │ │ └── ITHomeControllerTest.java │ │ │ │ ├── todo │ │ │ │ └── controller │ │ │ │ │ └── ITTodoControllerTest.java │ │ │ │ └── user │ │ │ │ ├── ITAuthenticationTest.java │ │ │ │ └── controller │ │ │ │ └── ITUserControllerTest.java │ │ └── org │ │ │ └── springframework │ │ │ └── test │ │ │ └── web │ │ │ └── server │ │ │ └── samples │ │ │ └── context │ │ │ ├── GenericWebContextLoader.java │ │ │ ├── SecurityRequestPostProcessors.java │ │ │ └── WebContextLoader.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ ├── controller │ │ │ │ └── HomeController.java │ │ │ └── util │ │ │ │ └── LocaleContextHolderWrapper.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ └── PersistenceContext.java │ │ │ ├── security │ │ │ ├── authentication │ │ │ │ ├── RestAuthenticationEntryPoint.java │ │ │ │ ├── RestAuthenticationFailureHandler.java │ │ │ │ ├── RestAuthenticationSuccessHandler.java │ │ │ │ └── RestLogoutSuccessHandler.java │ │ │ ├── authorization │ │ │ │ └── TodoPermissionEvaluator.java │ │ │ └── util │ │ │ │ └── SecurityContextUtil.java │ │ │ ├── todo │ │ │ ├── controller │ │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ │ ├── FieldValidationErrorDTO.java │ │ │ │ ├── FormValidationErrorDTO.java │ │ │ │ └── TodoDTO.java │ │ │ ├── exception │ │ │ │ ├── FormValidationError.java │ │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ │ ├── RepositoryTodoService.java │ │ │ │ └── TodoService.java │ │ │ └── user │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ └── dto │ │ │ ├── SecurityRole.java │ │ │ └── UserDTO.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext-security.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ └── jsp │ │ │ └── index.jsp │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── app │ │ ├── app.js │ │ ├── error │ │ │ ├── error.controller.js │ │ │ ├── error.events.js │ │ │ ├── error.routing.js │ │ │ └── error.views.js │ │ ├── todo │ │ │ ├── todo.controller.js │ │ │ ├── todo.events.js │ │ │ ├── todo.i18n.js │ │ │ ├── todo.models.js │ │ │ ├── todo.routing.js │ │ │ └── todo.views.js │ │ └── user │ │ │ ├── user.controller.js │ │ │ ├── user.events.js │ │ │ ├── user.models.js │ │ │ ├── user.routing.js │ │ │ └── user.views.js │ │ └── vendor │ │ ├── Backbone.ModelBinder.js │ │ ├── backbone.js │ │ ├── backbone.marionette.js │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ ├── i18next-1.5.8.js │ │ ├── jquery-1.8.2.js │ │ ├── spin.js │ │ └── underscore.js │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ ├── common │ ├── controller │ │ └── HomeControllerTest.java │ └── util │ │ └── LocaleContextHolderWrapperTest.java │ ├── security │ ├── authentication │ │ ├── RestAuthenticationEntryPointTest.java │ │ ├── RestAuthenticationFailureHandlerTest.java │ │ ├── RestAuthenticationSuccessHandlerTest.java │ │ └── RestLogoutSuccessHandlerTest.java │ ├── authorization │ │ └── TodoPermissionEvaluatorTest.java │ └── util │ │ └── SecurityContextUtilTest.java │ ├── todo │ ├── TodoTestUtil.java │ ├── config │ │ └── UnitTestContext.java │ ├── controller │ │ └── TodoControllerTest.java │ ├── dto │ │ └── FormValidationErrorDTOTest.java │ ├── model │ │ └── TodoTest.java │ └── service │ │ └── RepositoryTodoServiceTest.java │ └── user │ └── controller │ └── UserControllerTest.java ├── security-url-based ├── LICENSE ├── README ├── pom.xml ├── profiles │ ├── dev │ │ └── config.properties │ └── integration-test │ │ └── config.properties └── src │ ├── integration-test │ ├── java │ │ ├── net │ │ │ └── petrikainulainen │ │ │ │ └── spring │ │ │ │ └── testmvc │ │ │ │ ├── IntegrationTestUtil.java │ │ │ │ ├── common │ │ │ │ └── controller │ │ │ │ │ └── ITHomeControllerTest.java │ │ │ │ ├── todo │ │ │ │ └── controller │ │ │ │ │ └── ITTodoControllerTest.java │ │ │ │ └── user │ │ │ │ ├── ITAuthenticationTest.java │ │ │ │ └── controller │ │ │ │ └── ITUserControllerTest.java │ │ └── org │ │ │ └── springframework │ │ │ └── test │ │ │ └── web │ │ │ └── server │ │ │ └── samples │ │ │ └── context │ │ │ ├── GenericWebContextLoader.java │ │ │ ├── SecurityRequestPostProcessors.java │ │ │ └── WebContextLoader.java │ └── resources │ │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ └── todo │ │ └── controller │ │ ├── toDoData-add-expected.xml │ │ ├── toDoData-delete-expected.xml │ │ ├── toDoData-update-expected.xml │ │ └── toDoData.xml │ ├── main │ ├── java │ │ └── net │ │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── common │ │ │ ├── controller │ │ │ │ └── HomeController.java │ │ │ └── util │ │ │ │ └── LocaleContextHolderWrapper.java │ │ │ ├── config │ │ │ ├── ExampleApplicationConfig.java │ │ │ ├── ExampleApplicationContext.java │ │ │ └── PersistenceContext.java │ │ │ ├── security │ │ │ ├── authentication │ │ │ │ ├── RestAuthenticationEntryPoint.java │ │ │ │ ├── RestAuthenticationFailureHandler.java │ │ │ │ ├── RestAuthenticationSuccessHandler.java │ │ │ │ └── RestLogoutSuccessHandler.java │ │ │ └── util │ │ │ │ └── SecurityContextUtil.java │ │ │ ├── todo │ │ │ ├── controller │ │ │ │ └── TodoController.java │ │ │ ├── dto │ │ │ │ ├── FieldValidationErrorDTO.java │ │ │ │ ├── FormValidationErrorDTO.java │ │ │ │ └── TodoDTO.java │ │ │ ├── exception │ │ │ │ ├── FormValidationError.java │ │ │ │ └── TodoNotFoundException.java │ │ │ ├── model │ │ │ │ └── Todo.java │ │ │ ├── repository │ │ │ │ └── TodoRepository.java │ │ │ └── service │ │ │ │ ├── RepositoryTodoService.java │ │ │ │ └── TodoService.java │ │ │ └── user │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ └── dto │ │ │ ├── SecurityRole.java │ │ │ └── UserDTO.java │ ├── resources │ │ ├── application.properties │ │ ├── exampleApplicationContext-persistence.xml │ │ ├── exampleApplicationContext-security.xml │ │ ├── exampleApplicationContext.xml │ │ ├── i18n │ │ │ └── messages.properties │ │ └── log4j.properties │ └── webapp │ │ ├── WEB-INF │ │ └── jsp │ │ │ └── index.jsp │ │ └── static │ │ ├── css │ │ ├── bootstrap-responsive.css │ │ ├── bootstrap.css │ │ └── example.css │ │ ├── img │ │ ├── glyphicons-halflings-white.png │ │ └── glyphicons-halflings.png │ │ └── js │ │ ├── app │ │ ├── app.js │ │ ├── error │ │ │ ├── error.controller.js │ │ │ ├── error.events.js │ │ │ ├── error.routing.js │ │ │ └── error.views.js │ │ ├── todo │ │ │ ├── todo.controller.js │ │ │ ├── todo.events.js │ │ │ ├── todo.i18n.js │ │ │ ├── todo.models.js │ │ │ ├── todo.routing.js │ │ │ └── todo.views.js │ │ └── user │ │ │ ├── user.controller.js │ │ │ ├── user.events.js │ │ │ ├── user.models.js │ │ │ ├── user.routing.js │ │ │ └── user.views.js │ │ └── vendor │ │ ├── Backbone.ModelBinder.js │ │ ├── backbone.js │ │ ├── backbone.marionette.js │ │ ├── bootstrap-collapse.js │ │ ├── bootstrap-transition.js │ │ ├── bootstrap.js │ │ ├── handlebars-1.0.rc.1.js │ │ ├── i18next-1.5.8.js │ │ ├── jquery-1.8.2.js │ │ ├── spin.js │ │ └── underscore.js │ └── test │ └── java │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ ├── common │ ├── controller │ │ └── HomeControllerTest.java │ └── util │ │ └── LocaleContextHolderWrapperTest.java │ ├── security │ ├── authentication │ │ ├── RestAuthenticationEntryPointTest.java │ │ ├── RestAuthenticationFailureHandlerTest.java │ │ ├── RestAuthenticationSuccessHandlerTest.java │ │ └── RestLogoutSuccessHandlerTest.java │ └── util │ │ └── SecurityContextUtilTest.java │ ├── todo │ ├── TodoTestUtil.java │ ├── config │ │ └── UnitTestContext.java │ ├── controller │ │ └── TodoControllerTest.java │ ├── dto │ │ └── FormValidationErrorDTOTest.java │ ├── model │ │ └── TodoTest.java │ └── service │ │ └── RepositoryTodoServiceTest.java │ └── user │ └── controller │ └── UserControllerTest.java └── spring-32 ├── LICENSE ├── README ├── pom.xml ├── profiles ├── dev │ └── config.properties └── integration-test │ └── config.properties └── src ├── integration-test ├── java │ ├── net │ │ └── petrikainulainen │ │ │ └── spring │ │ │ └── testmvc │ │ │ ├── IntegrationTestUtil.java │ │ │ ├── common │ │ │ └── controller │ │ │ │ └── ITHomeControllerTest.java │ │ │ ├── todo │ │ │ └── controller │ │ │ │ └── ITTodoControllerTest.java │ │ │ └── user │ │ │ ├── ITAuthenticationTest.java │ │ │ └── controller │ │ │ └── ITUserControllerTest.java │ └── org │ │ └── springframework │ │ └── test │ │ └── web │ │ └── server │ │ └── samples │ │ └── context │ │ └── SecurityRequestPostProcessors.java └── resources │ └── net │ └── petrikainulainen │ └── spring │ └── testmvc │ └── todo │ └── controller │ ├── toDoData-add-expected.xml │ ├── toDoData-delete-expected.xml │ ├── toDoData-update-expected.xml │ └── toDoData.xml ├── main ├── java │ └── net │ │ └── petrikainulainen │ │ └── spring │ │ └── testmvc │ │ ├── common │ │ ├── controller │ │ │ └── HomeController.java │ │ └── util │ │ │ └── LocaleContextHolderWrapper.java │ │ ├── config │ │ ├── ExampleApplicationConfig.java │ │ ├── ExampleApplicationContext.java │ │ └── PersistenceContext.java │ │ ├── security │ │ ├── authentication │ │ │ ├── RestAuthenticationEntryPoint.java │ │ │ ├── RestAuthenticationFailureHandler.java │ │ │ ├── RestAuthenticationSuccessHandler.java │ │ │ └── RestLogoutSuccessHandler.java │ │ ├── authorization │ │ │ └── TodoPermissionEvaluator.java │ │ └── util │ │ │ └── SecurityContextUtil.java │ │ ├── todo │ │ ├── controller │ │ │ └── TodoController.java │ │ ├── dto │ │ │ ├── FieldValidationErrorDTO.java │ │ │ ├── FormValidationErrorDTO.java │ │ │ └── TodoDTO.java │ │ ├── exception │ │ │ ├── FormValidationError.java │ │ │ └── TodoNotFoundException.java │ │ ├── model │ │ │ └── Todo.java │ │ ├── repository │ │ │ └── TodoRepository.java │ │ └── service │ │ │ ├── RepositoryTodoService.java │ │ │ └── TodoService.java │ │ └── user │ │ ├── controller │ │ └── UserController.java │ │ └── dto │ │ ├── SecurityRole.java │ │ └── UserDTO.java ├── resources │ ├── application.properties │ ├── exampleApplicationContext-persistence.xml │ ├── exampleApplicationContext-security.xml │ ├── exampleApplicationContext.xml │ ├── i18n │ │ └── messages.properties │ └── log4j.properties └── webapp │ ├── WEB-INF │ └── jsp │ │ └── index.jsp │ └── static │ ├── css │ ├── bootstrap-responsive.css │ ├── bootstrap.css │ └── example.css │ ├── img │ ├── glyphicons-halflings-white.png │ └── glyphicons-halflings.png │ └── js │ ├── app │ ├── app.js │ ├── error │ │ ├── error.controller.js │ │ ├── error.events.js │ │ ├── error.routing.js │ │ └── error.views.js │ ├── todo │ │ ├── todo.controller.js │ │ ├── todo.events.js │ │ ├── todo.i18n.js │ │ ├── todo.models.js │ │ ├── todo.routing.js │ │ └── todo.views.js │ └── user │ │ ├── user.controller.js │ │ ├── user.events.js │ │ ├── user.models.js │ │ ├── user.routing.js │ │ └── user.views.js │ └── vendor │ ├── Backbone.ModelBinder.js │ ├── backbone.js │ ├── backbone.marionette.js │ ├── bootstrap-collapse.js │ ├── bootstrap-transition.js │ ├── bootstrap.js │ ├── handlebars-1.0.rc.1.js │ ├── i18next-1.5.8.js │ ├── jquery-1.8.2.js │ ├── spin.js │ └── underscore.js └── test └── java └── net └── petrikainulainen └── spring └── testmvc ├── common ├── controller │ └── HomeControllerTest.java └── util │ └── LocaleContextHolderWrapperTest.java ├── security ├── authentication │ ├── RestAuthenticationEntryPointTest.java │ ├── RestAuthenticationFailureHandlerTest.java │ ├── RestAuthenticationSuccessHandlerTest.java │ └── RestLogoutSuccessHandlerTest.java ├── authorization │ └── TodoPermissionEvaluatorTest.java └── util │ └── SecurityContextUtilTest.java ├── todo ├── TodoTestUtil.java ├── config │ └── UnitTestContext.java ├── controller │ └── TodoControllerTest.java ├── dto │ └── FormValidationErrorDTOTest.java ├── model │ └── TodoTest.java └── service │ └── RepositoryTodoServiceTest.java └── user └── controller └── UserControllerTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | target 3 | .idea 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Test With Spring Course 2 | 3 | If you are struggling to write good automated tests for Spring web applications, you are not alone! [I have launched a video course](https://www.testwithspring.com/?utm_source=github&utm_medium=social&utm_content=spring-mvc-test&utm_campaign=test-with-spring-course-presales) that describes how you can write automated tests which embrace change and help you to save your time (and nerves). 4 | 5 | # Spring MVC Test Tutorial 6 | 7 | This repository contains the example applications of my [Spring MVC Test tutorial](http://www.petrikainulainen.net/spring-mvc-test-tutorial/). 8 | 9 | -------------------------------------------------------------------------------- /configuration/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /configuration/README: -------------------------------------------------------------------------------- 1 | This is an example application of my blog entry: 2 | 3 | Integration Testing of Spring MVC Applications: Configuration 4 | 5 | http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-configuration/ 6 | 7 | RUNNING THE APPLICATION: 8 | 9 | - Download and install Maven 3 (http://maven.apache.org/download.html#Installation). If you have already installed Maven 3, you can skip this step. 10 | - Go the root directory of project (The one which contains the pom.xml file) 11 | - Run command mvn clean jetty:run 12 | - Start your browser and go to the location: http://localhost:8080 13 | 14 | RUNNING TESTS: 15 | 16 | - You can run unit tests by using this command: mvn test -P dev 17 | - You can run integration tests by using this command: mvn verify -P integration-test 18 | 19 | -------------------------------------------------------------------------------- /configuration/profiles/dev/config.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/configuration/profiles/dev/config.properties -------------------------------------------------------------------------------- /configuration/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/configuration/profiles/integration-test/config.properties -------------------------------------------------------------------------------- /configuration/src/integration-test/java/net/petrikainulainen/spring/testmvc/controller/ITStandaloneHomeControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.controller; 2 | 3 | import org.junit.Test; 4 | import org.springframework.test.web.server.setup.MockMvcBuilders; 5 | 6 | import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get; 7 | import static org.springframework.test.web.server.result.MockMvcResultMatchers.status; 8 | import static org.springframework.test.web.server.result.MockMvcResultMatchers.view; 9 | 10 | /** 11 | * @author Petri Kainulainen 12 | */ 13 | public class ITStandaloneHomeControllerTest { 14 | 15 | @Test 16 | public void showHomePage() throws Exception { 17 | MockMvcBuilders.standaloneSetup(new HomeController()).build() 18 | .perform(get("/")) 19 | .andExpect(status().isOk()) 20 | .andExpect(view().name(HomeController.VIEW_HOME_PAGE)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /configuration/src/integration-test/java/org/springframework/test/web/server/samples/context/WebContextLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.test.web.server.samples.context; 17 | 18 | public class WebContextLoader extends GenericWebContextLoader { 19 | 20 | public WebContextLoader() { 21 | super("src/test/resources/META-INF/web-resources", false); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /configuration/src/main/java/net/petrikainulainen/spring/testmvc/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String VIEW_HOME_PAGE = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return VIEW_HOME_PAGE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /configuration/src/main/resources/i18n/messages.properties: -------------------------------------------------------------------------------- 1 | spring.test.mvc.example.title=spring-test-mvc Example Application - Configuration 2 | 3 | #Navigation 4 | label.navigation.brand=spring-test-mvc 5 | label.navigation.homepage.link=Home 6 | 7 | #Homepage 8 | label.homepage.title=Home 9 | label.homepage.description=This is the homepage of spring-test-mvc example application. -------------------------------------------------------------------------------- /configuration/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG -------------------------------------------------------------------------------- /configuration/src/main/webapp/WEB-INF/jsp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 | <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 4 | 5 | 6 | 7 | 8 | 9 |

10 |

11 | 12 |

13 | 14 | -------------------------------------------------------------------------------- /configuration/src/main/webapp/WEB-INF/sitemesh3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /configuration/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } -------------------------------------------------------------------------------- /configuration/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/configuration/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /configuration/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/configuration/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /configuration/src/test/java/net/petrikainulainen/spring/testmvc/controller/HomeControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | /** 9 | * @author Petri Kainulainen 10 | */ 11 | public class HomeControllerTest { 12 | 13 | private HomeController controller; 14 | 15 | @Before 16 | public void setUp() { 17 | controller = new HomeController(); 18 | } 19 | 20 | @Test 21 | public void showHomePage() { 22 | String view = controller.showHomePage(); 23 | assertEquals(HomeController.VIEW_HOME_PAGE, view); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /controllers-unittest/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /controllers-unittest/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /controllers-unittest/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=false -------------------------------------------------------------------------------- /controllers-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /controllers-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /controllers-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /controllers-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /controllers-unittest/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/WEB-INF/jsp/error/404.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 | <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 5 | 6 | 7 | 8 | 9 | 10 |

11 |
12 |

13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/WEB-INF/jsp/error/error.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 | <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 5 | 6 | 7 | 8 | 9 | 10 |

11 |
12 |

13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/WEB-INF/sitemesh3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/controllers-unittest/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/controllers-unittest/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/static/js/todo.form.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | addValidationErrorClassesToForm(); 3 | 4 | function addValidationErrorClassesToForm() { 5 | $("form").find(".control-group").each(function() { 6 | var errorMessage = $(this).find(".help-inline").text(); 7 | 8 | if (errorMessage) { 9 | $(this).addClass("error"); 10 | } 11 | }) 12 | } 13 | }) -------------------------------------------------------------------------------- /controllers-unittest/src/main/webapp/static/js/todo.view.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | $(".well").on("click", "#delete-todo-link", function(e) { 4 | e.preventDefault(); 5 | 6 | var todoDeleteDialogTempate = Handlebars.compile($("#template-delete-todo-confirmation-dialog").html()); 7 | 8 | $("#view-holder").append(todoDeleteDialogTempate()); 9 | $("#delete-todo-confirmation-dialog").modal(); 10 | }) 11 | 12 | $("#view-holder").on("click", "#cancel-todo-button", function(e) { 13 | e.preventDefault(); 14 | 15 | var deleteConfirmationDialog = $("#delete-todo-confirmation-dialog") 16 | deleteConfirmationDialog.modal('hide'); 17 | deleteConfirmationDialog.remove(); 18 | }); 19 | 20 | $("#view-holder").on("click", "#delete-todo-button", function(e) { 21 | e.preventDefault(); 22 | window.location.href = "/todo/delete/" + $("#todo-id").text(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /controllers-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/TestUtil.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TestUtil { 7 | 8 | private static final String CHARACTER = "a"; 9 | 10 | public static String createRedirectViewPath(String path) { 11 | StringBuilder redirectViewPath = new StringBuilder(); 12 | redirectViewPath.append("redirect:"); 13 | redirectViewPath.append(path); 14 | return redirectViewPath.toString(); 15 | } 16 | 17 | public static String createStringWithLength(int length) { 18 | StringBuilder builder = new StringBuilder(); 19 | 20 | for (int index = 0; index < length; index++) { 21 | builder.append(CHARACTER); 22 | } 23 | 24 | return builder.toString(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /controllers-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/WebTestConstants.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class WebTestConstants { 7 | 8 | public static final String FORM_FIELD_DESCRIPTION = "description"; 9 | public static final String FORM_FIELD_ID = "id"; 10 | public static final String FORM_FIELD_TITLE = "title"; 11 | 12 | private WebTestConstants() {} 13 | } 14 | -------------------------------------------------------------------------------- /controllers-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/dto/TodoDTOBuilder.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoDTOBuilder { 7 | 8 | private TodoDTO dto; 9 | 10 | public TodoDTOBuilder() { 11 | dto = new TodoDTO(); 12 | } 13 | 14 | public TodoDTOBuilder id(Long id) { 15 | dto.setId(id); 16 | return this; 17 | } 18 | 19 | public TodoDTOBuilder description(String description) { 20 | dto.setDescription(description); 21 | return this; 22 | } 23 | 24 | public TodoDTOBuilder title(String title) { 25 | dto.setTitle(title); 26 | return this; 27 | } 28 | 29 | public TodoDTO build() { 30 | return dto; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /controllers-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/model/TodoBuilder.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.model; 2 | 3 | import org.springframework.test.util.ReflectionTestUtils; 4 | 5 | /** 6 | * @author Petri Kainulainen 7 | */ 8 | public class TodoBuilder { 9 | 10 | private Todo model; 11 | 12 | public TodoBuilder() { 13 | model = new Todo(); 14 | } 15 | 16 | public TodoBuilder id(Long id) { 17 | ReflectionTestUtils.setField(model, "id", id); 18 | return this; 19 | } 20 | 21 | public TodoBuilder description(String description) { 22 | model.update(description, model.getTitle()); 23 | return this; 24 | } 25 | 26 | public TodoBuilder title(String title) { 27 | model.update(model.getDescription(), title); 28 | return this; 29 | } 30 | 31 | public Todo build() { 32 | return model; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /controllers-unittest/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=OFF -------------------------------------------------------------------------------- /controllers-unittest/src/test/resources/testContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /controllers/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /controllers/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /controllers/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true -------------------------------------------------------------------------------- /controllers/src/integration-test/java/org/springframework/test/web/server/samples/context/WebContextLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.test.web.server.samples.context; 17 | 18 | public class WebContextLoader extends GenericWebContextLoader { 19 | 20 | public WebContextLoader() { 21 | super("src/test/resources/META-INF/web-resources", false); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /controllers/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /controllers/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /controllers/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /controllers/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /controllers/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /controllers/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /controllers/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /controllers/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /controllers/src/main/webapp/WEB-INF/jsp/error/404.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 | <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 5 | 6 | 7 | 8 | 9 | 10 |

11 |
12 |

13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /controllers/src/main/webapp/WEB-INF/jsp/error/error.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 3 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 | <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 5 | 6 | 7 | 8 | 9 | 10 |

11 |
12 |

13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /controllers/src/main/webapp/WEB-INF/sitemesh3.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /controllers/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /controllers/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/controllers/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /controllers/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/controllers/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /controllers/src/main/webapp/static/js/todo.form.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | addValidationErrorClassesToForm(); 3 | 4 | function addValidationErrorClassesToForm() { 5 | $("form").find(".control-group").each(function() { 6 | var errorMessage = $(this).find(".help-inline").text(); 7 | 8 | if (errorMessage) { 9 | $(this).addClass("error"); 10 | } 11 | }) 12 | } 13 | }) -------------------------------------------------------------------------------- /controllers/src/main/webapp/static/js/todo.view.js: -------------------------------------------------------------------------------- 1 | $(function() { 2 | 3 | $(".well").on("click", "#delete-todo-link", function(e) { 4 | e.preventDefault(); 5 | 6 | var todoDeleteDialogTempate = Handlebars.compile($("#template-delete-todo-confirmation-dialog").html()); 7 | 8 | $("#view-holder").append(todoDeleteDialogTempate()); 9 | $("#delete-todo-confirmation-dialog").modal(); 10 | }) 11 | 12 | $("#view-holder").on("click", "#cancel-todo-button", function(e) { 13 | e.preventDefault(); 14 | 15 | var deleteConfirmationDialog = $("#delete-todo-confirmation-dialog") 16 | deleteConfirmationDialog.modal('hide'); 17 | deleteConfirmationDialog.remove(); 18 | }); 19 | 20 | $("#view-holder").on("click", "#delete-todo-button", function(e) { 21 | e.preventDefault(); 22 | window.location.href = "/todo/delete/" + $("#todo-id").text(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /controllers/src/test/java/net/petrikainulainen/spring/testmvc/common/controller/ErrorControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | /** 9 | * @author Petri Kainulainen 10 | */ 11 | public class ErrorControllerTest { 12 | 13 | private ErrorController controller; 14 | 15 | @Before 16 | public void setUp() { 17 | controller = new ErrorController(); 18 | } 19 | 20 | @Test 21 | public void show404Page() { 22 | String view = controller.show404Page(); 23 | assertEquals(ErrorController.VIEW_NOT_FOUND, view); 24 | } 25 | 26 | @Test 27 | public void showInternalServerErrorPage() { 28 | String view = controller.showInternalServerErrorPage(); 29 | assertEquals(ErrorController.VIEW_INTERNAL_SERVER_ERROR, view); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /controllers/src/test/java/net/petrikainulainen/spring/testmvc/todo/config/UnitTestContext.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | @Configuration 11 | public class UnitTestContext { 12 | 13 | @Bean 14 | public LocalValidatorFactoryBean validator() { 15 | return new LocalValidatorFactoryBean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/integration-test/java/net/petrikainulainen/springmvctest/junit5/IntegrationTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5; 2 | 3 | import org.junit.jupiter.api.Tag; 4 | import org.springframework.test.context.ActiveProfiles; 5 | 6 | import java.lang.annotation.Documented; 7 | import java.lang.annotation.ElementType; 8 | import java.lang.annotation.Inherited; 9 | import java.lang.annotation.Retention; 10 | import java.lang.annotation.RetentionPolicy; 11 | import java.lang.annotation.Target; 12 | 13 | /** 14 | * This annotation allows us to identify the test methods which must 15 | * be run when we run integration tests. 16 | */ 17 | @Target({ ElementType.TYPE, ElementType.METHOD }) 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Documented 20 | @Inherited 21 | @ActiveProfiles("integrationTest") 22 | @Tag("integrationTest") 23 | public @interface IntegrationTest { 24 | } 25 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/integration-test/java/net/petrikainulainen/springmvctest/junit5/todoitem/TodoItemTableRow.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todoitem; 2 | 3 | /** 4 | * Specifies the row indexes of each todo item that's found from 5 | * the todo_item database table. 6 | */ 7 | enum TodoItemTableRow { 8 | 9 | NEW_TODO_ITEM(0), 10 | WRITE_BLOG_POST(1), 11 | WRITE_SAMPLE_CODE(0); 12 | 13 | private final int index; 14 | 15 | private TodoItemTableRow(int index) { 16 | this.index = index; 17 | } 18 | 19 | int getIndex() { 20 | return index; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/integration-test/resources/db/clear-database.sql: -------------------------------------------------------------------------------- 1 | DELETE FROM tag; 2 | DELETE FROM todo_item; -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/integration-test/resources/db/init-todo-items.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO todo_item ( 2 | id, 3 | description, 4 | title, 5 | status 6 | ) VALUES ( 7 | 1, 8 | 'Write runnable Maven project', 9 | 'Write sample code', 10 | 'DONE' 11 | ); 12 | 13 | INSERT INTO todo_item ( 14 | id, 15 | description, 16 | title, 17 | status 18 | ) VALUES ( 19 | 2, 20 | 'Publish the blog post on petrikainulainen.net', 21 | 'Write new blog post', 22 | 'OPEN' 23 | ); -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/java/net/petrikainulainen/springmvctest/junit5/SpringMVCTestExampleApplication.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringMVCTestExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringMVCTestExampleApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/java/net/petrikainulainen/springmvctest/junit5/todoitem/TodoItemStatus.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todoitem; 2 | 3 | /** 4 | * Identifies the status of a todo item. 5 | */ 6 | public enum TodoItemStatus { 7 | /** 8 | * The todo item is done. 9 | */ 10 | DONE, 11 | /** 12 | * The todo item is under work. 13 | */ 14 | IN_PROGRESS, 15 | /** 16 | * No one has started working on the todo item. 17 | */ 18 | OPEN 19 | } 20 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/java/net/petrikainulainen/springmvctest/junit5/todoitem/TodoListItemDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todoitem; 2 | 3 | /** 4 | * Contains the minimum information of one todo item which 5 | * is used when we want to get a list of todo items. 6 | */ 7 | public class TodoListItemDTO { 8 | 9 | private Long id; 10 | private String title; 11 | 12 | public Long getId() { 13 | return id; 14 | } 15 | 16 | public String getTitle() { 17 | return title; 18 | } 19 | 20 | public void setId(Long id) { 21 | this.id = id; 22 | } 23 | 24 | public void setTitle(String title) { 25 | this.title = title; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/resources/application-integrationTest.yml: -------------------------------------------------------------------------------- 1 | logging: 2 | level: 3 | root: DEBUG 4 | spring: 5 | datasource: 6 | url: jdbc:tc:postgresql:16.1:///springmvctest 7 | username: springmvctest 8 | password: springmvctest -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | logging: 2 | level: 3 | root: ERROR 4 | spring: 5 | datasource: 6 | url: should_be_overwritten 7 | username: should_be_overwritten 8 | password: should_be_overwritten -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/resources/db/migration/V001__create_todo_item_table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE enum_todo_item_status( 2 | id bigserial NOT NULL, 3 | enum_value text NOT NULL, 4 | value_description text NOT NULL, 5 | CONSTRAINT enum_todo_item_status_pk PRIMARY KEY (id), 6 | CONSTRAINT enum_todo_item_status_unique UNIQUE (enum_value) 7 | ); 8 | 9 | CREATE TABLE todo_item( 10 | id bigserial NOT NULL, 11 | description text NOT NULL, 12 | status text NOT NULL DEFAULT 'OPEN', 13 | title text NOT NULL, 14 | CONSTRAINT todo_item_pk PRIMARY KEY (id), 15 | CONSTRAINT todo_item_status_fk FOREIGN KEY (status) REFERENCES enum_todo_item_status(enum_value) 16 | ); 17 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/resources/db/migration/V002__insert_valid_todo_item_statuses.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO enum_todo_item_status 2 | (enum_value, value_description) 3 | VALUES 4 | ('DONE', 'The todo item is done.'); 5 | 6 | INSERT INTO enum_todo_item_status 7 | (enum_value, value_description) 8 | VALUES 9 | ('IN_PROGRESS', 'The todo item is in progress.'); 10 | 11 | INSERT INTO enum_todo_item_status 12 | (enum_value, value_description) 13 | VALUES 14 | ('OPEN', 'The todo item is waiting for someone to start working on it.'); -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/main/resources/db/migration/V003__create_tag_table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE tag( 2 | id bigserial NOT NULL, 3 | name text NOT NULL, 4 | todo_item_id bigint NOT NULL, 5 | CONSTRAINT tag_pk PRIMARY KEY (id), 6 | CONSTRAINT tag_todo_item_fk FOREIGN KEY (todo_item_id) REFERENCES todo_item(id) ON DELETE CASCADE 7 | ); -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/test/java/net/petrikainulainen/springmvctest/junit5/UnitTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5; 2 | 3 | import org.junit.jupiter.api.Tag; 4 | 5 | import java.lang.annotation.Documented; 6 | import java.lang.annotation.ElementType; 7 | import java.lang.annotation.Inherited; 8 | import java.lang.annotation.Retention; 9 | import java.lang.annotation.RetentionPolicy; 10 | import java.lang.annotation.Target; 11 | 12 | /** 13 | * This annotation allows us to identify the test methods which must 14 | * be run when we run unit tests. 15 | */ 16 | @Target({ ElementType.TYPE, ElementType.METHOD }) 17 | @Retention(RetentionPolicy.RUNTIME) 18 | @Documented 19 | @Inherited 20 | @Tag("unitTest") 21 | public @interface UnitTest { 22 | } 23 | -------------------------------------------------------------------------------- /junit-5/integration-tests/repositories-jooq/src/test/java/net/petrikainulainen/springmvctest/junit5/UnitTestDemoTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Test; 5 | 6 | @UnitTest 7 | class UnitTestDemoTest { 8 | 9 | @Test 10 | @DisplayName("Should be run when we run unit tests") 11 | void shouldBeRunWhenWeRunUnitTests() {} 12 | } 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2025 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/README.md: -------------------------------------------------------------------------------- 1 | # Writing Unit Tests With MockMvcTester - REST API 2 | 3 | This example demonstrates how you can write unit tests for a Spring MVC REST API by using JUnit 5 and MockMvcTester. 4 | 5 | ## Running Unit Tests With Maven 6 | 7 | You can run unit tests with Maven by running the following command at command prompt: 8 | 9 | mvn clean test -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/CreateTodoItemDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | import jakarta.validation.constraints.NotBlank; 4 | import jakarta.validation.constraints.Size; 5 | 6 | /** 7 | * Contains the information of a new todo item that's 8 | * saved to a database. 9 | */ 10 | public class CreateTodoItemDTO { 11 | 12 | @Size(max = 1000) 13 | private String description; 14 | 15 | @NotBlank 16 | @Size(max = 100) 17 | private String title; 18 | 19 | public CreateTodoItemDTO() {} 20 | 21 | public String getDescription() { 22 | return description; 23 | } 24 | 25 | public String getTitle() { 26 | return title; 27 | } 28 | 29 | public void setDescription(String description) { 30 | this.description = description; 31 | } 32 | 33 | public void setTitle(String title) { 34 | this.title = title; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TagDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the information of a single tag that's 5 | * can be added to a todo item. 6 | */ 7 | public class TagDTO { 8 | 9 | private Long id; 10 | private String name; 11 | 12 | public TagDTO() {} 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "TagDTO{" + 33 | "id=" + id + 34 | ", name='" + name + '\'' + 35 | '}'; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * This exception is thrown when the requested todo item 5 | * isn't found from the database. 6 | */ 7 | public class TodoItemNotFoundException extends RuntimeException { 8 | 9 | public TodoItemNotFoundException(String message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemStatus.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the status of a single todo item. The legal statuses are: 5 | * 18 | */ 19 | public enum TodoItemStatus { 20 | OPEN, 21 | IN_PROGRESS, 22 | DONE 23 | } 24 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/web/FieldErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * This DTO contains the validation error that describe the 5 | * problem found from a JSON field. 6 | */ 7 | public class FieldErrorDTO { 8 | 9 | private final String field; 10 | private final String errorCode; 11 | 12 | public FieldErrorDTO(String field, String errorCode) { 13 | this.field = field; 14 | this.errorCode = errorCode; 15 | } 16 | 17 | public String getField() { 18 | return field; 19 | } 20 | 21 | public String getErrorCode() { 22 | return errorCode; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/web/ValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * This class contains the field errors that describe the validation 8 | * errors found from the incoming HTTP request. 9 | */ 10 | public class ValidationErrorDTO { 11 | 12 | private final List fieldErrors = new ArrayList<>(); 13 | 14 | public ValidationErrorDTO() { 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldErrorDTO error = new FieldErrorDTO(path, message); 19 | fieldErrors.add(error); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/mock-mvc-tester/rest-api/src/test/java/net/petrikainulainen/springmvctest/junit5/web/WebTestUtil.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * Provides static factory methods which are useful when 5 | * we are writing automated tests for Spring MVC controllers. 6 | */ 7 | public class WebTestUtil { 8 | 9 | /** 10 | * Prevent instantion. 11 | */ 12 | private WebTestUtil() {} 13 | 14 | /** 15 | * Creates a new string. 16 | * @param length The length of the created string. 17 | * @return The created string. 18 | */ 19 | public static String createStringWithLength(int length) { 20 | StringBuilder testString = new StringBuilder(); 21 | 22 | for (int index = 0; index < length; index++) { 23 | testString.append("a"); 24 | } 25 | 26 | return testString.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/README.md: -------------------------------------------------------------------------------- 1 | # Writing Unit Tests for "Normal" Spring MVC Controllers 2 | 3 | This example application demonstrates how we can write unit tests for "normal" 4 | Spring MVC controllers by using JUnit 5 and the Spring MVC Test framework. 5 | 6 | ## Running Unit Tests With Maven 7 | 8 | You can run unit tests with Maven by running the following command at command prompt: 9 | 10 | mvn clean test -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/CreateTodoItemFormDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | import jakarta.validation.constraints.NotBlank; 4 | import jakarta.validation.constraints.Size; 5 | 6 | /** 7 | * Contains the information of a new todo item that's 8 | * saved to a database. 9 | */ 10 | public class CreateTodoItemFormDTO { 11 | 12 | @Size(max = 1000) 13 | private String description; 14 | 15 | @NotBlank 16 | @Size(max = 100) 17 | private String title; 18 | 19 | public CreateTodoItemFormDTO() {} 20 | 21 | public String getDescription() { 22 | return description; 23 | } 24 | 25 | public String getTitle() { 26 | return title; 27 | } 28 | 29 | public void setDescription(String description) { 30 | this.description = description; 31 | } 32 | 33 | public void setTitle(String title) { 34 | this.title = title; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TagDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the information of a single tag that's 5 | * can be added to a todo item. 6 | */ 7 | public class TagDTO { 8 | 9 | private Long id; 10 | private String name; 11 | 12 | public TagDTO() {} 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "TagDTO{" + 33 | "id=" + id + 34 | ", name='" + name + '\'' + 35 | '}'; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * This exception is thrown when the requested todo item 5 | * isn't found from the database. 6 | */ 7 | public class TodoItemNotFoundException extends RuntimeException { 8 | 9 | public TodoItemNotFoundException(String message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemStatus.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the status of a single todo item. The legal statuses are: 5 | *
    6 | *
  • 7 | * OPEN means that we haven't started working on 8 | * the todo item. 9 | *
  • 10 | *
  • 11 | * IN_PROGRESS means that we are currently working 12 | * on the todo item. 13 | *
  • 14 | *
  • 15 | * DONE means that we have finished the todo item. 16 | *
  • 17 | *
18 | */ 19 | public enum TodoItemStatus { 20 | OPEN, 21 | IN_PROGRESS, 22 | DONE 23 | } 24 | -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/normal-controllers/src/test/java/net/petrikainulainen/springmvctest/junit5/web/WebTestUtil.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * Provides static factory methods which are useful when 5 | * we are writing automated tests for Spring MVC controllers. 6 | */ 7 | public class WebTestUtil { 8 | 9 | /** 10 | * Prevent instantion. 11 | */ 12 | private WebTestUtil() {} 13 | 14 | /** 15 | * Creates a new string. 16 | * @param length The length of the created string. 17 | * @return The created string. 18 | */ 19 | public static String createStringWithLength(int length) { 20 | StringBuilder testString = new StringBuilder(); 21 | 22 | for (int index = 0; index < length; index++) { 23 | testString.append("a"); 24 | } 25 | 26 | return testString.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/README.md: -------------------------------------------------------------------------------- 1 | # How to Write MockMvc Tests Without ObjectMapper 2 | 3 | This example demonstrates how you can write unit tests for a Spring MVC REST API by using JUnit 5 and the Spring 4 | MVC Test framework. Also, this example doesn't use ObjectMapper when it creates the request body that's send to 5 | the system under test. 6 | 7 | ## Running Unit Tests With Maven 8 | 9 | You can run unit tests with Maven by running the following command at command prompt: 10 | 11 | mvn clean test -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/CreateTodoItemDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | import jakarta.validation.constraints.NotBlank; 4 | import jakarta.validation.constraints.Size; 5 | 6 | /** 7 | * Contains the information of a new todo item that's 8 | * saved to a database. 9 | */ 10 | public class CreateTodoItemDTO { 11 | 12 | @Size(max = 1000) 13 | private String description; 14 | 15 | @NotBlank 16 | @Size(max = 100) 17 | private String title; 18 | 19 | public CreateTodoItemDTO() {} 20 | 21 | public String getDescription() { 22 | return description; 23 | } 24 | 25 | public String getTitle() { 26 | return title; 27 | } 28 | 29 | public void setDescription(String description) { 30 | this.description = description; 31 | } 32 | 33 | public void setTitle(String title) { 34 | this.title = title; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TagDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the information of a single tag that's 5 | * can be added to a todo item. 6 | */ 7 | public class TagDTO { 8 | 9 | private Long id; 10 | private String name; 11 | 12 | public TagDTO() {} 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "TagDTO{" + 33 | "id=" + id + 34 | ", name='" + name + '\'' + 35 | '}'; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * This exception is thrown when the requested todo item 5 | * isn't found from the database. 6 | */ 7 | public class TodoItemNotFoundException extends RuntimeException { 8 | 9 | public TodoItemNotFoundException(String message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemStatus.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the status of a single todo item. The legal statuses are: 5 | *
    6 | *
  • 7 | * OPEN means that we haven't started working on 8 | * the todo item. 9 | *
  • 10 | *
  • 11 | * IN_PROGRESS means that we are currently working 12 | * on the todo item. 13 | *
  • 14 | *
  • 15 | * DONE means that we have finished the todo item. 16 | *
  • 17 | *
18 | */ 19 | public enum TodoItemStatus { 20 | OPEN, 21 | IN_PROGRESS, 22 | DONE 23 | } 24 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/java/net/petrikainulainen/springmvctest/junit5/web/FieldErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * This DTO contains the validation error that describe the 5 | * problem found from a JSON field. 6 | */ 7 | public class FieldErrorDTO { 8 | 9 | private final String field; 10 | private final String errorCode; 11 | 12 | public FieldErrorDTO(String field, String errorCode) { 13 | this.field = field; 14 | this.errorCode = errorCode; 15 | } 16 | 17 | public String getField() { 18 | return field; 19 | } 20 | 21 | public String getErrorCode() { 22 | return errorCode; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/java/net/petrikainulainen/springmvctest/junit5/web/ValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * This class contains the field errors that describe the validation 8 | * errors found from the incoming HTTP request. 9 | */ 10 | public class ValidationErrorDTO { 11 | 12 | private final List fieldErrors = new ArrayList<>(); 13 | 14 | public ValidationErrorDTO() { 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldErrorDTO error = new FieldErrorDTO(path, message); 19 | fieldErrors.add(error); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api-no-object-mapper/src/test/java/net/petrikainulainen/springmvctest/junit5/web/WebTestUtil.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * Provides static factory methods which are useful when 5 | * we are writing automated tests for Spring MVC controllers. 6 | */ 7 | public class WebTestUtil { 8 | 9 | /** 10 | * Prevent instantion. 11 | */ 12 | private WebTestUtil() {} 13 | 14 | /** 15 | * Creates a new string. 16 | * @param length The length of the created string. 17 | * @return The created string. 18 | */ 19 | public static String createStringWithLength(int length) { 20 | StringBuilder testString = new StringBuilder(); 21 | 22 | for (int index = 0; index < length; index++) { 23 | testString.append("a"); 24 | } 25 | 26 | return testString.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/README.md: -------------------------------------------------------------------------------- 1 | # Writing Unit Tests for a Spring MVC REST API 2 | 3 | This example application demonstrates how we can write unit tests for a 4 | Spring MVC REST API by using JUnit 5 and the Spring MVC Test framework. 5 | 6 | ## Running Unit Tests With Maven 7 | 8 | You can run unit tests with Maven by running the following command at command prompt: 9 | 10 | mvn clean test -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/CreateTodoItemDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | import jakarta.validation.constraints.NotBlank; 4 | import jakarta.validation.constraints.Size; 5 | 6 | /** 7 | * Contains the information of a new todo item that's 8 | * saved to a database. 9 | */ 10 | public class CreateTodoItemDTO { 11 | 12 | @Size(max = 1000) 13 | private String description; 14 | 15 | @NotBlank 16 | @Size(max = 100) 17 | private String title; 18 | 19 | public CreateTodoItemDTO() {} 20 | 21 | public String getDescription() { 22 | return description; 23 | } 24 | 25 | public String getTitle() { 26 | return title; 27 | } 28 | 29 | public void setDescription(String description) { 30 | this.description = description; 31 | } 32 | 33 | public void setTitle(String title) { 34 | this.title = title; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TagDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the information of a single tag that's 5 | * can be added to a todo item. 6 | */ 7 | public class TagDTO { 8 | 9 | private Long id; 10 | private String name; 11 | 12 | public TagDTO() {} 13 | 14 | public Long getId() { 15 | return id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public void setId(Long id) { 23 | this.id = id; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "TagDTO{" + 33 | "id=" + id + 34 | ", name='" + name + '\'' + 35 | '}'; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * This exception is thrown when the requested todo item 5 | * isn't found from the database. 6 | */ 7 | public class TodoItemNotFoundException extends RuntimeException { 8 | 9 | public TodoItemNotFoundException(String message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/todo/TodoItemStatus.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.todo; 2 | 3 | /** 4 | * Contains the status of a single todo item. The legal statuses are: 5 | *
    6 | *
  • 7 | * OPEN means that we haven't started working on 8 | * the todo item. 9 | *
  • 10 | *
  • 11 | * IN_PROGRESS means that we are currently working 12 | * on the todo item. 13 | *
  • 14 | *
  • 15 | * DONE means that we have finished the todo item. 16 | *
  • 17 | *
18 | */ 19 | public enum TodoItemStatus { 20 | OPEN, 21 | IN_PROGRESS, 22 | DONE 23 | } 24 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/web/FieldErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * This DTO contains the validation error that describe the 5 | * problem found from a JSON field. 6 | */ 7 | public class FieldErrorDTO { 8 | 9 | private final String field; 10 | private final String errorCode; 11 | 12 | public FieldErrorDTO(String field, String errorCode) { 13 | this.field = field; 14 | this.errorCode = errorCode; 15 | } 16 | 17 | public String getField() { 18 | return field; 19 | } 20 | 21 | public String getErrorCode() { 22 | return errorCode; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/java/net/petrikainulainen/springmvctest/junit5/web/ValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * This class contains the field errors that describe the validation 8 | * errors found from the incoming HTTP request. 9 | */ 10 | public class ValidationErrorDTO { 11 | 12 | private final List fieldErrors = new ArrayList<>(); 13 | 14 | public ValidationErrorDTO() { 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldErrorDTO error = new FieldErrorDTO(path, message); 19 | fieldErrors.add(error); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /junit-5/unit-tests/rest-api/src/test/java/net/petrikainulainen/springmvctest/junit5/web/WebTestUtil.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.springmvctest.junit5.web; 2 | 3 | /** 4 | * Provides static factory methods which are useful when 5 | * we are writing automated tests for Spring MVC controllers. 6 | */ 7 | public class WebTestUtil { 8 | 9 | /** 10 | * Prevent instantion. 11 | */ 12 | private WebTestUtil() {} 13 | 14 | /** 15 | * Creates a new string. 16 | * @param length The length of the created string. 17 | * @return The created string. 18 | */ 19 | public static String createStringWithLength(int length) { 20 | StringBuilder testString = new StringBuilder(); 21 | 22 | for (int index = 0; index < length; index++) { 23 | testString.append("a"); 24 | } 25 | 26 | return testString.toString(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /rest-jsonpath/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /rest-jsonpath/README: -------------------------------------------------------------------------------- 1 | This is an example application of my blog entry that describes how we can create assertions 2 | by using the JsonPath library. 3 | 4 | Integration Testing of Spring MVC Applications: Creating assertion with JsonPath (Not published yet) 5 | 6 | RUNNING THE APPLICATION: 7 | 8 | - Download and install Maven 3 (http://maven.apache.org/download.html#Installation). If you have already installed Maven 3, you can skip this step. 9 | - Go the root directory of project (The one which contains the pom.xml file) 10 | - Run command mvn clean jetty:run 11 | - Start your browser and go to the location: http://localhost:8080 12 | - Use credentials (user/password) to log in. 13 | 14 | RUNNING TESTS: 15 | 16 | - You can run unit tests by using this command: mvn test -P dev 17 | - You can run integration tests by using this command: mvn verify -P integration-test 18 | 19 | -------------------------------------------------------------------------------- /rest-jsonpath/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /rest-jsonpath/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true -------------------------------------------------------------------------------- /rest-jsonpath/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /rest-jsonpath/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /rest-jsonpath/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /rest-jsonpath/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/common/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String HOME_VIEW = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return HOME_VIEW; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/common/util/LocaleContextHolderWrapper.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.i18n.LocaleContextHolder; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.Locale; 9 | 10 | /** 11 | * @author Petri Kainulainen 12 | */ 13 | @Component 14 | public class LocaleContextHolderWrapper { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(LocaleContextHolderWrapper.class); 17 | 18 | public Locale getCurrentLocale() { 19 | LOGGER.debug("Getting current locale"); 20 | return LocaleContextHolder.getLocale(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | import org.springframework.security.web.AuthenticationEntryPoint; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 15 | @Override 16 | public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException { 17 | response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized" ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestLogoutSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.Authentication; 4 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestLogoutSuccessHandler implements LogoutSuccessHandler { 15 | @Override 16 | public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 17 | response.setStatus(HttpServletResponse.SC_OK); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FieldValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class FieldValidationErrorDTO { 7 | 8 | private String path; 9 | private String message; 10 | 11 | public FieldValidationErrorDTO(String path, String message) { 12 | this.path = path; 13 | this.message = message; 14 | } 15 | 16 | public String getPath() { 17 | return path; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FormValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public class FormValidationErrorDTO { 10 | 11 | private List fieldErrors = new ArrayList(); 12 | 13 | public FormValidationErrorDTO() { 14 | 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldValidationErrorDTO fieldError = new FieldValidationErrorDTO(path, message); 19 | fieldErrors.add(fieldError); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/FormValidationError.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | import org.springframework.validation.FieldError; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | public class FormValidationError extends Exception { 11 | 12 | private List fieldErrors; 13 | 14 | public FormValidationError(List fieldErrors) { 15 | this.fieldErrors = fieldErrors; 16 | } 17 | 18 | public List getFieldErrors() { 19 | return fieldErrors; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/SecurityRole.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public enum SecurityRole { 7 | ROLE_USER 8 | } 9 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | import org.apache.commons.lang.builder.ToStringBuilder; 4 | 5 | /** 6 | * @author Petri Kainulainen 7 | */ 8 | public class UserDTO { 9 | 10 | private String username; 11 | 12 | private SecurityRole role; 13 | 14 | public UserDTO(String username, SecurityRole role) { 15 | this.username = username; 16 | this.role = role; 17 | } 18 | 19 | public String getUsername() { 20 | return username; 21 | } 22 | 23 | public SecurityRole getRole() { 24 | return role; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return ToStringBuilder.reflectionToString(this); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /rest-jsonpath/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/rest-jsonpath/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/rest-jsonpath/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/error/error.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.ErrorController = { 2 | 404: function() { 3 | window.log("Rendering 404 view."); 4 | var notFoundView = new TodoApp.Views.NotFoundView(); 5 | TodoApp.mainRegion.show(notFoundView); 6 | }, 7 | notAuthorized: function() { 8 | window.log("Rendering not authorized view"); 9 | var notAuthorizedView = new TodoApp.Views.NotAuthorizedView(); 10 | TodoApp.mainRegion.show(notAuthorizedView); 11 | }, 12 | error: function() { 13 | window.log("Rendering error view."); 14 | var errorView = new TodoApp.Views.ErrorView(); 15 | TodoApp.mainRegion.show(errorView); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/error/error.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("error:404", function() { 2 | window.log("Processing 404 event.") 3 | Backbone.history.navigate("#/error/404"); 4 | }); 5 | 6 | TodoApp.vent.on("error:notAuthorized", function() { 7 | window.log("Processing not authorized event"); 8 | Backbone.history.navigate("#/error/notAuthorized") 9 | }); 10 | 11 | TodoApp.vent.on("error:error", function(){ 12 | window.log("Processing error event") 13 | Backbone.history.navigate("#/error/error"); 14 | }); -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/error/error.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.ErrorRouting = function(){ 2 | var ErrorRouting = {}; 3 | 4 | ErrorRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "error/404": "404", 7 | "error/notAuthorized": "notAuthorized", 8 | "error/error": "error" 9 | } 10 | }); 11 | 12 | TodoApp.addInitializer(function(){ 13 | ErrorRouting.router = new ErrorRouting.Router({ 14 | controller: TodoApp.Controllers.ErrorController 15 | }); 16 | }); 17 | 18 | return ErrorRouting; 19 | }(); 20 | 21 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/error/error.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.NotAuthorizedView = Marionette.ItemView.extend({ 2 | template: "#template-not-authorized-view" 3 | }) 4 | 5 | TodoApp.Views.NotFoundView = Marionette.ItemView.extend({ 6 | template: "#template-not-found-view" 7 | }); 8 | 9 | TodoApp.Views.ErrorView = Marionette.ItemView.extend({ 10 | template: "#template-error-view" 11 | }); -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/todo/todo.i18n.js: -------------------------------------------------------------------------------- 1 | TodoApp.Translations.resources = { 2 | dev: { 3 | translation: { 4 | 'loginFailed': 'Login failed.', 5 | 'todoAdded': 'Todo entry: __title__ was added.', 6 | 'todoDeleted': 'Todo entry: __title__ was deleted.', 7 | 'todoUpdated': 'Todo entry: __title__ was updated.' 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/todo/todo.models.js: -------------------------------------------------------------------------------- 1 | //Models 2 | TodoApp.Models.FeedbackMessage = Backbone.Model.extend(); 3 | 4 | TodoApp.Models.Todo = Backbone.Model.extend({ 5 | urlRoot: "/api/todo" 6 | }); 7 | 8 | //Collections 9 | TodoApp.Collections.Todos = Backbone.Collection.extend({ 10 | model: TodoApp.Models.Todo, 11 | url: function() { 12 | return "/api/todo"; 13 | } 14 | }) -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/todo/todo.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.TodoRouting = function(){ 2 | var TodoRouting = {}; 3 | 4 | TodoRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "todo/add": "add", 7 | "todo/:id": "view", 8 | "todo/update/:id": "update", 9 | "": "list" 10 | } 11 | }); 12 | 13 | TodoApp.addInitializer(function(){ 14 | TodoRouting.router = new TodoRouting.Router({ 15 | controller: TodoApp.Controllers.TodoController 16 | }); 17 | 18 | TodoApp.vent.trigger("routing:started"); 19 | }); 20 | 21 | return TodoRouting; 22 | }(); 23 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/user/user.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.UserController = { 2 | login: function() { 3 | window.log("Rendering login page"); 4 | var loginView = new TodoApp.Views.LoginView(); 5 | TodoApp.mainRegion.show(loginView); 6 | }, 7 | logout: function() { 8 | window.log("Logging user out") 9 | $.get("/api/logout", function(data) { 10 | window.log("Logout successful. Received data: ", data); 11 | TodoApp.vent.trigger("user:logoutSuccess"); 12 | }) 13 | } 14 | }; -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/user/user.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("user:login", function(){ 2 | Backbone.history.navigate("#/user/login"); 3 | }); 4 | 5 | TodoApp.vent.on("user:loginFailed", function() { 6 | var translatedErrorMessage = i18n.t("loginFailed"); 7 | var errorMessage = new TodoApp.Models.FeedbackMessage({message: translatedErrorMessage}); 8 | var errorMessageView = new TodoApp.Views.ErrorMessageView({model: errorMessage}); 9 | TodoApp.messageRegion.show(errorMessageView); 10 | }); 11 | 12 | TodoApp.vent.on("user:loginSuccess", function() { 13 | var showTodoList = function() { 14 | Backbone.history.navigate("#/"); 15 | TodoApp.showLogoutLink(); 16 | } 17 | 18 | TodoApp.getLoggedInUser(showTodoList); 19 | }); 20 | 21 | TodoApp.vent.on("user:logoutSuccess", function() { 22 | TodoApp.setUserAsAnonymous(); 23 | Backbone.history.navigate("#/"); 24 | }); 25 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/user/user.models.js: -------------------------------------------------------------------------------- 1 | TodoApp.Models.User = Backbone.Model.extend(); 2 | -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/user/user.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.UserRouting = function() { 2 | var UserRouting = {}; 3 | 4 | UserRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "user/login": "login", 7 | "user/logout": "logout" 8 | } 9 | }); 10 | 11 | TodoApp.addInitializer(function(){ 12 | UserRouting.router = new UserRouting.Router({ 13 | controller: TodoApp.Controllers.UserController 14 | }); 15 | 16 | TodoApp.vent.trigger("routing:started"); 17 | }); 18 | 19 | return UserRouting; 20 | }(); -------------------------------------------------------------------------------- /rest-jsonpath/src/main/webapp/static/js/app/user/user.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.LoginView = Marionette.ItemView.extend({ 2 | template: "#template-login-view", 3 | events: { 4 | "click #login-button": "login" 5 | }, 6 | login: function() { 7 | window.log("Log in"); 8 | var user = {}; 9 | user.username = $("#user-username").val(); 10 | user.password = $("#user-password").val(); 11 | $.post("/api/login", user, function(){ 12 | TodoApp.vent.trigger("user:loginSuccess"); 13 | }) 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /rest-jsonpath/src/test/java/net/petrikainulainen/spring/testmvc/common/controller/HomeControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | /** 9 | * @author Petri Kainulainen 10 | */ 11 | public class HomeControllerTest { 12 | 13 | private HomeController controller; 14 | 15 | @Before 16 | public void setUp() { 17 | controller = new HomeController(); 18 | } 19 | 20 | @Test 21 | public void showHomePage() { 22 | String view = controller.showHomePage(); 23 | assertEquals(HomeController.HOME_VIEW, view); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rest-jsonpath/src/test/java/net/petrikainulainen/spring/testmvc/todo/config/UnitTestContext.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | @Configuration 11 | public class UnitTestContext { 12 | 13 | @Bean 14 | public LocalValidatorFactoryBean validator() { 15 | return new LocalValidatorFactoryBean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rest-unittest/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /rest-unittest/README: -------------------------------------------------------------------------------- 1 | This is an example application of my blog post called 2 | 3 | Unit Testing of Spring MVC Controllers: REST API 4 | http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-rest-api/ 5 | 6 | RUNNING THE APPLICATION: 7 | 8 | - Download and install Maven 3 (http://maven.apache.org/download.html#Installation). If you have already installed Maven 3, you can skip this step. 9 | - Go the root directory of project (The one which contains the pom.xml file) 10 | - Run command mvn clean jetty:run 11 | - Start your browser and go to the location: http://localhost:8080 12 | 13 | RUNNING TESTS: 14 | 15 | - You can run unit tests by using this command: mvn test -P dev 16 | - You can run integration tests by using this command: mvn verify -P integration-test 17 | 18 | -------------------------------------------------------------------------------- /rest-unittest/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /rest-unittest/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=false -------------------------------------------------------------------------------- /rest-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /rest-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /rest-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /rest-unittest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /rest-unittest/src/main/java/net/petrikainulainen/spring/testmvc/common/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String HOME_VIEW = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return HOME_VIEW; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rest-unittest/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FieldErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class FieldErrorDTO { 7 | 8 | private String path; 9 | private String message; 10 | 11 | public FieldErrorDTO(String path, String message) { 12 | this.path = path; 13 | this.message = message; 14 | } 15 | 16 | public String getPath() { 17 | return path; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /rest-unittest/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/ValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public class ValidationErrorDTO { 10 | 11 | private List fieldErrors = new ArrayList(); 12 | 13 | public ValidationErrorDTO() { 14 | 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldErrorDTO fieldError = new FieldErrorDTO(path, message); 19 | fieldErrors.add(fieldError); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rest-unittest/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /rest-unittest/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /rest-unittest/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /rest-unittest/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/rest-unittest/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/rest-unittest/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/error/error.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.ErrorController = { 2 | 404: function() { 3 | window.log("Rendering 404 view."); 4 | var notFoundView = new TodoApp.Views.NotFoundView(); 5 | TodoApp.mainRegion.show(notFoundView); 6 | }, 7 | error: function() { 8 | window.log("Rendering error view."); 9 | var errorView = new TodoApp.Views.ErrorView(); 10 | TodoApp.mainRegion.show(errorView); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/error/error.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("error:404", function() { 2 | window.log("Processing 404 event.") 3 | Backbone.history.navigate("#/error/404"); 4 | }); 5 | 6 | TodoApp.vent.on("error:error", function(){ 7 | window.log("Processing error event") 8 | Backbone.history.navigate("#/error/error"); 9 | }); -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/error/error.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.ErrorRouting = function(){ 2 | var ErrorRouting = {}; 3 | 4 | ErrorRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "error/404": "404", 7 | "error/error": "error" 8 | } 9 | }); 10 | 11 | TodoApp.addInitializer(function(){ 12 | ErrorRouting.router = new ErrorRouting.Router({ 13 | controller: TodoApp.Controllers.ErrorController 14 | }); 15 | }); 16 | 17 | return ErrorRouting; 18 | }(); 19 | 20 | -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/error/error.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.NotFoundView = Marionette.ItemView.extend({ 2 | template: "#template-not-found-view" 3 | }); 4 | 5 | TodoApp.Views.ErrorView = Marionette.ItemView.extend({ 6 | template: "#template-error-view" 7 | }); -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/todo/todo.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.TodoController = { 2 | add: function() { 3 | window.log("Rendering add todo view."); 4 | var addTodoView = new TodoApp.Views.AddTodoView(); 5 | TodoApp.mainRegion.show(addTodoView); 6 | }, 7 | view: function(id) { 8 | window.log("Rendering view page for todo entry with id: ", id); 9 | var viewTodoView = new TodoApp.Views.ViewTodoView({id: id}); 10 | TodoApp.mainRegion.show(viewTodoView); 11 | }, 12 | list: function() { 13 | window.log("Rendering todo list view."); 14 | var todoPageView = new TodoApp.Views.TodoListView(); 15 | TodoApp.mainRegion.show(todoPageView); 16 | }, 17 | update: function(id) { 18 | window.log("Rendering update todo view.") 19 | 20 | var updateTodoView = new TodoApp.Views.UpdateTodoView({id: id}); 21 | TodoApp.mainRegion.show(updateTodoView); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/todo/todo.i18n.js: -------------------------------------------------------------------------------- 1 | TodoApp.Translations.resources = { 2 | dev: { 3 | translation: { 4 | 'todoAdded': 'Todo entry: __title__ was added.', 5 | 'todoDeleted': 'Todo entry: __title__ was deleted.', 6 | 'todoUpdated': 'Todo entry: __title__ was updated.' 7 | } 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/todo/todo.models.js: -------------------------------------------------------------------------------- 1 | //Models 2 | TodoApp.Models.FeedbackMessage = Backbone.Model.extend(); 3 | 4 | TodoApp.Models.Todo = Backbone.Model.extend({ 5 | urlRoot: "/api/todo" 6 | }); 7 | 8 | //Collections 9 | TodoApp.Collections.Todos = Backbone.Collection.extend({ 10 | model: TodoApp.Models.Todo, 11 | url: function() { 12 | return "/api/todo"; 13 | } 14 | }) -------------------------------------------------------------------------------- /rest-unittest/src/main/webapp/static/js/app/todo/todo.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.TodoRouting = function(){ 2 | var TodoRouting = {}; 3 | 4 | TodoRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "todo/add": "add", 7 | "todo/:id": "view", 8 | "todo/update/:id": "update", 9 | "": "list" 10 | } 11 | }); 12 | 13 | TodoApp.addInitializer(function(){ 14 | TodoRouting.router = new TodoRouting.Router({ 15 | controller: TodoApp.Controllers.TodoController 16 | }); 17 | 18 | TodoApp.vent.trigger("routing:started"); 19 | }); 20 | 21 | return TodoRouting; 22 | }(); 23 | -------------------------------------------------------------------------------- /rest-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/dto/TodoDTOBuilder.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoDTOBuilder { 7 | 8 | private TodoDTO dto; 9 | 10 | public TodoDTOBuilder() { 11 | dto = new TodoDTO(); 12 | } 13 | 14 | public TodoDTOBuilder id(Long id) { 15 | dto.setId(id); 16 | return this; 17 | } 18 | 19 | public TodoDTOBuilder description(String description) { 20 | dto.setDescription(description); 21 | return this; 22 | } 23 | 24 | public TodoDTOBuilder title(String title) { 25 | dto.setTitle(title); 26 | return this; 27 | } 28 | 29 | public TodoDTO build() { 30 | return dto; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /rest-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/dto/ValidationErrorDTOTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static junit.framework.Assert.assertEquals; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | public class ValidationErrorDTOTest { 13 | 14 | private static final String FIELD_PATH = "foo"; 15 | private static final String ERROR_MESSAGE = "bar"; 16 | 17 | @Test 18 | public void addFieldError() { 19 | ValidationErrorDTO dto = new ValidationErrorDTO(); 20 | 21 | dto.addFieldError(FIELD_PATH, ERROR_MESSAGE); 22 | 23 | List fieldErrors = dto.getFieldErrors(); 24 | 25 | assertEquals(1, fieldErrors.size()); 26 | 27 | FieldErrorDTO fieldError = fieldErrors.get(0); 28 | assertEquals(FIELD_PATH, fieldError.getPath()); 29 | assertEquals(ERROR_MESSAGE, fieldError.getMessage()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /rest-unittest/src/test/java/net/petrikainulainen/spring/testmvc/todo/model/TodoBuilder.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.model; 2 | 3 | import org.springframework.test.util.ReflectionTestUtils; 4 | 5 | /** 6 | * @author Petri Kainulainen 7 | */ 8 | public class TodoBuilder { 9 | 10 | private Todo model; 11 | 12 | public TodoBuilder() { 13 | model = new Todo(); 14 | } 15 | 16 | public TodoBuilder id(Long id) { 17 | ReflectionTestUtils.setField(model, "id", id); 18 | return this; 19 | } 20 | 21 | public TodoBuilder description(String description) { 22 | model.update(description, model.getTitle()); 23 | return this; 24 | } 25 | 26 | public TodoBuilder title(String title) { 27 | model.update(model.getDescription(), title); 28 | return this; 29 | } 30 | 31 | public Todo build() { 32 | return model; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /rest-unittest/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=OFF -------------------------------------------------------------------------------- /rest-unittest/src/test/resources/testContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /rest/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /rest/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /rest/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true -------------------------------------------------------------------------------- /rest/src/integration-test/java/net/petrikainulainen/spring/testmvc/IntegrationTestUtil.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc; 2 | 3 | import org.codehaus.jackson.map.ObjectMapper; 4 | import org.codehaus.jackson.map.annotate.JsonSerialize; 5 | import org.springframework.http.MediaType; 6 | 7 | import java.io.IOException; 8 | import java.nio.charset.Charset; 9 | import java.util.Iterator; 10 | import java.util.Map; 11 | import java.util.Set; 12 | 13 | /** 14 | * @author Petri Kainulainen 15 | */ 16 | public class IntegrationTestUtil { 17 | 18 | public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); 19 | 20 | public static byte[] convertObjectToJsonBytes(Object object) throws IOException { 21 | ObjectMapper mapper = new ObjectMapper(); 22 | mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); 23 | return mapper.writeValueAsBytes(object); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rest/src/integration-test/java/org/springframework/test/web/server/samples/context/WebContextLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.test.web.server.samples.context; 17 | 18 | public class WebContextLoader extends GenericWebContextLoader { 19 | 20 | public WebContextLoader() { 21 | super("src/test/resources/META-INF/web-resources", false); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /rest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /rest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /rest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /rest/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/common/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String HOME_VIEW = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return HOME_VIEW; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/common/util/LocaleContextHolderWrapper.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.i18n.LocaleContextHolder; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.Locale; 9 | 10 | /** 11 | * @author Petri Kainulainen 12 | */ 13 | @Component 14 | public class LocaleContextHolderWrapper { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(LocaleContextHolderWrapper.class); 17 | 18 | public Locale getCurrentLocale() { 19 | LOGGER.debug("Getting current locale"); 20 | return LocaleContextHolder.getLocale(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FieldValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class FieldValidationErrorDTO { 7 | 8 | private String path; 9 | private String message; 10 | 11 | public FieldValidationErrorDTO(String path, String message) { 12 | this.path = path; 13 | this.message = message; 14 | } 15 | 16 | public String getPath() { 17 | return path; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FormValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public class FormValidationErrorDTO { 10 | 11 | private List fieldErrors = new ArrayList(); 12 | 13 | public FormValidationErrorDTO() { 14 | 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldValidationErrorDTO fieldError = new FieldValidationErrorDTO(path, message); 19 | fieldErrors.add(fieldError); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/FormValidationError.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | import org.springframework.validation.FieldError; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | public class FormValidationError extends Exception { 11 | 12 | private List fieldErrors; 13 | 14 | public FormValidationError(List fieldErrors) { 15 | this.fieldErrors = fieldErrors; 16 | } 17 | 18 | public List getFieldErrors() { 19 | return fieldErrors; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /rest/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /rest/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /rest/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /rest/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /rest/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/rest/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /rest/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/rest/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/error/error.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.ErrorController = { 2 | 404: function() { 3 | window.log("Rendering 404 view."); 4 | var notFoundView = new TodoApp.Views.NotFoundView(); 5 | TodoApp.mainRegion.show(notFoundView); 6 | }, 7 | error: function() { 8 | window.log("Rendering error view."); 9 | var errorView = new TodoApp.Views.ErrorView(); 10 | TodoApp.mainRegion.show(errorView); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/error/error.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("error:404", function() { 2 | window.log("Processing 404 event.") 3 | Backbone.history.navigate("#/error/404"); 4 | }); 5 | 6 | TodoApp.vent.on("error:error", function(){ 7 | window.log("Processing error event") 8 | Backbone.history.navigate("#/error/error"); 9 | }); -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/error/error.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.ErrorRouting = function(){ 2 | var ErrorRouting = {}; 3 | 4 | ErrorRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "error/404": "404", 7 | "error/error": "error" 8 | } 9 | }); 10 | 11 | TodoApp.addInitializer(function(){ 12 | ErrorRouting.router = new ErrorRouting.Router({ 13 | controller: TodoApp.Controllers.ErrorController 14 | }); 15 | }); 16 | 17 | return ErrorRouting; 18 | }(); 19 | 20 | -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/error/error.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.NotFoundView = Marionette.ItemView.extend({ 2 | template: "#template-not-found-view" 3 | }); 4 | 5 | TodoApp.Views.ErrorView = Marionette.ItemView.extend({ 6 | template: "#template-error-view" 7 | }); -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/todo/todo.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.TodoController = { 2 | add: function() { 3 | window.log("Rendering add todo view."); 4 | var addTodoView = new TodoApp.Views.AddTodoView(); 5 | TodoApp.mainRegion.show(addTodoView); 6 | }, 7 | view: function(id) { 8 | window.log("Rendering view page for todo entry with id: ", id); 9 | var viewTodoView = new TodoApp.Views.ViewTodoView({id: id}); 10 | TodoApp.mainRegion.show(viewTodoView); 11 | }, 12 | list: function() { 13 | window.log("Rendering todo list view."); 14 | var todoPageView = new TodoApp.Views.TodoListView(); 15 | TodoApp.mainRegion.show(todoPageView); 16 | }, 17 | update: function(id) { 18 | window.log("Rendering update todo view.") 19 | 20 | var updateTodoView = new TodoApp.Views.UpdateTodoView({id: id}); 21 | TodoApp.mainRegion.show(updateTodoView); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/todo/todo.i18n.js: -------------------------------------------------------------------------------- 1 | TodoApp.Translations.resources = { 2 | dev: { 3 | translation: { 4 | 'todoAdded': 'Todo entry: __title__ was added.', 5 | 'todoDeleted': 'Todo entry: __title__ was deleted.', 6 | 'todoUpdated': 'Todo entry: __title__ was updated.' 7 | } 8 | } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/todo/todo.models.js: -------------------------------------------------------------------------------- 1 | //Models 2 | TodoApp.Models.FeedbackMessage = Backbone.Model.extend(); 3 | 4 | TodoApp.Models.Todo = Backbone.Model.extend({ 5 | urlRoot: "/api/todo" 6 | }); 7 | 8 | //Collections 9 | TodoApp.Collections.Todos = Backbone.Collection.extend({ 10 | model: TodoApp.Models.Todo, 11 | url: function() { 12 | return "/api/todo"; 13 | } 14 | }) -------------------------------------------------------------------------------- /rest/src/main/webapp/static/js/app/todo/todo.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.TodoRouting = function(){ 2 | var TodoRouting = {}; 3 | 4 | TodoRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "todo/add": "add", 7 | "todo/:id": "view", 8 | "todo/update/:id": "update", 9 | "": "list" 10 | } 11 | }); 12 | 13 | TodoApp.addInitializer(function(){ 14 | TodoRouting.router = new TodoRouting.Router({ 15 | controller: TodoApp.Controllers.TodoController 16 | }); 17 | 18 | TodoApp.vent.trigger("routing:started"); 19 | }); 20 | 21 | return TodoRouting; 22 | }(); 23 | -------------------------------------------------------------------------------- /rest/src/test/java/net/petrikainulainen/spring/testmvc/todo/config/UnitTestContext.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | @Configuration 11 | public class UnitTestContext { 12 | 13 | @Bean 14 | public LocalValidatorFactoryBean validator() { 15 | return new LocalValidatorFactoryBean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rest/src/test/java/net/petrikainulainen/spring/testmvc/todo/dto/FormValidationErrorDTOTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.List; 6 | 7 | import static junit.framework.Assert.assertEquals; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | public class FormValidationErrorDTOTest { 13 | 14 | private static final String FIELD_PATH = "foo"; 15 | private static final String ERROR_MESSAGE = "bar"; 16 | 17 | @Test 18 | public void addFieldError() { 19 | FormValidationErrorDTO dto = new FormValidationErrorDTO(); 20 | 21 | dto.addFieldError(FIELD_PATH, ERROR_MESSAGE); 22 | 23 | List fieldErrors = dto.getFieldErrors(); 24 | 25 | assertEquals(1, fieldErrors.size()); 26 | 27 | FieldValidationErrorDTO fieldError = fieldErrors.get(0); 28 | assertEquals(FIELD_PATH, fieldError.getPath()); 29 | assertEquals(ERROR_MESSAGE, fieldError.getMessage()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /security-annotations/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /security-annotations/README: -------------------------------------------------------------------------------- 1 | This is an example application of my blog entry that describe the integration testing of Spring MVC application that 2 | uses annotation based security: 3 | 4 | Integration Testing of Spring MVC Applications: Security: 5 | http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-security/ 6 | 7 | RUNNING THE APPLICATION: 8 | 9 | - Download and install Maven 3 (http://maven.apache.org/download.html#Installation). If you have already installed Maven 3, you can skip this step. 10 | - Go the root directory of project (The one which contains the pom.xml file) 11 | - Run command mvn clean jetty:run 12 | - Start your browser and go to the location: http://localhost:8080 13 | - Use credentials (user/password) to log in. 14 | 15 | RUNNING TESTS: 16 | 17 | - You can run unit tests by using this command: mvn test -P dev 18 | - You can run integration tests by using this command: mvn verify -P integration-test 19 | 20 | -------------------------------------------------------------------------------- /security-annotations/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /security-annotations/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true -------------------------------------------------------------------------------- /security-annotations/src/integration-test/java/org/springframework/test/web/server/samples/context/WebContextLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.springframework.test.web.server.samples.context; 17 | 18 | public class WebContextLoader extends GenericWebContextLoader { 19 | 20 | public WebContextLoader() { 21 | super("src/test/resources/META-INF/web-resources", false); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /security-annotations/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /security-annotations/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /security-annotations/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /security-annotations/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/common/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String HOME_VIEW = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return HOME_VIEW; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/common/util/LocaleContextHolderWrapper.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.i18n.LocaleContextHolder; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.Locale; 9 | 10 | /** 11 | * @author Petri Kainulainen 12 | */ 13 | @Component 14 | public class LocaleContextHolderWrapper { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(LocaleContextHolderWrapper.class); 17 | 18 | public Locale getCurrentLocale() { 19 | LOGGER.debug("Getting current locale"); 20 | return LocaleContextHolder.getLocale(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | import org.springframework.security.web.AuthenticationEntryPoint; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 15 | @Override 16 | public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException { 17 | response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized" ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestLogoutSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.Authentication; 4 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestLogoutSuccessHandler implements LogoutSuccessHandler { 15 | @Override 16 | public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 17 | response.setStatus(HttpServletResponse.SC_OK); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FieldValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class FieldValidationErrorDTO { 7 | 8 | private String path; 9 | private String message; 10 | 11 | public FieldValidationErrorDTO(String path, String message) { 12 | this.path = path; 13 | this.message = message; 14 | } 15 | 16 | public String getPath() { 17 | return path; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FormValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public class FormValidationErrorDTO { 10 | 11 | private List fieldErrors = new ArrayList(); 12 | 13 | public FormValidationErrorDTO() { 14 | 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldValidationErrorDTO fieldError = new FieldValidationErrorDTO(path, message); 19 | fieldErrors.add(fieldError); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/FormValidationError.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | import org.springframework.validation.FieldError; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | public class FormValidationError extends Exception { 11 | 12 | private List fieldErrors; 13 | 14 | public FormValidationError(List fieldErrors) { 15 | this.fieldErrors = fieldErrors; 16 | } 17 | 18 | public List getFieldErrors() { 19 | return fieldErrors; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/SecurityRole.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public enum SecurityRole { 7 | ROLE_USER 8 | } 9 | -------------------------------------------------------------------------------- /security-annotations/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | import org.apache.commons.lang.builder.ToStringBuilder; 4 | 5 | /** 6 | * @author Petri Kainulainen 7 | */ 8 | public class UserDTO { 9 | 10 | private String username; 11 | 12 | private SecurityRole role; 13 | 14 | public UserDTO(String username, SecurityRole role) { 15 | this.username = username; 16 | this.role = role; 17 | } 18 | 19 | public String getUsername() { 20 | return username; 21 | } 22 | 23 | public SecurityRole getRole() { 24 | return role; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return ToStringBuilder.reflectionToString(this); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /security-annotations/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /security-annotations/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/security-annotations/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/security-annotations/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/error/error.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.ErrorController = { 2 | 404: function() { 3 | window.log("Rendering 404 view."); 4 | var notFoundView = new TodoApp.Views.NotFoundView(); 5 | TodoApp.mainRegion.show(notFoundView); 6 | }, 7 | notAuthorized: function() { 8 | window.log("Rendering not authorized view"); 9 | var notAuthorizedView = new TodoApp.Views.NotAuthorizedView(); 10 | TodoApp.mainRegion.show(notAuthorizedView); 11 | }, 12 | error: function() { 13 | window.log("Rendering error view."); 14 | var errorView = new TodoApp.Views.ErrorView(); 15 | TodoApp.mainRegion.show(errorView); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/error/error.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("error:404", function() { 2 | window.log("Processing 404 event.") 3 | Backbone.history.navigate("#/error/404"); 4 | }); 5 | 6 | TodoApp.vent.on("error:notAuthorized", function() { 7 | window.log("Processing not authorized event"); 8 | Backbone.history.navigate("#/error/notAuthorized") 9 | }); 10 | 11 | TodoApp.vent.on("error:error", function(){ 12 | window.log("Processing error event") 13 | Backbone.history.navigate("#/error/error"); 14 | }); -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/error/error.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.ErrorRouting = function(){ 2 | var ErrorRouting = {}; 3 | 4 | ErrorRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "error/404": "404", 7 | "error/notAuthorized": "notAuthorized", 8 | "error/error": "error" 9 | } 10 | }); 11 | 12 | TodoApp.addInitializer(function(){ 13 | ErrorRouting.router = new ErrorRouting.Router({ 14 | controller: TodoApp.Controllers.ErrorController 15 | }); 16 | }); 17 | 18 | return ErrorRouting; 19 | }(); 20 | 21 | -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/error/error.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.NotAuthorizedView = Marionette.ItemView.extend({ 2 | template: "#template-not-authorized-view" 3 | }) 4 | 5 | TodoApp.Views.NotFoundView = Marionette.ItemView.extend({ 6 | template: "#template-not-found-view" 7 | }); 8 | 9 | TodoApp.Views.ErrorView = Marionette.ItemView.extend({ 10 | template: "#template-error-view" 11 | }); -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/todo/todo.i18n.js: -------------------------------------------------------------------------------- 1 | TodoApp.Translations.resources = { 2 | dev: { 3 | translation: { 4 | 'loginFailed': 'Login failed.', 5 | 'todoAdded': 'Todo entry: __title__ was added.', 6 | 'todoDeleted': 'Todo entry: __title__ was deleted.', 7 | 'todoUpdated': 'Todo entry: __title__ was updated.' 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/todo/todo.models.js: -------------------------------------------------------------------------------- 1 | //Models 2 | TodoApp.Models.FeedbackMessage = Backbone.Model.extend(); 3 | 4 | TodoApp.Models.Todo = Backbone.Model.extend({ 5 | urlRoot: "/api/todo" 6 | }); 7 | 8 | //Collections 9 | TodoApp.Collections.Todos = Backbone.Collection.extend({ 10 | model: TodoApp.Models.Todo, 11 | url: function() { 12 | return "/api/todo"; 13 | } 14 | }) -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/todo/todo.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.TodoRouting = function(){ 2 | var TodoRouting = {}; 3 | 4 | TodoRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "todo/add": "add", 7 | "todo/:id": "view", 8 | "todo/update/:id": "update", 9 | "": "list" 10 | } 11 | }); 12 | 13 | TodoApp.addInitializer(function(){ 14 | TodoRouting.router = new TodoRouting.Router({ 15 | controller: TodoApp.Controllers.TodoController 16 | }); 17 | 18 | TodoApp.vent.trigger("routing:started"); 19 | }); 20 | 21 | return TodoRouting; 22 | }(); 23 | -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/user/user.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.UserController = { 2 | login: function() { 3 | window.log("Rendering login page"); 4 | var loginView = new TodoApp.Views.LoginView(); 5 | TodoApp.mainRegion.show(loginView); 6 | }, 7 | logout: function() { 8 | window.log("Logging user out") 9 | $.get("/api/logout", function(data) { 10 | window.log("Logout successful. Received data: ", data); 11 | TodoApp.vent.trigger("user:logoutSuccess"); 12 | }) 13 | } 14 | }; -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/user/user.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("user:login", function(){ 2 | Backbone.history.navigate("#/user/login"); 3 | }); 4 | 5 | TodoApp.vent.on("user:loginFailed", function() { 6 | var translatedErrorMessage = i18n.t("loginFailed"); 7 | var errorMessage = new TodoApp.Models.FeedbackMessage({message: translatedErrorMessage}); 8 | var errorMessageView = new TodoApp.Views.ErrorMessageView({model: errorMessage}); 9 | TodoApp.messageRegion.show(errorMessageView); 10 | }); 11 | 12 | TodoApp.vent.on("user:loginSuccess", function() { 13 | var showTodoList = function() { 14 | Backbone.history.navigate("#/"); 15 | TodoApp.showLogoutLink(); 16 | } 17 | 18 | TodoApp.getLoggedInUser(showTodoList); 19 | }); 20 | 21 | TodoApp.vent.on("user:logoutSuccess", function() { 22 | TodoApp.setUserAsAnonymous(); 23 | Backbone.history.navigate("#/"); 24 | }); 25 | -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/user/user.models.js: -------------------------------------------------------------------------------- 1 | TodoApp.Models.User = Backbone.Model.extend(); 2 | -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/user/user.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.UserRouting = function() { 2 | var UserRouting = {}; 3 | 4 | UserRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "user/login": "login", 7 | "user/logout": "logout" 8 | } 9 | }); 10 | 11 | TodoApp.addInitializer(function(){ 12 | UserRouting.router = new UserRouting.Router({ 13 | controller: TodoApp.Controllers.UserController 14 | }); 15 | 16 | TodoApp.vent.trigger("routing:started"); 17 | }); 18 | 19 | return UserRouting; 20 | }(); -------------------------------------------------------------------------------- /security-annotations/src/main/webapp/static/js/app/user/user.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.LoginView = Marionette.ItemView.extend({ 2 | template: "#template-login-view", 3 | events: { 4 | "click #login-button": "login" 5 | }, 6 | login: function() { 7 | window.log("Log in"); 8 | var user = {}; 9 | user.username = $("#user-username").val(); 10 | user.password = $("#user-password").val(); 11 | $.post("/api/login", user, function(){ 12 | TodoApp.vent.trigger("user:loginSuccess"); 13 | }) 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /security-annotations/src/test/java/net/petrikainulainen/spring/testmvc/common/controller/HomeControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | /** 9 | * @author Petri Kainulainen 10 | */ 11 | public class HomeControllerTest { 12 | 13 | private HomeController controller; 14 | 15 | @Before 16 | public void setUp() { 17 | controller = new HomeController(); 18 | } 19 | 20 | @Test 21 | public void showHomePage() { 22 | String view = controller.showHomePage(); 23 | assertEquals(HomeController.HOME_VIEW, view); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /security-annotations/src/test/java/net/petrikainulainen/spring/testmvc/todo/config/UnitTestContext.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | @Configuration 11 | public class UnitTestContext { 12 | 13 | @Bean 14 | public LocalValidatorFactoryBean validator() { 15 | return new LocalValidatorFactoryBean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /security-url-based/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /security-url-based/README: -------------------------------------------------------------------------------- 1 | This is an example application of my blog entry that describe the integration testing of Spring MVC application that 2 | uses url based security: 3 | 4 | Integration Testing of Spring MVC Applications: Security: 5 | http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-security/ 6 | 7 | RUNNING THE APPLICATION: 8 | 9 | - Download and install Maven 3 (http://maven.apache.org/download.html#Installation). If you have already installed Maven 3, you can skip this step. 10 | - Go the root directory of project (The one which contains the pom.xml file) 11 | - Run command mvn clean jetty:run 12 | - Start your browser and go to the location: http://localhost:8080 13 | - Use credentials (user/password) to log in. 14 | 15 | RUNNING TESTS: 16 | 17 | - You can run unit tests by using this command: mvn test -P dev 18 | - You can run integration tests by using this command: mvn verify -P integration-test 19 | 20 | -------------------------------------------------------------------------------- /security-url-based/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /security-url-based/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true -------------------------------------------------------------------------------- /security-url-based/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /security-url-based/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /security-url-based/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /security-url-based/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/common/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String HOME_VIEW = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return HOME_VIEW; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/common/util/LocaleContextHolderWrapper.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.i18n.LocaleContextHolder; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.Locale; 9 | 10 | /** 11 | * @author Petri Kainulainen 12 | */ 13 | @Component 14 | public class LocaleContextHolderWrapper { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(LocaleContextHolderWrapper.class); 17 | 18 | public Locale getCurrentLocale() { 19 | LOGGER.debug("Getting current locale"); 20 | return LocaleContextHolder.getLocale(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | import org.springframework.security.web.AuthenticationEntryPoint; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 15 | @Override 16 | public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException { 17 | response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized" ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestLogoutSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.Authentication; 4 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestLogoutSuccessHandler implements LogoutSuccessHandler { 15 | @Override 16 | public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 17 | response.setStatus(HttpServletResponse.SC_OK); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FieldValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class FieldValidationErrorDTO { 7 | 8 | private String path; 9 | private String message; 10 | 11 | public FieldValidationErrorDTO(String path, String message) { 12 | this.path = path; 13 | this.message = message; 14 | } 15 | 16 | public String getPath() { 17 | return path; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FormValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public class FormValidationErrorDTO { 10 | 11 | private List fieldErrors = new ArrayList(); 12 | 13 | public FormValidationErrorDTO() { 14 | 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldValidationErrorDTO fieldError = new FieldValidationErrorDTO(path, message); 19 | fieldErrors.add(fieldError); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/FormValidationError.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | import org.springframework.validation.FieldError; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | public class FormValidationError extends Exception { 11 | 12 | private List fieldErrors; 13 | 14 | public FormValidationError(List fieldErrors) { 15 | this.fieldErrors = fieldErrors; 16 | } 17 | 18 | public List getFieldErrors() { 19 | return fieldErrors; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/SecurityRole.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public enum SecurityRole { 7 | ROLE_USER 8 | } 9 | -------------------------------------------------------------------------------- /security-url-based/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | import org.apache.commons.lang.builder.ToStringBuilder; 4 | 5 | /** 6 | * @author Petri Kainulainen 7 | */ 8 | public class UserDTO { 9 | 10 | private String username; 11 | 12 | private SecurityRole role; 13 | 14 | public UserDTO(String username, SecurityRole role) { 15 | this.username = username; 16 | this.role = role; 17 | } 18 | 19 | public String getUsername() { 20 | return username; 21 | } 22 | 23 | public SecurityRole getRole() { 24 | return role; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return ToStringBuilder.reflectionToString(this); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /security-url-based/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /security-url-based/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/security-url-based/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/security-url-based/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/error/error.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.ErrorController = { 2 | 404: function() { 3 | window.log("Rendering 404 view."); 4 | var notFoundView = new TodoApp.Views.NotFoundView(); 5 | TodoApp.mainRegion.show(notFoundView); 6 | }, 7 | notAuthorized: function() { 8 | window.log("Rendering not authorized view"); 9 | var notAuthorizedView = new TodoApp.Views.NotAuthorizedView(); 10 | TodoApp.mainRegion.show(notAuthorizedView); 11 | }, 12 | error: function() { 13 | window.log("Rendering error view."); 14 | var errorView = new TodoApp.Views.ErrorView(); 15 | TodoApp.mainRegion.show(errorView); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/error/error.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("error:404", function() { 2 | window.log("Processing 404 event.") 3 | Backbone.history.navigate("#/error/404"); 4 | }); 5 | 6 | TodoApp.vent.on("error:notAuthorized", function() { 7 | window.log("Processing not authorized event"); 8 | Backbone.history.navigate("#/error/notAuthorized") 9 | }); 10 | 11 | TodoApp.vent.on("error:error", function(){ 12 | window.log("Processing error event") 13 | Backbone.history.navigate("#/error/error"); 14 | }); -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/error/error.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.ErrorRouting = function(){ 2 | var ErrorRouting = {}; 3 | 4 | ErrorRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "error/404": "404", 7 | "error/notAuthorized": "notAuthorized", 8 | "error/error": "error" 9 | } 10 | }); 11 | 12 | TodoApp.addInitializer(function(){ 13 | ErrorRouting.router = new ErrorRouting.Router({ 14 | controller: TodoApp.Controllers.ErrorController 15 | }); 16 | }); 17 | 18 | return ErrorRouting; 19 | }(); 20 | 21 | -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/error/error.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.NotAuthorizedView = Marionette.ItemView.extend({ 2 | template: "#template-not-authorized-view" 3 | }) 4 | 5 | TodoApp.Views.NotFoundView = Marionette.ItemView.extend({ 6 | template: "#template-not-found-view" 7 | }); 8 | 9 | TodoApp.Views.ErrorView = Marionette.ItemView.extend({ 10 | template: "#template-error-view" 11 | }); -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/todo/todo.i18n.js: -------------------------------------------------------------------------------- 1 | TodoApp.Translations.resources = { 2 | dev: { 3 | translation: { 4 | 'loginFailed': 'Login failed.', 5 | 'todoAdded': 'Todo entry: __title__ was added.', 6 | 'todoDeleted': 'Todo entry: __title__ was deleted.', 7 | 'todoUpdated': 'Todo entry: __title__ was updated.' 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/todo/todo.models.js: -------------------------------------------------------------------------------- 1 | //Models 2 | TodoApp.Models.FeedbackMessage = Backbone.Model.extend(); 3 | 4 | TodoApp.Models.Todo = Backbone.Model.extend({ 5 | urlRoot: "/api/todo" 6 | }); 7 | 8 | //Collections 9 | TodoApp.Collections.Todos = Backbone.Collection.extend({ 10 | model: TodoApp.Models.Todo, 11 | url: function() { 12 | return "/api/todo"; 13 | } 14 | }) -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/todo/todo.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.TodoRouting = function(){ 2 | var TodoRouting = {}; 3 | 4 | TodoRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "todo/add": "add", 7 | "todo/:id": "view", 8 | "todo/update/:id": "update", 9 | "": "list" 10 | } 11 | }); 12 | 13 | TodoApp.addInitializer(function(){ 14 | TodoRouting.router = new TodoRouting.Router({ 15 | controller: TodoApp.Controllers.TodoController 16 | }); 17 | 18 | TodoApp.vent.trigger("routing:started"); 19 | }); 20 | 21 | return TodoRouting; 22 | }(); 23 | -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/user/user.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.UserController = { 2 | login: function() { 3 | window.log("Rendering login page"); 4 | var loginView = new TodoApp.Views.LoginView(); 5 | TodoApp.mainRegion.show(loginView); 6 | }, 7 | logout: function() { 8 | window.log("Logging user out") 9 | $.get("/api/logout", function(data) { 10 | window.log("Logout successful. Received data: ", data); 11 | TodoApp.vent.trigger("user:logoutSuccess"); 12 | }) 13 | } 14 | }; -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/user/user.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("user:login", function(){ 2 | Backbone.history.navigate("#/user/login"); 3 | }); 4 | 5 | TodoApp.vent.on("user:loginFailed", function() { 6 | var translatedErrorMessage = i18n.t("loginFailed"); 7 | var errorMessage = new TodoApp.Models.FeedbackMessage({message: translatedErrorMessage}); 8 | var errorMessageView = new TodoApp.Views.ErrorMessageView({model: errorMessage}); 9 | TodoApp.messageRegion.show(errorMessageView); 10 | }); 11 | 12 | TodoApp.vent.on("user:loginSuccess", function() { 13 | var showTodoList = function() { 14 | Backbone.history.navigate("#/"); 15 | TodoApp.showLogoutLink(); 16 | } 17 | 18 | TodoApp.getLoggedInUser(showTodoList); 19 | }); 20 | 21 | 22 | TodoApp.vent.on("user:logoutSuccess", function() { 23 | TodoApp.setUserAsAnonymous(); 24 | Backbone.history.navigate("#/"); 25 | }); -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/user/user.models.js: -------------------------------------------------------------------------------- 1 | TodoApp.Models.User = Backbone.Model.extend(); 2 | -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/user/user.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.UserRouting = function() { 2 | var UserRouting = {}; 3 | 4 | UserRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "user/login": "login", 7 | "user/logout": "logout" 8 | } 9 | }); 10 | 11 | TodoApp.addInitializer(function(){ 12 | UserRouting.router = new UserRouting.Router({ 13 | controller: TodoApp.Controllers.UserController 14 | }); 15 | 16 | TodoApp.vent.trigger("routing:started"); 17 | }); 18 | 19 | return UserRouting; 20 | }(); -------------------------------------------------------------------------------- /security-url-based/src/main/webapp/static/js/app/user/user.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.LoginView = Marionette.ItemView.extend({ 2 | template: "#template-login-view", 3 | events: { 4 | "click #login-button": "login" 5 | }, 6 | login: function() { 7 | window.log("Log in"); 8 | var user = {}; 9 | user.username = $("#user-username").val(); 10 | user.password = $("#user-password").val(); 11 | $.post("/api/login", user, function(){ 12 | TodoApp.vent.trigger("user:loginSuccess"); 13 | }) 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /security-url-based/src/test/java/net/petrikainulainen/spring/testmvc/common/controller/HomeControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | /** 9 | * @author Petri Kainulainen 10 | */ 11 | public class HomeControllerTest { 12 | 13 | private HomeController controller; 14 | 15 | @Before 16 | public void setUp() { 17 | controller = new HomeController(); 18 | } 19 | 20 | @Test 21 | public void showHomePage() { 22 | String view = controller.showHomePage(); 23 | assertEquals(HomeController.HOME_VIEW, view); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /security-url-based/src/test/java/net/petrikainulainen/spring/testmvc/todo/config/UnitTestContext.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | @Configuration 11 | public class UnitTestContext { 12 | 13 | @Bean 14 | public LocalValidatorFactoryBean validator() { 15 | return new LocalValidatorFactoryBean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-32/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Petri Kainulainen 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. -------------------------------------------------------------------------------- /spring-32/README: -------------------------------------------------------------------------------- 1 | This is an example application of my blog entry that describe how we can migrate our integration tests 2 | from Spring 3.1 to Spring 3.2. 3 | 4 | Integration Testing of Spring MVC Applications: Migrating to Spring 3.2: 5 | http://www.petrikainulainen.net/programming/spring-framework/integration-testing-of-spring-mvc-applications-migrating-to-spring-3-2/ 6 | 7 | RUNNING THE APPLICATION: 8 | 9 | - Download and install Maven 3 (http://maven.apache.org/download.html#Installation). If you have already installed Maven 3, you can skip this step. 10 | - Go the root directory of project (The one which contains the pom.xml file) 11 | - Run command mvn clean jetty:run 12 | - Start your browser and go to the location: http://localhost:8080 13 | - Use credentials (user/password) to log in. 14 | 15 | RUNNING TESTS: 16 | 17 | - You can run unit tests by using this command: mvn test -P dev 18 | - You can run integration tests by using this command: mvn verify -P integration-test 19 | 20 | -------------------------------------------------------------------------------- /spring-32/profiles/dev/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true 13 | 14 | -------------------------------------------------------------------------------- /spring-32/profiles/integration-test/config.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=org.h2.Driver 3 | db.url=jdbc:h2:mem:datajpa 4 | db.username=sa 5 | db.password= 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=org.hibernate.dialect.H2Dialect 9 | hibernate.format_sql=true 10 | hibernate.hbm2ddl.auto=create-drop 11 | hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 12 | hibernate.show_sql=true -------------------------------------------------------------------------------- /spring-32/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-add-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /spring-32/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-delete-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /spring-32/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData-update-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-32/src/integration-test/resources/net/petrikainulainen/spring/testmvc/todo/controller/toDoData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/common/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | 9 | /** 10 | * @author Petri Kainulainen 11 | */ 12 | @Controller 13 | public class HomeController { 14 | 15 | private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); 16 | 17 | protected static final String HOME_VIEW = "index"; 18 | 19 | @RequestMapping(value = "/", method = RequestMethod.GET) 20 | public String showHomePage() { 21 | LOGGER.debug("Rendering home page."); 22 | return HOME_VIEW; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/common/util/LocaleContextHolderWrapper.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.i18n.LocaleContextHolder; 6 | import org.springframework.stereotype.Component; 7 | 8 | import java.util.Locale; 9 | 10 | /** 11 | * @author Petri Kainulainen 12 | */ 13 | @Component 14 | public class LocaleContextHolderWrapper { 15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(LocaleContextHolderWrapper.class); 17 | 18 | public Locale getCurrentLocale() { 19 | LOGGER.debug("Getting current locale"); 20 | return LocaleContextHolder.getLocale(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | import org.springframework.security.web.AuthenticationEntryPoint; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 15 | @Override 16 | public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException { 17 | response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized" ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/security/authentication/RestLogoutSuccessHandler.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.security.authentication; 2 | 3 | import org.springframework.security.core.Authentication; 4 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | import java.io.IOException; 10 | 11 | /** 12 | * @author Petri Kainulainen 13 | */ 14 | public class RestLogoutSuccessHandler implements LogoutSuccessHandler { 15 | @Override 16 | public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 17 | response.setStatus(HttpServletResponse.SC_OK); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FieldValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class FieldValidationErrorDTO { 7 | 8 | private String path; 9 | private String message; 10 | 11 | public FieldValidationErrorDTO(String path, String message) { 12 | this.path = path; 13 | this.message = message; 14 | } 15 | 16 | public String getPath() { 17 | return path; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/todo/dto/FormValidationErrorDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.dto; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public class FormValidationErrorDTO { 10 | 11 | private List fieldErrors = new ArrayList(); 12 | 13 | public FormValidationErrorDTO() { 14 | 15 | } 16 | 17 | public void addFieldError(String path, String message) { 18 | FieldValidationErrorDTO fieldError = new FieldValidationErrorDTO(path, message); 19 | fieldErrors.add(fieldError); 20 | } 21 | 22 | public List getFieldErrors() { 23 | return fieldErrors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/FormValidationError.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | import org.springframework.validation.FieldError; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | public class FormValidationError extends Exception { 11 | 12 | private List fieldErrors; 13 | 14 | public FormValidationError(List fieldErrors) { 15 | this.fieldErrors = fieldErrors; 16 | } 17 | 18 | public List getFieldErrors() { 19 | return fieldErrors; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/todo/exception/TodoNotFoundException.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.exception; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public class TodoNotFoundException extends Exception { 7 | 8 | public TodoNotFoundException(String message) { 9 | super(message); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/todo/repository/TodoRepository.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.repository; 2 | 3 | import net.petrikainulainen.spring.testmvc.todo.model.Todo; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * @author Petri Kainulainen 8 | */ 9 | public interface TodoRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/SecurityRole.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | /** 4 | * @author Petri Kainulainen 5 | */ 6 | public enum SecurityRole { 7 | ROLE_USER 8 | } 9 | -------------------------------------------------------------------------------- /spring-32/src/main/java/net/petrikainulainen/spring/testmvc/user/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.user.dto; 2 | 3 | import org.apache.commons.lang.builder.ToStringBuilder; 4 | 5 | /** 6 | * @author Petri Kainulainen 7 | */ 8 | public class UserDTO { 9 | 10 | private String username; 11 | 12 | private SecurityRole role; 13 | 14 | public UserDTO(String username, SecurityRole role) { 15 | this.username = username; 16 | this.role = role; 17 | } 18 | 19 | public String getUsername() { 20 | return username; 21 | } 22 | 23 | public SecurityRole getRole() { 24 | return role; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return ToStringBuilder.reflectionToString(this); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-32/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Database Configuration 2 | db.driver=${db.driver} 3 | db.url=${db.url} 4 | db.username=${db.username} 5 | db.password=${db.password} 6 | 7 | #Hibernate Configuration 8 | hibernate.dialect=${hibernate.dialect} 9 | hibernate.format_sql=${hibernate.format_sql} 10 | hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} 11 | hibernate.ejb.naming_strategy=${hibernate.ejb.naming_strategy} 12 | hibernate.show_sql=${hibernate.show_sql} -------------------------------------------------------------------------------- /spring-32/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.Stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout 3 | log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} - %m\n 4 | 5 | log4j.rootLogger=DEBUG,Stdout 6 | log4j.logger.org.springframework=DEBUG 7 | log4j.logger.org.dbunit=INFO -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/css/example.css: -------------------------------------------------------------------------------- 1 | .page { 2 | margin-left: 5%; 3 | margin-right: 5% 4 | } 5 | 6 | .content { 7 | padding-left: 1em; 8 | padding-right: 1em; 9 | } 10 | 11 | .page-content { 12 | margin-bottom: 20px; 13 | margin-top: 20px; 14 | } 15 | 16 | .action-buttons { 17 | bottom: 20px; 18 | float: right; 19 | position: relative; 20 | } 21 | -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/img/glyphicons-halflings-white.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/spring-32/src/main/webapp/static/img/glyphicons-halflings-white.png -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/img/glyphicons-halflings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkainulainen/spring-mvc-test-examples/c2c44d47e058f207c7becde2e76ae043bd0d49eb/spring-32/src/main/webapp/static/img/glyphicons-halflings.png -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/error/error.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.ErrorController = { 2 | 404: function() { 3 | window.log("Rendering 404 view."); 4 | var notFoundView = new TodoApp.Views.NotFoundView(); 5 | TodoApp.mainRegion.show(notFoundView); 6 | }, 7 | notAuthorized: function() { 8 | window.log("Rendering not authorized view"); 9 | var notAuthorizedView = new TodoApp.Views.NotAuthorizedView(); 10 | TodoApp.mainRegion.show(notAuthorizedView); 11 | }, 12 | error: function() { 13 | window.log("Rendering error view."); 14 | var errorView = new TodoApp.Views.ErrorView(); 15 | TodoApp.mainRegion.show(errorView); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/error/error.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("error:404", function() { 2 | window.log("Processing 404 event.") 3 | Backbone.history.navigate("#/error/404"); 4 | }); 5 | 6 | TodoApp.vent.on("error:notAuthorized", function() { 7 | window.log("Processing not authorized event"); 8 | Backbone.history.navigate("#/error/notAuthorized") 9 | }); 10 | 11 | TodoApp.vent.on("error:error", function(){ 12 | window.log("Processing error event") 13 | Backbone.history.navigate("#/error/error"); 14 | }); -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/error/error.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.ErrorRouting = function(){ 2 | var ErrorRouting = {}; 3 | 4 | ErrorRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "error/404": "404", 7 | "error/notAuthorized": "notAuthorized", 8 | "error/error": "error" 9 | } 10 | }); 11 | 12 | TodoApp.addInitializer(function(){ 13 | ErrorRouting.router = new ErrorRouting.Router({ 14 | controller: TodoApp.Controllers.ErrorController 15 | }); 16 | }); 17 | 18 | return ErrorRouting; 19 | }(); 20 | 21 | -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/error/error.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.NotAuthorizedView = Marionette.ItemView.extend({ 2 | template: "#template-not-authorized-view" 3 | }) 4 | 5 | TodoApp.Views.NotFoundView = Marionette.ItemView.extend({ 6 | template: "#template-not-found-view" 7 | }); 8 | 9 | TodoApp.Views.ErrorView = Marionette.ItemView.extend({ 10 | template: "#template-error-view" 11 | }); -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/todo/todo.i18n.js: -------------------------------------------------------------------------------- 1 | TodoApp.Translations.resources = { 2 | dev: { 3 | translation: { 4 | 'loginFailed': 'Login failed.', 5 | 'todoAdded': 'Todo entry: __title__ was added.', 6 | 'todoDeleted': 'Todo entry: __title__ was deleted.', 7 | 'todoUpdated': 'Todo entry: __title__ was updated.' 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/todo/todo.models.js: -------------------------------------------------------------------------------- 1 | //Models 2 | TodoApp.Models.FeedbackMessage = Backbone.Model.extend(); 3 | 4 | TodoApp.Models.Todo = Backbone.Model.extend({ 5 | urlRoot: "/api/todo" 6 | }); 7 | 8 | //Collections 9 | TodoApp.Collections.Todos = Backbone.Collection.extend({ 10 | model: TodoApp.Models.Todo, 11 | url: function() { 12 | return "/api/todo"; 13 | } 14 | }) -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/todo/todo.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.TodoRouting = function(){ 2 | var TodoRouting = {}; 3 | 4 | TodoRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "todo/add": "add", 7 | "todo/:id": "view", 8 | "todo/update/:id": "update", 9 | "": "list" 10 | } 11 | }); 12 | 13 | TodoApp.addInitializer(function(){ 14 | TodoRouting.router = new TodoRouting.Router({ 15 | controller: TodoApp.Controllers.TodoController 16 | }); 17 | 18 | TodoApp.vent.trigger("routing:started"); 19 | }); 20 | 21 | return TodoRouting; 22 | }(); 23 | -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/user/user.controller.js: -------------------------------------------------------------------------------- 1 | TodoApp.Controllers.UserController = { 2 | login: function() { 3 | window.log("Rendering login page"); 4 | var loginView = new TodoApp.Views.LoginView(); 5 | TodoApp.mainRegion.show(loginView); 6 | }, 7 | logout: function() { 8 | window.log("Logging user out") 9 | $.get("/api/logout", function(data) { 10 | window.log("Logout successful. Received data: ", data); 11 | TodoApp.vent.trigger("user:logoutSuccess"); 12 | }) 13 | } 14 | }; -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/user/user.events.js: -------------------------------------------------------------------------------- 1 | TodoApp.vent.on("user:login", function(){ 2 | Backbone.history.navigate("#/user/login"); 3 | }); 4 | 5 | TodoApp.vent.on("user:loginFailed", function() { 6 | var translatedErrorMessage = i18n.t("loginFailed"); 7 | var errorMessage = new TodoApp.Models.FeedbackMessage({message: translatedErrorMessage}); 8 | var errorMessageView = new TodoApp.Views.ErrorMessageView({model: errorMessage}); 9 | TodoApp.messageRegion.show(errorMessageView); 10 | }); 11 | 12 | TodoApp.vent.on("user:loginSuccess", function() { 13 | var showTodoList = function() { 14 | Backbone.history.navigate("#/"); 15 | TodoApp.showLogoutLink(); 16 | } 17 | 18 | TodoApp.getLoggedInUser(showTodoList); 19 | }); 20 | 21 | TodoApp.vent.on("user:logoutSuccess", function() { 22 | TodoApp.setUserAsAnonymous(); 23 | Backbone.history.navigate("#/"); 24 | }); 25 | -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/user/user.models.js: -------------------------------------------------------------------------------- 1 | TodoApp.Models.User = Backbone.Model.extend(); 2 | -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/user/user.routing.js: -------------------------------------------------------------------------------- 1 | TodoApp.UserRouting = function() { 2 | var UserRouting = {}; 3 | 4 | UserRouting.Router = Backbone.Marionette.AppRouter.extend({ 5 | appRoutes: { 6 | "user/login": "login", 7 | "user/logout": "logout" 8 | } 9 | }); 10 | 11 | TodoApp.addInitializer(function(){ 12 | UserRouting.router = new UserRouting.Router({ 13 | controller: TodoApp.Controllers.UserController 14 | }); 15 | 16 | TodoApp.vent.trigger("routing:started"); 17 | }); 18 | 19 | return UserRouting; 20 | }(); -------------------------------------------------------------------------------- /spring-32/src/main/webapp/static/js/app/user/user.views.js: -------------------------------------------------------------------------------- 1 | TodoApp.Views.LoginView = Marionette.ItemView.extend({ 2 | template: "#template-login-view", 3 | events: { 4 | "click #login-button": "login" 5 | }, 6 | login: function() { 7 | window.log("Log in"); 8 | var user = {}; 9 | user.username = $("#user-username").val(); 10 | user.password = $("#user-password").val(); 11 | $.post("/api/login", user, function(){ 12 | TodoApp.vent.trigger("user:loginSuccess"); 13 | }) 14 | } 15 | }); 16 | -------------------------------------------------------------------------------- /spring-32/src/test/java/net/petrikainulainen/spring/testmvc/common/controller/HomeControllerTest.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.common.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static junit.framework.Assert.assertEquals; 7 | 8 | /** 9 | * @author Petri Kainulainen 10 | */ 11 | public class HomeControllerTest { 12 | 13 | private HomeController controller; 14 | 15 | @Before 16 | public void setUp() { 17 | controller = new HomeController(); 18 | } 19 | 20 | @Test 21 | public void showHomePage() { 22 | String view = controller.showHomePage(); 23 | assertEquals(HomeController.HOME_VIEW, view); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-32/src/test/java/net/petrikainulainen/spring/testmvc/todo/config/UnitTestContext.java: -------------------------------------------------------------------------------- 1 | package net.petrikainulainen.spring.testmvc.todo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; 6 | 7 | /** 8 | * @author Petri Kainulainen 9 | */ 10 | @Configuration 11 | public class UnitTestContext { 12 | 13 | @Bean 14 | public LocalValidatorFactoryBean validator() { 15 | return new LocalValidatorFactoryBean(); 16 | } 17 | } 18 | --------------------------------------------------------------------------------