Skip to content

Commit cd3e529

Browse files
committed
SQL Matcher enhancement
The SQL Matcher, used in tests, gives better results and a more understandable stack when failing
1 parent d3e2e64 commit cd3e529

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/test/java/com/thibaultdelor/JSQL/SQLQueryMatcher.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
public class SQLQueryMatcher extends BaseMatcher<CharSequence> {
77

8+
9+
private final String originalExpectedQuery;
810
private final String expectedQuery;
911

1012
private static final char[] whiteSpaceNotSensitiveChars = { ',', '(', ')',
@@ -14,7 +16,8 @@ public SQLQueryMatcher(CharSequence expectedQuery) {
1416
if (expectedQuery == null)
1517
throw new IllegalArgumentException(
1618
"Non-null value required by SQLQueryMatcher");
17-
this.expectedQuery = minimizeQuery(expectedQuery.toString());
19+
this.originalExpectedQuery = expectedQuery.toString();
20+
this.expectedQuery = minimizeQuery(originalExpectedQuery);
1821
}
1922

2023
@Override
@@ -23,7 +26,24 @@ public boolean matches(Object match) {
2326
throw new IllegalArgumentException("Cannot match to a null value");
2427

2528
String minimizedQuery = minimizeQuery(match.toString());
26-
return minimizedQuery.equalsIgnoreCase(expectedQuery);
29+
boolean equals = minimizedQuery.equals(expectedQuery);
30+
31+
if(!equals){
32+
StringBuilder sb = new StringBuilder();
33+
34+
sb.append("\nOriginal Queries");
35+
sb.append("\n expected : ");
36+
sb.append(originalExpectedQuery);
37+
sb.append("\n got : ");
38+
sb.append(match.toString());
39+
sb.append("\nMinimized Queries");
40+
sb.append("\n expected : ");
41+
sb.append(expectedQuery);
42+
sb.append("\n got : ");
43+
sb.append(minimizedQuery);
44+
throw new AssertionError(sb.toString());
45+
}
46+
return equals;
2747
}
2848

2949
@Override
@@ -43,26 +63,26 @@ public void describeTo(Description description) {
4363
*/
4464
public static String minimizeQuery(String query) {
4565
StringBuilder result = new StringBuilder();
46-
boolean lastWasSpace = true;
66+
int lastType =0; //0 whitespace, 1 whitespace sensitive, 2 char
4767
for (int i = 0; i < query.length(); ++i) {
4868
char c = query.charAt(i);
4969
if (Character.isWhitespace(c)) {
50-
if (!(lastWasSpace)) {
70+
if (lastType==2) {
5171
result.append(' ');
5272
}
53-
lastWasSpace = true;
73+
lastType = 0;
5474
} else if (!isWhiteSpaceSensitive(c)) {
55-
if (lastWasSpace && result.length() > 0)
75+
if (lastType==0 && result.length() > 0)
5676
result.deleteCharAt(result.length() - 1);
5777

5878
result.append(c);
59-
lastWasSpace = true;
79+
lastType = 1;
6080
} else {
6181
result.append(c);
62-
lastWasSpace = false;
82+
lastType = 2;
6383
}
6484
}
65-
return result.toString().trim();
85+
return result.toString().trim().toLowerCase();
6686
}
6787

6888
private static boolean isWhiteSpaceSensitive(char c) {

0 commit comments

Comments
 (0)