Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LangUtilTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.util;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Collection;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import java.util.StringTokenizer;
  20. import junit.framework.TestCase;
  21. import junit.textui.TestRunner;
  22. /**
  23. *
  24. * @author isberg
  25. */
  26. public class LangUtilTest extends TestCase {
  27. private static final String ME
  28. = "org.aspectj.testing.util.LangUtilTest";
  29. /** @param args ignored */
  30. public static void main(String[] args) {
  31. TestRunner.main(new String[] {ME});
  32. }
  33. /**
  34. * Constructor for LangUtilTest.
  35. * @param name
  36. */
  37. public LangUtilTest(String name) {
  38. super(name);
  39. }
  40. void check(String l, StringTokenizer st, int max, String delim) {
  41. for (int i = 0; i < max; i++) {
  42. if ((i > 0) && (null != delim)) {
  43. assertEquals(l, delim, st.nextToken());
  44. }
  45. assertEquals(l, ""+i, st.nextToken());
  46. }
  47. assertTrue(l, !st.hasMoreTokens());
  48. }
  49. void checkUnflatten(FTest test) {
  50. String[] exp = test.unflattened;
  51. ArrayList result = LangUtil.unflatten(test.toUnflatten, test.spec);
  52. String label = test + " -> " + result;
  53. assertNotNull(label, result);
  54. assertEquals(label, exp.length, result.size());
  55. for (int i = 0; i < exp.length; i++) {
  56. assertEquals(label, exp[i], result.get(i));
  57. }
  58. }
  59. public void skiptestUnflatten() {
  60. // LangUtil.FlattenSpec COMMA = LangUtil.FlattenSpec.COMMA;
  61. LangUtil.FlattenSpec LIST = LangUtil.FlattenSpec.LIST;
  62. FTest[] tests = new FTest[]
  63. { new FTest("[]", new String[0], LIST)
  64. , new FTest("[1]", new String[] {"1"}, LIST)
  65. , new FTest("[1, 2]", new String[] {"1", "2"}, LIST)
  66. , new FTest("[1,2]", new String[] {"1,2"}, LIST)
  67. , new FTest("[1, 2, 3]", new String[] {"1","2","3"}, LIST)
  68. };
  69. for (int i = 0; i < tests.length; i++) {
  70. checkUnflatten(tests[i]);
  71. }
  72. }
  73. public void testArrayList() {
  74. ArrayList l = new ArrayList();
  75. l.add(null);
  76. l.add(null);
  77. assertTrue(null == l.get(0));
  78. assertTrue(null == l.get(1));
  79. assertTrue(2 == l.size());
  80. assertEquals("[null, null]", "" + l);
  81. }
  82. public void testCombineStrings() {
  83. String[] one = new String[]{};
  84. String[] two = new String[]{};
  85. String[] expect = new String[]{};
  86. checkCombineStrings(one, two, expect);
  87. one = new String[]{null};
  88. two = new String[]{null};
  89. expect = new String[]{};
  90. checkCombineStrings(one, two, expect);
  91. one = new String[]{"1"};
  92. two = new String[]{null};
  93. expect = new String[]{"1"};
  94. checkCombineStrings(one, two, expect);
  95. one = new String[]{null};
  96. two = new String[]{"2"};
  97. expect = new String[]{"2"};
  98. checkCombineStrings(one, two, expect);
  99. one = new String[]{"1"};
  100. two = new String[]{"2"};
  101. expect = new String[]{"1", "2"};
  102. checkCombineStrings(one, two, expect);
  103. one = new String[]{null, null, "1", null, null};
  104. two = new String[]{null, "2", null};
  105. expect = new String[]{"1", "2"};
  106. checkCombineStrings(one, two, expect);
  107. one = new String[]{"1", "2", "3", "4"};
  108. two = new String[]{"5", null, "6"};
  109. expect = new String[]{"1", "2", "3", "4", "5", "6"};
  110. checkCombineStrings(one, two, expect);
  111. }
  112. void checkCombineStrings(String[] one, String[] two, String[] expected) {
  113. String[] actual = LangUtil.combine(one, two);
  114. String aString = LangUtil.arrayAsList(actual).toString();
  115. String eString = LangUtil.arrayAsList(expected).toString();
  116. String both = "actual=\"" + aString + "\" expected=\"" + eString + "\"";
  117. assertTrue(both, aString.equals(eString));
  118. }
  119. final String[] sABCDE = new String[] {"A", "B", "C", "D", "E" };
  120. final String[] sABC = new String[] {"A", "B", "C" };
  121. final String[] sDE = new String[] {"D", "E" };
  122. final String[] sabcde = new String[] {"a", "b", "c", "d", "e" };
  123. final String[] sabc = new String[] {"a", "b", "c" };
  124. final String[] sde = new String[] {"d", "e" };
  125. final String[] s12345 = new String[] {"1", "2", "3", "4", "5" };
  126. final String[] s13579 = new String[] {"1", "3", "5", "7", "9" };
  127. final String[] s02468 = new String[] {"0", "2", "4", "6", "8" };
  128. final String[] s135 = new String[] {"1", "3", "5" };
  129. final String[] s79 = new String[] {"7", "9" };
  130. final String[] s24 = new String[] {"2", "4" };
  131. final String[] s0 = new String[] {"0"};
  132. final String[] s068 = new String[] {"0", "6", "8" };
  133. final boolean unmodifiable = true;
  134. final boolean modifiable = false;
  135. final List lABCDE = makeList(unmodifiable, sABCDE);
  136. final List lABC = makeList(unmodifiable, sABC);
  137. final List lDE = makeList(unmodifiable, sDE);
  138. final List labcde = makeList(unmodifiable, sabcde);
  139. final List labc = makeList(unmodifiable, sabc);
  140. final List lde = makeList(unmodifiable, sde);
  141. final List l12345 = makeList(unmodifiable, s12345);
  142. final List l13579 = makeList(unmodifiable, s13579);
  143. final List l02468 = makeList(unmodifiable, s02468);
  144. final List l135 = makeList(unmodifiable, s135);
  145. final List l79 = makeList(unmodifiable, s79);
  146. final List l24 = makeList(unmodifiable, s24);
  147. final List l0 = makeList(unmodifiable, s0);
  148. final List l068 = makeList(unmodifiable, s068);
  149. final List rlabcde = makeList(modifiable, sabcde);
  150. final List rlabc = makeList(modifiable, sabc);
  151. final List rlde = makeList(modifiable, sde);
  152. final List rlABCDE = makeList(modifiable, sABCDE);
  153. final List rlABC = makeList(modifiable, sABC);
  154. final List rlDE = makeList(modifiable, sDE);
  155. final List rl12345 = makeList(modifiable, s12345);
  156. final List rl13579 = makeList(modifiable, s13579);
  157. final List rl02468 = makeList(modifiable, s02468);
  158. final List rl135 = makeList(modifiable, s135);
  159. final List rl79 = makeList(modifiable, s79);
  160. final List rl24 = makeList(modifiable, s24);
  161. final List rl0 = makeList(modifiable, s0);
  162. final List rl068 = makeList(modifiable, s068);
  163. final List NONE = Collections.EMPTY_LIST;
  164. {
  165. Collections.shuffle(rlABCDE);
  166. Collections.shuffle(rlABC);
  167. Collections.shuffle(rlDE);
  168. Collections.shuffle(rlabcde);
  169. Collections.shuffle(rlabc);
  170. Collections.shuffle(rlde);
  171. Collections.shuffle(rl12345);
  172. Collections.shuffle(rl13579);
  173. Collections.shuffle(rl02468);
  174. Collections.shuffle(rl135);
  175. Collections.shuffle(rl79);
  176. Collections.shuffle(rl24);
  177. Collections.shuffle(rl0);
  178. Collections.shuffle(rl068);
  179. }
  180. public void testDiffsEmptyIdentities() {
  181. checkDiff(l02468, null, l02468, NONE);
  182. checkDiff(null, l02468, NONE, l02468);
  183. checkDiff(l0, null, l0, NONE);
  184. checkDiff(null, l0, NONE, l0);
  185. checkDiff(l0, rl0, NONE, NONE);
  186. checkDiff(labc, rlabc, NONE, NONE);
  187. }
  188. public void testDiffsEmpties() {
  189. checkDiff(NONE, NONE, NONE, NONE);
  190. checkDiff(null, NONE, NONE, NONE);
  191. checkDiff(NONE, null, NONE, NONE);
  192. checkDiff(null, null, NONE, NONE);
  193. checkDiff(null, null, NONE, NONE);
  194. }
  195. public void testDiffsIdentities() {
  196. checkDiff(l02468, l02468, NONE, NONE);
  197. checkDiff(rl02468, l02468, NONE, NONE);
  198. checkDiff(l02468, rl02468, NONE, NONE);
  199. checkDiff(l13579, l13579, NONE, NONE);
  200. checkDiff(rl13579, l13579, NONE, NONE);
  201. checkDiff(l13579, rl13579, NONE, NONE);
  202. checkDiff(l13579, rl13579, NONE, NONE);
  203. }
  204. public void testDiffsEvens() {
  205. checkDiff(l02468, l12345, l068, l135);
  206. checkDiff(rl02468, rl12345, rl068, rl135);
  207. }
  208. public void testDiffsOdds() {
  209. checkDiff(l13579, l12345, l79, l24);
  210. checkDiff(rl13579, rl12345, rl79, rl24);
  211. checkDiff(l13579, rl12345, l79, rl24);
  212. checkDiff(rl13579, l12345, rl79, l24);
  213. }
  214. public void testSoftDiffs() {
  215. checkDiffSoft(labcde, lABCDE, NONE, NONE);
  216. checkDiffSoft(lABC, labc, NONE, NONE);
  217. checkDiffSoft(lABCDE, lABC, lDE, NONE);
  218. checkDiffSoft(lDE, lABCDE, NONE, lABC);
  219. checkDiffSoft(rlABCDE, rlABC, rlDE, NONE);
  220. checkDiffSoft(rlDE, rlABCDE, NONE, rlABC);
  221. checkDiffSoft(labcde, lABC, lDE, NONE);
  222. checkDiffSoft(lde, lABCDE, NONE, lABC);
  223. checkDiffSoft(rlabcde, rlABC, rlDE, NONE);
  224. checkDiffSoft(rlde, rlABCDE, NONE, rlABC);
  225. }
  226. // ---------------------- utilities
  227. List makeList(boolean unmodifiable, String[] ra) {
  228. if (unmodifiable) {
  229. return Collections.unmodifiableList(Arrays.asList(ra));
  230. } else {
  231. ArrayList list = new ArrayList();
  232. list.addAll(Arrays.asList(ra));
  233. return list;
  234. }
  235. }
  236. /** check both hard and soft - assuming list contain String */
  237. void checkDiff(List expected, List actual, List missing, List extra) {
  238. ArrayList extraOut = new ArrayList();
  239. ArrayList missingOut = new ArrayList();
  240. LangUtil.makeDiffs(expected, actual, missingOut, extraOut);
  241. checkSame(missing, missingOut);
  242. checkSame(extra, extraOut);
  243. extraOut.clear();
  244. missingOut.clear();
  245. LangUtil.makeSoftDiffs(expected, actual, missingOut, extraOut,
  246. String.CASE_INSENSITIVE_ORDER);
  247. checkSame(missing, missingOut); // XXX does not detect bad order
  248. checkSame(extra, extraOut);
  249. }
  250. void checkSame(Collection one, Collection two) { // just convert and string-compare?
  251. String label = one + "?=" + two;
  252. assertTrue(label, (null == one) == (null == two));
  253. if (null != one) {
  254. assertTrue(label, one.containsAll(two));
  255. assertTrue(label, two.containsAll(one));
  256. }
  257. }
  258. /** check only soft - assuming list contain String */
  259. void checkDiffSoft(List expected, List actual, List missing, List extra) {
  260. ArrayList extraOut = new ArrayList();
  261. ArrayList missingOut = new ArrayList();
  262. LangUtil.makeSoftDiffs(expected, actual, missingOut, extraOut,
  263. String.CASE_INSENSITIVE_ORDER);
  264. checkSameSoft(missing, missingOut);
  265. checkSameSoft(extra, extraOut);
  266. }
  267. /** @param one modifiable List of String
  268. * @param two modifiable List of String
  269. */
  270. void checkSameSoft(List one, List two) { // assume String
  271. String label = one + "?=" + two;
  272. assertTrue(label, (null == one) == (null == two));
  273. if (null != one) {
  274. ArrayList aone = new ArrayList();
  275. aone.addAll(one);
  276. ArrayList atwo = new ArrayList();
  277. aone.addAll(two);
  278. Collections.sort(aone);
  279. Collections.sort(atwo);
  280. String sone = (""+aone).toLowerCase();
  281. String stwo = (""+aone).toLowerCase();
  282. assertTrue(label, sone.equals(stwo));
  283. }
  284. }
  285. static class FTest {
  286. String toUnflatten;
  287. String[] unflattened;
  288. LangUtil.FlattenSpec spec;
  289. FTest(String in, String[] out, LangUtil.FlattenSpec spec) {
  290. toUnflatten = in;
  291. unflattened = out;
  292. this.spec = spec;
  293. }
  294. public String toString() {
  295. return "FTest("
  296. + "toUnflatten=" + toUnflatten
  297. + ", unflattened=" + Arrays.asList(unflattened)
  298. + ", spec=" + spec
  299. + ")";
  300. }
  301. }
  302. }