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  // file: classpath:guru/mikelue/jdut/junit4
21  // 	-> JdutYamlFactoryForClassRuleTest.yaml
22  @JdutResource
23  public class JdutYamlFactoryForClassRuleTest extends AbstractDataSourceTestBase {
24  	public JdutYamlFactoryForClassRuleTest() {}
25  
26  	@Before
27  	public void setupTable() throws SQLException
28  	{
29  		JdbcTemplateFactory.buildRunnable(
30  			() -> getDataSource().getConnection(),
31  			conn -> DbStatement.buildRunnableForStatement(
32  				conn, stmt -> {
33  					stmt.execute("DROP TABLE IF EXISTS class_t1");
34  					stmt.execute("CREATE TABLE class_t1(t1_id INTEGER)");
35  				}
36  			).runJdbc()
37  		).runJdbc();
38  	}
39  
40  	/**
41  	 * Tests build/clean data on class level.
42  	 */
43  	@Test
44  	public void sampleTest() throws Throwable, SQLException
45  	{
46  		final TestRule testedRule = JdutYamlFactory.buildByDataSource(AbstractDataSourceTestBase::getDataSource);
47  
48  		final MutableBoolean isAssertAfterBuild = new MutableBoolean(false);
49  		testedRule.apply(
50  			new Statement() {
51  				@Override
52  				public void evaluate() throws Throwable
53  				{
54  					isAssertAfterBuild.setTrue();
55  					assertData(2);
56  				}
57  			},
58  			Description.createSuiteDescription(getClass())
59  		).evaluate();
60  
61  		Assert.assertTrue(isAssertAfterBuild.booleanValue());
62  		assertData(0);
63  	}
64  
65  	/**
66  	 * Tests build/clean data without @JdutResource on class level(nothing happened).
67  	 */
68  	@Test
69  	public void sampleTestWithoutAnnotation() throws Throwable, SQLException
70  	{
71  		final TestRule testedRule = JdutYamlFactory.buildByDataSource(AbstractDataSourceTestBase::getDataSource);
72  
73  		final MutableBoolean isAssertAfterBuild = new MutableBoolean(false);
74  		testedRule.apply(
75  			new Statement() {
76  				@Override
77  				public void evaluate() throws Throwable
78  				{
79  					isAssertAfterBuild.setTrue();
80  					assertData(0);
81  				}
82  			},
83  			Description.createSuiteDescription(WithoutAnnotation.class)
84  		).evaluate();
85  
86  		Assert.assertTrue(isAssertAfterBuild.booleanValue());
87  		assertData(0);
88  	}
89  
90  	private void assertData(int expectedCount) throws SQLException
91  	 {
92  		String checkData = "SELECT COUNT(*) FROM class_t1 WHERE t1_id = 33";
93  		JdbcTemplateFactory.buildRunnable(
94  			() -> getDataSource().getConnection(),
95  			conn -> DbResultSet.buildRunnable(
96  				conn, checkData,
97  				rs -> new ResultSetAssert(rs)
98  					.assertNextTrue()
99  					.assertInt(1, expectedCount)
100 			).runJdbc()
101 		).runJdbc();
102 	}
103 }
104 
105 class WithoutAnnotation {}