() {
49 | @Override
50 | protected boolean matchesSafely(QueryExecution item) {
51 | return !item.isSuccess();
52 | }
53 |
54 | @Override
55 | public void describeTo(Description description) {
56 | description.appendText("failure");
57 | }
58 |
59 | @Override
60 | protected void describeMismatchSafely(QueryExecution item, Description mismatchDescription) {
61 | mismatchDescription.appendText("was success");
62 | }
63 | };
64 | }
65 |
66 | /**
67 | * Matcher to check {@link QueryExecution} was a batch execution.
68 | *
69 | * Example:
70 | * assertThat(qe, batch());
71 | */
72 | public static Matcher super QueryExecution> batch() {
73 | return new ExecutionTypeMatcher(ExecutionType.IS_BATCH);
74 | }
75 |
76 |
77 | /**
78 | * Matcher to check {@link QueryExecution} was an execution of {@link java.sql.Statement}.
79 | *
80 | * Example:
81 | * assertThat(qe, statement());
82 | */
83 | public static Matcher super QueryExecution> statement() {
84 | return new ExecutionTypeMatcher(ExecutionType.IS_STATEMENT);
85 | }
86 |
87 | /**
88 | * Matcher to check {@link QueryExecution} was a batch execution of {@link java.sql.Statement}.
89 | *
90 | * Example:
91 | * assertThat(qe, batchStatement());
92 | */
93 | public static Matcher super QueryExecution> batchStatement() {
94 | return new ExecutionTypeMatcher(ExecutionType.IS_BATCH_STATEMENT);
95 | }
96 |
97 | /**
98 | * Matcher to check {@link QueryExecution} was a normal or batch execution of {@link java.sql.Statement}.
99 | *
100 | * Example:
101 | * assertThat(qe, statementOrBatchStatement());
102 | */
103 | public static Matcher super QueryExecution> statementOrBatchStatement() {
104 | return new ExecutionTypeMatcher(ExecutionType.IS_STATEMENT_OR_BATCH_STATEMENT);
105 | }
106 |
107 | /**
108 | * Matcher to check {@link QueryExecution} was an execution of {@link java.sql.PreparedStatement}.
109 | *
110 | * Example:
111 | * assertThat(qe, prepared());
112 | */
113 | public static Matcher super QueryExecution> prepared() {
114 | return new ExecutionTypeMatcher(ExecutionType.IS_PREPARED);
115 | }
116 |
117 | /**
118 | * Matcher to check {@link QueryExecution} was a batch execution of {@link java.sql.PreparedStatement}.
119 | *
120 | * Example:
121 | * assertThat(qe, batchPrepared());
122 | */
123 | public static Matcher super QueryExecution> batchPrepared() {
124 | return new ExecutionTypeMatcher(ExecutionType.IS_BATCH_PREPARED);
125 | }
126 |
127 | /**
128 | * Matcher to check {@link QueryExecution} was a normal or batch execution of {@link java.sql.PreparedStatement}.
129 | *
130 | * Example:
131 | * assertThat(qe, preparedOrBatchPrepared());
132 | */
133 | public static Matcher super QueryExecution> preparedOrBatchPrepared() {
134 | return new ExecutionTypeMatcher(ExecutionType.IS_PREPARED_OR_BATCH_PREPARED);
135 | }
136 |
137 | /**
138 | * Matcher to check {@link QueryExecution} was an execution of {@link java.sql.CallableStatement}.
139 | *
140 | * Example:
141 | * assertThat(qe, callable());
142 | */
143 | public static Matcher super QueryExecution> callable() {
144 | return new ExecutionTypeMatcher(ExecutionType.IS_CALLABLE);
145 | }
146 |
147 | /**
148 | * Matcher to check {@link QueryExecution} was a batch execution of {@link java.sql.CallableStatement}.
149 | *
150 | * Example:
151 | * assertThat(qe, batchCallable());
152 | */
153 | public static Matcher super QueryExecution> batchCallable() {
154 | return new ExecutionTypeMatcher(ExecutionType.IS_BATCH_CALLABLE);
155 | }
156 |
157 | /**
158 | * Matcher to check {@link QueryExecution} was a normal or batch execution of {@link java.sql.CallableStatement}.
159 | *
160 | * Example:
161 | * assertThat(qe, callableOrBatchCallable());
162 | */
163 | public static Matcher super QueryExecution> callableOrBatchCallable() {
164 | return new ExecutionTypeMatcher(ExecutionType.IS_CALLABLE_OR_BATCH_CALLABLE);
165 | }
166 |
167 | }
168 |
--------------------------------------------------------------------------------
/src/main/java/net/ttddyy/dsproxy/asserts/hamcrest/QueryHolderAssertions.java:
--------------------------------------------------------------------------------
1 | package net.ttddyy.dsproxy.asserts.hamcrest;
2 |
3 | import net.ttddyy.dsproxy.QueryType;
4 | import net.ttddyy.dsproxy.asserts.QueryHolder;
5 | import net.ttddyy.dsproxy.listener.QueryUtils;
6 | import org.hamcrest.Description;
7 | import org.hamcrest.FeatureMatcher;
8 | import org.hamcrest.Matcher;
9 | import org.hamcrest.TypeSafeMatcher;
10 |
11 | /**
12 | * Hamcrest matchers for {@link QueryHolder}.
13 | *
14 | * @author Tadaya Tsuyukubo
15 | * @since 1.0
16 | */
17 | class QueryHolderAssertions {
18 |
19 | /**
20 | * Matcher to examine the query with given {@link String} matcher.
21 | *
22 | * Example:
23 | * assertThat(qe, query(startsWith("select")));
24 | */
25 | public static Matcher super QueryHolder> query(final Matcher stringMatcher) {
26 | return new FeatureMatcher(stringMatcher, "query", "query") {
27 | @Override
28 | protected String featureValueOf(QueryHolder actual) {
29 | return actual.getQuery();
30 | }
31 | };
32 | }
33 |
34 | /**
35 | * Matcher to examine the query type.
36 | *
37 | * Example:
38 | * assertThat(qe, queryType(SELECT));
39 | */
40 | public static Matcher super QueryHolder> queryType(final QueryType expectedType) {
41 |
42 | return new TypeSafeMatcher() {
43 | @Override
44 | protected boolean matchesSafely(QueryHolder item) {
45 | String query = item.getQuery();
46 | QueryType actualType = QueryUtils.getQueryType(query);
47 | if (expectedType != actualType) {
48 | return false;
49 | }
50 | return true;
51 | }
52 |
53 | @Override
54 | public void describeTo(Description description) {
55 | // expected clause
56 | description.appendText("query type \"" + expectedType + "\"");
57 | }
58 |
59 | @Override
60 | protected void describeMismatchSafely(QueryHolder item, Description mismatchDescription) {
61 | // but was clause
62 | String query = item.getQuery();
63 | QueryType actualType = QueryUtils.getQueryType(query);
64 |
65 | String msg = "query type was \"" + actualType + "\" (" + query + ")";
66 | mismatchDescription.appendText(msg);
67 | }
68 | };
69 | }
70 |
71 | /**
72 | * Matcher to examine the query type is SELECT.
73 | *
74 | * Example:
75 | * assertThat(qe, select());
76 | */
77 | public static Matcher super QueryHolder> select() {
78 | return queryType(QueryType.SELECT);
79 | }
80 |
81 | /**
82 | * Matcher to examine the query type is SELECT.
83 | *
84 | * Example:
85 | * assertThat(qe, insert());
86 | */
87 | public static Matcher super QueryHolder> insert() {
88 | return queryType(QueryType.INSERT);
89 | }
90 |
91 | /**
92 | * Matcher to examine the query type is SELECT.
93 | *
94 | * Example:
95 | * assertThat(qe, update());
96 | */
97 | public static Matcher super QueryHolder> update() {
98 | return queryType(QueryType.UPDATE);
99 | }
100 |
101 | /**
102 | * Matcher to examine the query type is SELECT.
103 | *
104 | * Example:
105 | * assertThat(qe, delete());
106 | */
107 | public static Matcher super QueryHolder> delete() {
108 | return queryType(QueryType.DELETE);
109 | }
110 |
111 | /**
112 | * Matcher to examine the query type is SELECT.
113 | *
114 | * Example:
115 | * assertThat(qe, other());
116 | */
117 | public static Matcher super QueryHolder> other() {
118 | return queryType(QueryType.OTHER);
119 | }
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/src/main/java/net/ttddyy/dsproxy/asserts/hamcrest/SqlTypeMatcher.java:
--------------------------------------------------------------------------------
1 | package net.ttddyy.dsproxy.asserts.hamcrest;
2 |
3 | import org.hamcrest.Description;
4 | import org.hamcrest.TypeSafeMatcher;
5 |
6 | import java.lang.reflect.Field;
7 | import java.sql.Types;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 |
11 | /**
12 | * Hamcrest matcher for {@link java.sql.Types}(int).
13 | *
14 | * @author Tadaya Tsuyukubo
15 | * @see java.sql.Types
16 | * @since 1.0
17 | */
18 | public class SqlTypeMatcher extends TypeSafeMatcher {
19 |
20 | public static final Map typeNameByValue = new HashMap<>();
21 |
22 | static {
23 | try {
24 | Field[] fields = Types.class.getDeclaredFields();
25 | for (Field field : fields) {
26 | typeNameByValue.put(field.getInt(null), field.getName());
27 | }
28 | } catch (Exception e) {
29 | }
30 | }
31 |
32 | protected int expectedSqlType;
33 | protected String messagePrefix = "";
34 | protected String messageSuffix = "";
35 |
36 | public SqlTypeMatcher(int expectedSqlType) {
37 | this.expectedSqlType = expectedSqlType;
38 | }
39 |
40 | public SqlTypeMatcher(int expectedSqlType, String messagePrefix, String messageSuffix) {
41 | this.expectedSqlType = expectedSqlType;
42 | this.messagePrefix = messagePrefix;
43 | this.messageSuffix = messageSuffix;
44 | }
45 |
46 | @Override
47 | protected boolean matchesSafely(Integer item) {
48 | if (item == null) {
49 | return false;
50 | }
51 | return this.expectedSqlType == item;
52 | }
53 |
54 |
55 | @Override
56 | public void describeTo(Description description) {
57 | // expected message
58 | description.appendText(getMessage(this.expectedSqlType));
59 | }
60 |
61 | @Override
62 | protected void describeMismatchSafely(Integer item, Description mismatchDescription) {
63 | // for actual(but was) message
64 | mismatchDescription.appendText(getMessage(item));
65 | }
66 |
67 | protected String getMessage(Integer sqlType) {
68 | String typeName = typeNameByValue.get(sqlType);
69 | if (typeName == null) {
70 | typeName = "UNKNOWN";
71 | }
72 | return this.messagePrefix + typeName + ":" + sqlType + this.messageSuffix;
73 | }
74 |
75 | public void setMessagePrefix(String messagePrefix) {
76 | this.messagePrefix = messagePrefix;
77 | }
78 |
79 | public void setMessageSuffix(String messageSuffix) {
80 | this.messageSuffix = messageSuffix;
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/src/test/java/net/ttddyy/dsproxy/asserts/DatabaseTestRule.java:
--------------------------------------------------------------------------------
1 | package net.ttddyy.dsproxy.asserts;
2 |
3 | import org.flywaydb.core.Flyway;
4 | import org.hsqldb.jdbc.JDBCDataSource;
5 | import org.junit.rules.ExternalResource;
6 |
7 | import javax.sql.DataSource;
8 |
9 | /**
10 | * @author Tadaya Tsuyukubo
11 | * @since 1.0
12 | */
13 | public class DatabaseTestRule extends ExternalResource {
14 |
15 | public DataSource dataSource;
16 |
17 | private Flyway flyway;
18 |
19 | @Override
20 | protected void before() throws Throwable {
21 |
22 | // prepare ds
23 | JDBCDataSource ds = new JDBCDataSource();
24 | ds.setDatabase("jdbc:hsqldb:mem:aname");
25 | ds.setUser("sa");
26 | this.dataSource = ds;
27 |
28 | // populate data
29 | this.flyway = new Flyway();
30 | this.flyway.setDataSource(ds);
31 | this.flyway.migrate();
32 |
33 | }
34 |
35 | @Override
36 | protected void after() {
37 | flyway.clean();
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/test/java/net/ttddyy/dsproxy/asserts/ParameterKeyValueTest.java:
--------------------------------------------------------------------------------
1 | package net.ttddyy.dsproxy.asserts;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.SortedSet;
6 | import java.util.TreeSet;
7 |
8 | import static net.ttddyy.dsproxy.asserts.ParameterKeyValueUtils.createRegisterOut;
9 | import static net.ttddyy.dsproxy.asserts.ParameterKeyValueUtils.createSetParam;
10 | import static org.assertj.core.api.Assertions.assertThat;
11 |
12 | /**
13 | * @author Tadaya Tsuyukubo
14 | */
15 | public class ParameterKeyValueTest {
16 |
17 | @Test
18 | public void compareToIndex() {
19 | SortedSet set = new TreeSet<>();
20 | set.add(createSetParam(1, 10));
21 | set.add(createSetParam(3, 10));
22 | set.add(createSetParam(2, 10));
23 |
24 | assertThat(set).hasSize(3).extracting("key.index").containsExactly(1, 2, 3);
25 | }
26 |
27 | @Test
28 | public void compareToSameIndex() {
29 | SortedSet set = new TreeSet<>();
30 | set.add(createSetParam(1, 10));
31 | set.add(createSetParam(1, 30));
32 | set.add(createSetParam(1, 20));
33 |
34 | assertThat(set).hasSize(3).extracting("key.index").containsExactly(1, 1, 1);
35 |
36 | // value will be insertion order
37 | assertThat(set).extracting("value").containsExactly(10, 30, 20);
38 | }
39 |
40 | @Test
41 | public void compareToSameIndexDifferentValueType() {
42 | SortedSet set = new TreeSet<>();
43 | set.add(createSetParam(1, 10));
44 | set.add(createSetParam(1, "30"));
45 | set.add(createSetParam(1, 20));
46 |
47 | assertThat(set).hasSize(3).extracting("key.index").containsExactly(1, 1, 1);
48 |
49 | // value will be insertion order
50 | assertThat(set).extracting("value").containsExactly(10, "30", 20);
51 | }
52 |
53 | @Test
54 | public void compareToDifferentValue() {
55 | SortedSet set = new TreeSet<>();
56 | set.add(createSetParam(1, null));
57 | set.add(createRegisterOut(1, null));
58 | set.add(createSetParam(1, null)); // same key, value again
59 |
60 | // regardless operation type, it only checks key & value
61 | assertThat(set).hasSize(1).extracting("key.index").containsExactly(1);
62 | }
63 |
64 | @Test
65 | public void compareToDifferentKeyTypeWithInt() {
66 | SortedSet set = new TreeSet<>();
67 | set.add(createSetParam(1, 10));
68 | set.add(createSetParam("10", 20));
69 | set.add(createSetParam(1, 20));
70 |
71 | // regardless operation type, it only checks key & value
72 | assertThat(set).hasSize(3).extracting("key.keyAsString").containsExactly("1", "1", "10");
73 | }
74 |
75 | @Test
76 | public void compareToNumberKeyInString() {
77 | SortedSet set = new TreeSet<>();
78 | set.add(createSetParam(1, 10));
79 | set.add(createSetParam("1", 20));
80 |
81 | assertThat(set).hasSize(2).extracting("key.keyAsString").containsExactly("1", "1");
82 | assertThat(set).extracting("value").containsExactly(10, 20);
83 | }
84 |
85 | @Test
86 | public void compareToDifferentKeyType() {
87 | SortedSet set = new TreeSet<>();
88 | set.add(createSetParam(1, 10));
89 | set.add(createSetParam("foo", 200));
90 | set.add(createSetParam(1, 20));
91 | set.add(createSetParam("bar", 300));
92 |
93 | // regardless operation type, it only checks key & value
94 | assertThat(set).hasSize(4).extracting("key.keyAsString").containsExactly("1", "1", "bar", "foo");
95 | assertThat(set).extracting("value").containsExactly(10, 20, 300, 200);
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/src/test/java/net/ttddyy/dsproxy/asserts/assertj/QueryExecutionAssertTest.java:
--------------------------------------------------------------------------------
1 | package net.ttddyy.dsproxy.asserts.assertj;
2 |
3 | import net.ttddyy.dsproxy.asserts.CallableBatchExecution;
4 | import net.ttddyy.dsproxy.asserts.CallableExecution;
5 | import net.ttddyy.dsproxy.asserts.PreparedBatchExecution;
6 | import net.ttddyy.dsproxy.asserts.PreparedExecution;
7 | import net.ttddyy.dsproxy.asserts.QueryExecution;
8 | import net.ttddyy.dsproxy.asserts.StatementBatchExecution;
9 | import net.ttddyy.dsproxy.asserts.StatementExecution;
10 | import org.junit.Test;
11 |
12 | import static org.assertj.core.api.Assertions.assertThat;
13 | import static org.junit.Assert.fail;
14 | import static org.mockito.BDDMockito.given;
15 | import static org.mockito.Mockito.mock;
16 |
17 | /**
18 | * @author Tadaya Tsuyukubo
19 | */
20 | public class QueryExecutionAssertTest {
21 |
22 | @Test
23 | public void testIsSuccess() {
24 | QueryExecution qe = mock(QueryExecution.class);
25 | given(qe.isSuccess()).willReturn(true);
26 |
27 | DataSourceAssertAssertions.assertThat(qe).isSuccess();
28 |
29 | try {
30 | DataSourceAssertAssertions.assertThat(qe).isFailure();
31 | fail("asserts should fail");
32 | } catch (AssertionError e) {
33 | assertThat(e).hasMessage("\nExpecting: but was: \n");
34 | }
35 |
36 | }
37 |
38 | @Test
39 | public void testIsFailure() {
40 | QueryExecution qe = mock(QueryExecution.class);
41 | given(qe.isSuccess()).willReturn(false);
42 |
43 | DataSourceAssertAssertions.assertThat(qe).isFailure();
44 |
45 | try {
46 | DataSourceAssertAssertions.assertThat(qe).isSuccess();
47 | fail("asserts should fail");
48 | } catch (AssertionError e) {
49 | assertThat(e).hasMessage("\nExpecting: but was: \n");
50 | }
51 | }
52 |
53 | @Test
54 | public void testIsBatch() {
55 | // batch=true
56 | QueryExecution qe = mock(QueryExecution.class);
57 | given(qe.isBatch()).willReturn(true);
58 |
59 | DataSourceAssertAssertions.assertThat(qe).isBatch();
60 |
61 | // batch=false
62 | qe = mock(QueryExecution.class);
63 | given(qe.isBatch()).willReturn(false);
64 | try {
65 | DataSourceAssertAssertions.assertThat(qe).isBatch();
66 | fail("asserts should fail");
67 | } catch (AssertionError e) {
68 | assertThat(e).hasMessage("\nExpecting: but was: \n");
69 | }
70 | }
71 |
72 | @Test
73 | public void testIsStatement() {
74 | QueryExecution se = new StatementExecution();
75 | QueryExecution sbe = new StatementBatchExecution();
76 | QueryExecution pe = new PreparedExecution();
77 |
78 | DataSourceAssertAssertions.assertThat(se).isStatement();
79 | DataSourceAssertAssertions.assertThat(sbe).isBatchStatement();
80 | DataSourceAssertAssertions.assertThat(se).isStatementOrBatchStatement();
81 | DataSourceAssertAssertions.assertThat(sbe).isStatementOrBatchStatement();
82 |
83 | try {
84 | DataSourceAssertAssertions.assertThat(pe).isStatement();
85 | fail("asserts should fail");
86 | } catch (AssertionError e) {
87 | assertThat(e).hasMessage("\nExpecting: but was: \n");
88 | }
89 | try {
90 | DataSourceAssertAssertions.assertThat(pe).isBatchStatement();
91 | fail("asserts should fail");
92 | } catch (AssertionError e) {
93 | assertThat(e).hasMessage("\nExpecting: but was: \n");
94 | }
95 | try {
96 | DataSourceAssertAssertions.assertThat(pe).isStatementOrBatchStatement();
97 | fail("asserts should fail");
98 | } catch (AssertionError e) {
99 | assertThat(e).hasMessage("\nExpecting: but was: \n");
100 | }
101 | }
102 |
103 | @Test
104 | public void testIsPrepared() {
105 | QueryExecution pe = new PreparedExecution();
106 | QueryExecution pbe = new PreparedBatchExecution();
107 | QueryExecution se = new StatementExecution();
108 |
109 | DataSourceAssertAssertions.assertThat(pe).isPrepared();
110 | DataSourceAssertAssertions.assertThat(pbe).isBatchPrepared();
111 | DataSourceAssertAssertions.assertThat(pe).isPreparedOrBatchPrepared();
112 | DataSourceAssertAssertions.assertThat(pbe).isPreparedOrBatchPrepared();
113 |
114 | try {
115 | DataSourceAssertAssertions.assertThat(se).isPrepared();
116 | fail("asserts should fail");
117 | } catch (AssertionError e) {
118 | assertThat(e).hasMessage("\nExpecting: but was: \n");
119 | }
120 | try {
121 | DataSourceAssertAssertions.assertThat(se).isBatchPrepared();
122 | fail("asserts should fail");
123 | } catch (AssertionError e) {
124 | assertThat(e).hasMessage("\nExpecting: but was: \n");
125 | }
126 | try {
127 | DataSourceAssertAssertions.assertThat(se).isPreparedOrBatchPrepared();
128 | fail("asserts should fail");
129 | } catch (AssertionError e) {
130 | assertThat(e).hasMessage("\nExpecting: but was: \n");
131 | }
132 | }
133 |
134 | @Test
135 | public void testIsCallable() {
136 | QueryExecution ce = new CallableExecution();
137 | QueryExecution cbe = new CallableBatchExecution();
138 | QueryExecution se = new StatementExecution();
139 |
140 | DataSourceAssertAssertions.assertThat(ce).isCallable();
141 | DataSourceAssertAssertions.assertThat(cbe).isBatchCallable();
142 | DataSourceAssertAssertions.assertThat(ce).isCallableOrBatchCallable();
143 | DataSourceAssertAssertions.assertThat(cbe).isCallableOrBatchCallable();
144 |
145 | try {
146 | DataSourceAssertAssertions.assertThat(se).isCallable();
147 | fail("asserts should fail");
148 | } catch (AssertionError e) {
149 | assertThat(e).hasMessage("\nExpecting: but was: \n");
150 | }
151 | try {
152 | DataSourceAssertAssertions.assertThat(se).isBatchCallable();
153 | fail("asserts should fail");
154 | } catch (AssertionError e) {
155 | assertThat(e).hasMessage("\nExpecting: but was: \n");
156 | }
157 | try {
158 | DataSourceAssertAssertions.assertThat(se).isCallableOrBatchCallable();
159 | fail("asserts should fail");
160 | } catch (AssertionError e) {
161 | assertThat(e).hasMessage("\nExpecting: but was: \n");
162 | }
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/src/test/java/net/ttddyy/dsproxy/asserts/assertj/StatementBatchExecutionAssertTest.java:
--------------------------------------------------------------------------------
1 | package net.ttddyy.dsproxy.asserts.assertj;
2 |
3 | import net.ttddyy.dsproxy.QueryType;
4 | import net.ttddyy.dsproxy.asserts.StatementBatchExecution;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Arrays;
10 | import java.util.List;
11 |
12 | import static org.assertj.core.api.Assertions.assertThat;
13 | import static org.assertj.core.api.Assertions.atIndex;
14 | import static org.junit.Assert.fail;
15 | import static org.mockito.BDDMockito.given;
16 | import static org.mockito.Mockito.mock;
17 |
18 | /**
19 | * @author Tadaya Tsuyukubo
20 | */
21 | public class StatementBatchExecutionAssertTest {
22 | private StatementBatchExecution sbe;
23 | private StatementBatchExecutionAssert sbeAssert;
24 |
25 | @Before
26 | public void setUp() {
27 | this.sbe = new StatementBatchExecution();
28 | this.sbeAssert = new StatementBatchExecutionAssert(this.sbe);
29 | }
30 |
31 | @Test
32 | public void testIsSuccess() {
33 | // success case
34 | this.sbe.setSuccess(true);
35 | this.sbeAssert.isSuccess();
36 |
37 | // failure case
38 | this.sbe.setSuccess(false);
39 | try {
40 | this.sbeAssert.isSuccess();
41 | fail("asserts should fail");
42 | } catch (AssertionError e) {
43 | assertThat(e).hasMessage("\nExpecting: but was: \n");
44 | }
45 |
46 | }
47 |
48 | @Test
49 | public void testIsFailure() {
50 | // success case
51 | this.sbe.setSuccess(false);
52 | this.sbeAssert.isFailure();
53 |
54 | // failure case
55 | this.sbe.setSuccess(true);
56 | try {
57 | this.sbeAssert.isFailure();
58 | fail("asserts should fail");
59 | } catch (AssertionError e) {
60 | assertThat(e).hasMessage("\nExpecting: but was: \n");
61 | }
62 | }
63 |
64 | @Test
65 | public void testHasBatchSize() {
66 | List entries = new ArrayList<>();
67 | entries.addAll(Arrays.asList("a", "b", "c"));
68 |
69 | StatementBatchExecution sbe = mock(StatementBatchExecution.class);
70 | given(sbe.getQueries()).willReturn(entries);
71 |
72 | DataSourceAssertAssertions.assertThat(sbe).hasBatchSize(3);
73 |
74 | try {
75 | DataSourceAssertAssertions.assertThat(sbe).hasBatchSize(1);
76 | fail("exception should be thrown");
77 | } catch (AssertionError e) {
78 | assertThat(e).hasMessage("\nExpected batch size:<1> but was:<3> in batch statement executions\n");
79 | }
80 | }
81 |
82 | @Test
83 | public void testQueries() {
84 | StatementBatchExecution sbe = new StatementBatchExecution();
85 | sbe.getQueries().add("foo");
86 | sbe.getQueries().add("bar");
87 | sbe.getQueries().add("baz");
88 |
89 | DataSourceAssertAssertions.assertThat(sbe).queries().contains("bar");
90 |
91 | try {
92 | DataSourceAssertAssertions.assertThat(sbe).queries().contains("WRONG");
93 | fail("exception should be thrown");
94 | } catch (AssertionError e) {
95 | }
96 | }
97 |
98 | @Test
99 | public void testQuery() {
100 | StatementBatchExecution sbe = new StatementBatchExecution();
101 | sbe.getQueries().add("foo");
102 | sbe.getQueries().add("bar");
103 | sbe.getQueries().add("baz");
104 |
105 | DataSourceAssertAssertions.assertThat(sbe).query(1).isEqualTo("bar");
106 |
107 | try {
108 | DataSourceAssertAssertions.assertThat(sbe).query(3);
109 | fail("exception should be thrown");
110 | } catch (Throwable e) {
111 | assertThat(e).isInstanceOf(IndexOutOfBoundsException.class);
112 | }
113 | }
114 |
115 | @Test
116 | public void testContains() {
117 | StatementBatchExecution sbe = new StatementBatchExecution();
118 | sbe.getQueries().add("SELECT");
119 | sbe.getQueries().add("INSERT");
120 | sbe.getQueries().add("SELECT");
121 |
122 | DataSourceAssertAssertions.assertThat(sbe).contains(QueryType.SELECT, 0);
123 | DataSourceAssertAssertions.assertThat(sbe).contains(QueryType.INSERT, 1);
124 | DataSourceAssertAssertions.assertThat(sbe).contains(QueryType.SELECT, atIndex(2));
125 |
126 | // wrong type
127 | try {
128 | DataSourceAssertAssertions.assertThat(sbe).contains(QueryType.DELETE, 0);
129 | fail("exception should be thrown");
130 | } catch (AssertionError e) {
131 | assertThat(e).hasMessage("\nExpected query type: but was: