Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. package test.loginController;
  2.  
  3. import static org.hamcrest.Matchers.hasProperty;
  4. import static org.hamcrest.Matchers.isEmptyOrNullString;
  5. import static org.hamcrest.Matchers.nullValue;
  6. import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get;
  7. import static org.springframework.test.web.server.result.MockMvcResultMatchers.forwardedUrl;
  8. import static org.springframework.test.web.server.result.MockMvcResultMatchers.model;
  9. import static org.springframework.test.web.server.result.MockMvcResultMatchers.status;
  10. import static org.springframework.test.web.server.result.MockMvcResultMatchers.view;
  11.  
  12. import java.io.InputStreamReader;
  13. import java.sql.Connection;
  14.  
  15. import javax.sql.DataSource;
  16.  
  17. import org.dbunit.JdbcDatabaseTester;
  18. import org.dbunit.dataset.IDataSet;
  19. import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
  20. import org.h2.jdbcx.JdbcDataSource;
  21. import org.h2.tools.RunScript;
  22. import org.junit.Before;
  23. import org.junit.BeforeClass;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.springframework.test.context.ContextConfiguration;
  27. import org.springframework.test.context.TestExecutionListeners;
  28. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  29. import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
  30. import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
  31. import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
  32. import org.springframework.test.web.server.MockMvc;
  33.  
  34. import com.github.springtestdbunit.DbUnitTestExecutionListener;
  35. import com.github.springtestdbunit.annotation.DatabaseSetup;
  36. import com.github.springtestdbunit.annotation.ExpectedDatabase;
  37.  
  38. @RunWith(SpringJUnit4ClassRunner.class)
  39. @ContextConfiguration
  40. @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
  41. TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
  42. @DatabaseSetup("login.xml")
  43. public class LoginControllerTest2 {
  44.  
  45. private static final String JDBC_DRIVER = "org.h2.Driver";
  46. private static final String JDBC_URL = "jdbc:h2:mem:login;DB_CLOSE_DELAY=-1";
  47. private static final String USER = "sa";
  48. private static final String PASS = "";
  49. private static final String SCHEMA_FILE = "../sql/h2.sql";
  50. private static final String DATASET = "login.xml";
  51.  
  52. private MockMvc mockMvc;
  53.  
  54. @BeforeClass
  55. public static void createSchema() throws ClassNotFoundException {
  56. Class.forName(JDBC_DRIVER);
  57. try {
  58. Connection conn = dataSource().getConnection();
  59. InputStreamReader in = new InputStreamReader(LoginControllerTest2.class.getResourceAsStream(SCHEMA_FILE));
  60. RunScript.execute(conn, in);
  61. } catch (Exception e) {
  62. // TODO: handle exception
  63. }
  64. }
  65.  
  66. @Before
  67. public void loadTestData() throws Exception {
  68. IDataSet ids = new FlatXmlDataSetBuilder().build(LoginControllerTest2.class.getResourceAsStream(DATASET));
  69. JdbcDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASS);
  70. databaseTester.setSetUpOperation(org.dbunit.operation.DatabaseOperation.CLEAN_INSERT);
  71. databaseTester.setDataSet(ids);
  72. databaseTester.onSetup();
  73. }
  74.  
  75. private static DataSource dataSource() {
  76. JdbcDataSource dataSource = new JdbcDataSource();
  77. dataSource.setURL(JDBC_URL);
  78. dataSource.setUser(USER);
  79. dataSource.setPassword(PASS);
  80. return dataSource;
  81. }
  82.  
  83. @Test
  84. @ExpectedDatabase("login.xml")
  85. public void testShowForm() throws Exception {
  86. mockMvc.perform(get("/login")).andExpect(status().isOk()).andExpect(view().name("/login"))
  87. .andExpect(forwardedUrl("../../WebContent/j/login.jsp"))
  88. .andExpect(model().attribute("form", hasProperty("passportId", nullValue())))
  89. .andExpect(model().attribute("form", hasProperty("email", isEmptyOrNullString())))
  90. .andExpect(model().attribute("form", hasProperty("username", isEmptyOrNullString())))
  91. .andExpect(model().attribute("form", hasProperty("hostname", isEmptyOrNullString())))
  92. .andExpect(model().attribute("form", hasProperty("pass", isEmptyOrNullString())));
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement