001: package com.mockrunner.test.jdbc;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import com.mockrunner.jdbc.SQLStatementMatcher;
009:
010: import junit.framework.TestCase;
011:
012: public class SQLStatementMatcherTest extends TestCase {
013: public void testGetMatchingObjects() {
014: Map testMap = new HashMap();
015: testMap.put("Test1", "TestObject1");
016: testMap.put("Test2", "TestObject2");
017: List testList = new ArrayList();
018: testList.add("TestList1");
019: testList.add("TestList2");
020: testList.add("TestList3");
021: testMap.put("Test4", testList);
022: SQLStatementMatcher matcher = new SQLStatementMatcher(true,
023: false);
024: List resultList = matcher.getMatchingObjects(testMap, "Test",
025: false, false);
026: assertEquals(3, resultList.size());
027: assertTrue(resultList.contains("TestObject1"));
028: assertTrue(resultList.contains("TestObject2"));
029: assertTrue(resultList.contains(testList));
030: }
031:
032: public void testGetMatchingObjectsRegEx() {
033: Map testMap = new HashMap();
034: testMap.put("Test1", "TestObject1");
035: testMap.put("Test2", "TestObject2");
036: SQLStatementMatcher matcher = new SQLStatementMatcher(true,
037: false, true);
038: List resultList = matcher.getMatchingObjects(testMap, "Test.*",
039: false, false);
040: assertEquals(2, resultList.size());
041: assertTrue(resultList.contains("TestObject1"));
042: assertTrue(resultList.contains("TestObject2"));
043: resultList = matcher.getMatchingObjects(testMap, "test.*",
044: false, false);
045: assertEquals(0, resultList.size());
046: }
047:
048: public void testGetMatchingObjectsResolveCollection() {
049: Map testMap = new HashMap();
050: testMap.put("Test1", "TestObject1");
051: testMap.put("Test2", "TestObject2");
052: testMap.put("Test3", "TestObject3");
053: testMap.put("Test", "TestObject");
054: SQLStatementMatcher matcher = new SQLStatementMatcher(true,
055: true);
056: List resultList = matcher.getMatchingObjects(testMap, "Test",
057: true, false);
058: assertTrue(resultList.size() == 1);
059: assertTrue(resultList.contains("TestObject"));
060: assertFalse(resultList.contains("TestObject1"));
061: assertFalse(resultList.contains("TestObject2"));
062: assertFalse(resultList.contains("TestObject3"));
063: matcher = new SQLStatementMatcher(true, false);
064: resultList = matcher.getMatchingObjects(testMap, "Test", true,
065: false);
066: assertTrue(resultList.size() == 4);
067: assertTrue(resultList.contains("TestObject"));
068: assertTrue(resultList.contains("TestObject1"));
069: assertTrue(resultList.contains("TestObject2"));
070: assertTrue(resultList.contains("TestObject3"));
071: matcher = new SQLStatementMatcher(true, false);
072: resultList = matcher.getMatchingObjects(testMap, "test", true,
073: false);
074: assertTrue(resultList.isEmpty());
075: matcher = new SQLStatementMatcher(false, false);
076: resultList = matcher.getMatchingObjects(testMap, "test", true,
077: false);
078: assertTrue(resultList.size() == 4);
079: assertTrue(resultList.contains("TestObject"));
080: assertTrue(resultList.contains("TestObject1"));
081: assertTrue(resultList.contains("TestObject2"));
082: assertTrue(resultList.contains("TestObject3"));
083: List testList = new ArrayList();
084: testList.add("TestList1");
085: testList.add("TestList2");
086: testList.add("TestList3");
087: testMap.put("Test4", testList);
088: matcher = new SQLStatementMatcher(true, false);
089: resultList = matcher.getMatchingObjects(testMap, "", true,
090: false);
091: assertTrue(resultList.size() == 7);
092: assertTrue(resultList.contains("TestObject"));
093: assertTrue(resultList.contains("TestObject1"));
094: assertTrue(resultList.contains("TestObject2"));
095: assertTrue(resultList.contains("TestObject3"));
096: assertTrue(resultList.contains("TestList1"));
097: assertTrue(resultList.contains("TestList2"));
098: assertTrue(resultList.contains("TestList3"));
099: matcher = new SQLStatementMatcher(true, false);
100: resultList = matcher
101: .getMatchingObjects(testMap, "", true, true);
102: assertTrue(resultList.isEmpty());
103: matcher = new SQLStatementMatcher(true, false);
104: resultList = matcher.getMatchingObjects(testMap,
105: "TestObjectObject", true, true);
106: assertTrue(resultList.size() == 1);
107: assertTrue(resultList.contains("TestObject"));
108: }
109:
110: public void testGetMatchingObjectsResolveCollectionRegEx() {
111: Map testMap = new HashMap();
112: testMap.put("Test1", "TestObject1");
113: testMap.put("Test2", "TestObject2");
114: List testList = new ArrayList();
115: testList.add("TestList1");
116: testList.add("TestList2");
117: testList.add("TestList3");
118: testMap.put("Test3", testList);
119: SQLStatementMatcher matcher = new SQLStatementMatcher(true,
120: false, true);
121: List resultList = matcher.getMatchingObjects(testMap, "[T].*",
122: true, false);
123: assertEquals(5, resultList.size());
124: }
125:
126: public void testContains() {
127: ArrayList list = new ArrayList();
128: list.add("TestString1");
129: list.add("TestString2");
130: list.add("TestString3");
131: SQLStatementMatcher matcher = new SQLStatementMatcher(false,
132: false);
133: assertTrue(matcher.contains(list, "TESTSTRING", false));
134: matcher = new SQLStatementMatcher(true, false);
135: assertFalse(matcher.contains(list, "TESTSTRING", false));
136: matcher = new SQLStatementMatcher(false, true);
137: assertFalse(matcher.contains(list, "TESTSTRING", false));
138: matcher = new SQLStatementMatcher(true, true);
139: assertTrue(matcher.contains(list, "TestString3", false));
140: matcher = new SQLStatementMatcher(true, true);
141: assertFalse(matcher.contains(list, "TestString4", false));
142: matcher = new SQLStatementMatcher(false, false);
143: assertFalse(matcher.contains(list, "TestString4", false));
144: matcher = new SQLStatementMatcher(false, false);
145: assertFalse(matcher.contains(list, "TESTSTRING1XYZ", false));
146: matcher = new SQLStatementMatcher(false, false);
147: assertTrue(matcher.contains(list, "TESTSTRING1XYZ", true));
148: matcher = new SQLStatementMatcher(true, false);
149: assertFalse(matcher.contains(list, "TEstString3Test", true));
150: matcher = new SQLStatementMatcher(false, false);
151: assertTrue(matcher.contains(list, "TEstString3Test", true));
152: }
153:
154: public void testContainsRegEx() {
155: ArrayList list = new ArrayList();
156: list.add("TestString1");
157: list.add("TestString2");
158: list.add("TestString3");
159: SQLStatementMatcher matcher = new SQLStatementMatcher(false,
160: false, true);
161: assertTrue(matcher.contains(list, "TESTSTRING.", false));
162: assertTrue(matcher.contains(list, ".*", false));
163: assertFalse(matcher.contains(list, "...", false));
164: list = new ArrayList();
165: list.add("TestString..");
166: assertTrue(matcher.contains(list, "TESTSTRING11", true));
167: assertFalse(matcher.contains(list, "TESTSTRING1", true));
168: }
169:
170: public void testDoStringsMatch() {
171: SQLStatementMatcher matcher = new SQLStatementMatcher(true,
172: false);
173: assertFalse(matcher.doStringsMatch("X", "x"));
174: matcher = new SQLStatementMatcher(false, true);
175: assertTrue(matcher.doStringsMatch("X", "x"));
176: matcher = new SQLStatementMatcher(false, false);
177: assertTrue(matcher.doStringsMatch("Test", "tes"));
178: matcher = new SQLStatementMatcher(true, false);
179: assertFalse(matcher.doStringsMatch("Test", "tes"));
180: matcher = new SQLStatementMatcher(true, false);
181: assertTrue(matcher.doStringsMatch("Test", ""));
182: matcher = new SQLStatementMatcher(true, false);
183: assertTrue(matcher.doStringsMatch("Test", null));
184: matcher = new SQLStatementMatcher(false, true);
185: assertFalse(matcher.doStringsMatch("Test", null));
186: matcher = new SQLStatementMatcher(true, true);
187: assertTrue(matcher.doStringsMatch(null, null));
188: matcher = new SQLStatementMatcher(true, true);
189: assertTrue(matcher.doStringsMatch("ThisIsATest", "ThisIsATest"));
190: }
191:
192: public void testDoStringsMatchRegEx() {
193: SQLStatementMatcher matcher = new SQLStatementMatcher(true,
194: false, true);
195: assertFalse(matcher.doStringsMatch("X", "x"));
196: assertTrue(matcher.doStringsMatch("AbcDef", ".*"));
197: assertFalse(matcher.doStringsMatch("AbcDef", "a.*"));
198: matcher = new SQLStatementMatcher(false, false, true);
199: assertTrue(matcher.doStringsMatch("AbcDef", "a.*"));
200: assertTrue(matcher.doStringsMatch("myTest", "[nmg]ytest"));
201: assertFalse(matcher.doStringsMatch("dyTest", "[nmg]ytest"));
202: matcher = new SQLStatementMatcher(true, true, true);
203: assertFalse(matcher.doStringsMatch("myTest", "[nmg]ytest"));
204: }
205: }
|