View Javadoc
1   package guru.mikelue.jdut.junit4;
2   
3   import java.sql.SQLException;
4   
5   import org.apache.commons.lang3.mutable.MutableBoolean;
6   import org.junit.Assert;
7   import org.junit.Before;
8   import org.junit.Test;
9   import org.junit.rules.TestRule;
10  import org.junit.runner.Description;
11  import org.junit.runners.model.Statement;
12  
13  import guru.mikelue.jdut.annotation.JdutResource;
14  import guru.mikelue.jdut.assertion.ResultSetAssert;
15  import guru.mikelue.jdut.jdbc.JdbcTemplateFactory;
16  import guru.mikelue.jdut.jdbc.function.DbResultSet;
17  import guru.mikelue.jdut.jdbc.function.DbStatement;
18  import guru.mikelue.jdut.junit4.test.AbstractDataSourceTestBase;
19  
20  public class JdutYamlFactoryTest extends AbstractDataSourceTestBase {
21  	public JdutYamlFactoryTest() {}
22  
23  	@Before
24  	public void setupTable() throws SQLException
25  	{
26  		JdbcTemplateFactory.buildRunnable(
27  			() -> getDataSource().getConnection(),
28  			conn -> DbStatement.buildRunnableForStatement(
29  				conn, stmt -> {
30  					stmt.execute("DROP TABLE IF EXISTS method_t1");
31  					stmt.execute("CREATE TABLE method_t1(t1_id INTEGER)");
32  				}
33  			).runJdbc()
34  		).runJdbc();
35  	}
36  
37  	/**
38  	 * Tests build/clean data on method level.
39  	 */
40  	// file: classpath:guru/mikelue/jdut/junit4
41  	// 	-> JdutYamlFactoryTest-sampleTest.yaml
42  	@Test @JdutResource
43  	public void sampleTest() throws Throwable, SQLException
44  	{
45  		final TestRule testedRule = JdutYamlFactory.buildByDataSource(AbstractDataSourceTestBase::getDataSource);
46  
47  		final MutableBoolean isAssertAfterBuild = new MutableBoolean(false);
48  		testedRule.apply(
49  			new Statement() {
50  				@Override
51  				public void evaluate() throws Throwable
52  				{
53  					isAssertAfterBuild.setTrue();
54  					assertData(2);
55  				}
56  			},
57  			Description.createTestDescription(getClass(), "sampleTest", getClass().getMethod("sampleTest").getAnnotation(JdutResource.class))
58  		).evaluate();
59  
60  		Assert.assertTrue(isAssertAfterBuild.booleanValue());
61  		assertData(0);
62  	}
63  
64  	/**
65  	 * Tests build/clean data without @JdutResource(nothing happened).
66  	 */
67  	@Test
68  	public void sampleTestWithoutAnnotation() throws Throwable, SQLException
69  	{
70  		final TestRule testedRule = JdutYamlFactory.buildByDataSource(AbstractDataSourceTestBase::getDataSource);
71  
72  		final MutableBoolean isAssertAfterBuild = new MutableBoolean(false);
73  		testedRule.apply(
74  			new Statement() {
75  				@Override
76  				public void evaluate() throws Throwable
77  				{
78  					isAssertAfterBuild.setTrue();
79  					assertData(0);
80  				}
81  			},
82  			Description.createTestDescription(getClass(), "sampleTestWithoutAnnotation")
83  		).evaluate();
84  
85  		Assert.assertTrue(isAssertAfterBuild.booleanValue());
86  		assertData(0);
87  	}
88  
89  	private void assertData(int expectedCount) throws SQLException
90  	{
91  		String checkData = "SELECT COUNT(*) FROM method_t1 WHERE t1_id = 33";
92  		JdbcTemplateFactory.buildRunnable(
93  			() -> getDataSource().getConnection(),
94  			conn -> DbResultSet.buildRunnable(
95  				conn, checkData,
96  				rs -> new ResultSetAssert(rs)
97  					.assertNextTrue()
98  					.assertInt(1, expectedCount)
99  			).runJdbc()
100 		).runJdbc();
101 	}
102 }