5
5
6
6
public class SQLQueryMatcher extends BaseMatcher <CharSequence > {
7
7
8
+
9
+ private final String originalExpectedQuery ;
8
10
private final String expectedQuery ;
9
11
10
12
private static final char [] whiteSpaceNotSensitiveChars = { ',' , '(' , ')' ,
@@ -14,7 +16,8 @@ public SQLQueryMatcher(CharSequence expectedQuery) {
14
16
if (expectedQuery == null )
15
17
throw new IllegalArgumentException (
16
18
"Non-null value required by SQLQueryMatcher" );
17
- this .expectedQuery = minimizeQuery (expectedQuery .toString ());
19
+ this .originalExpectedQuery = expectedQuery .toString ();
20
+ this .expectedQuery = minimizeQuery (originalExpectedQuery );
18
21
}
19
22
20
23
@ Override
@@ -23,7 +26,24 @@ public boolean matches(Object match) {
23
26
throw new IllegalArgumentException ("Cannot match to a null value" );
24
27
25
28
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 ("\n Original Queries" );
35
+ sb .append ("\n expected : " );
36
+ sb .append (originalExpectedQuery );
37
+ sb .append ("\n got : " );
38
+ sb .append (match .toString ());
39
+ sb .append ("\n Minimized 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 ;
27
47
}
28
48
29
49
@ Override
@@ -43,26 +63,26 @@ public void describeTo(Description description) {
43
63
*/
44
64
public static String minimizeQuery (String query ) {
45
65
StringBuilder result = new StringBuilder ();
46
- boolean lastWasSpace = true ;
66
+ int lastType = 0 ; //0 whitespace, 1 whitespace sensitive, 2 char
47
67
for (int i = 0 ; i < query .length (); ++i ) {
48
68
char c = query .charAt (i );
49
69
if (Character .isWhitespace (c )) {
50
- if (!( lastWasSpace ) ) {
70
+ if (lastType == 2 ) {
51
71
result .append (' ' );
52
72
}
53
- lastWasSpace = true ;
73
+ lastType = 0 ;
54
74
} else if (!isWhiteSpaceSensitive (c )) {
55
- if (lastWasSpace && result .length () > 0 )
75
+ if (lastType == 0 && result .length () > 0 )
56
76
result .deleteCharAt (result .length () - 1 );
57
77
58
78
result .append (c );
59
- lastWasSpace = true ;
79
+ lastType = 1 ;
60
80
} else {
61
81
result .append (c );
62
- lastWasSpace = false ;
82
+ lastType = 2 ;
63
83
}
64
84
}
65
- return result .toString ().trim ();
85
+ return result .toString ().trim (). toLowerCase () ;
66
86
}
67
87
68
88
private static boolean isWhiteSpaceSensitive (char c ) {
0 commit comments