001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.testplugin;
011:
012: import java.io.BufferedReader;
013: import java.io.IOException;
014: import java.io.StringReader;
015: import java.util.ArrayList;
016: import java.util.Arrays;
017:
018: import junit.framework.Assert;
019:
020: /**
021: *
022: */
023: public class StringAsserts {
024: /**
025: *
026: */
027: public StringAsserts() {
028: super ();
029: }
030:
031: private static int getDiffPos(String str1, String str2) {
032: int len1 = Math.min(str1.length(), str2.length());
033:
034: int diffPos = -1;
035: for (int i = 0; i < len1; i++) {
036: if (str1.charAt(i) != str2.charAt(i)) {
037: diffPos = i;
038: break;
039: }
040: }
041: if (diffPos == -1 && str1.length() != str2.length()) {
042: diffPos = len1;
043: }
044: return diffPos;
045: }
046:
047: private static final int printRange = 6;
048:
049: public static void assertEqualString(String actual, String expected) {
050: if (actual == null || expected == null) {
051: if (actual == expected) {
052: return;
053: }
054: if (actual == null) {
055: Assert.assertTrue(
056: "Content not as expected: is 'null' expected: "
057: + expected, false);
058: } else {
059: Assert.assertTrue(
060: "Content not as expected: expected 'null' is: "
061: + actual, false);
062: }
063: }
064:
065: int diffPos = getDiffPos(actual, expected);
066: if (diffPos != -1) {
067: int diffAhead = Math.max(0, diffPos - printRange);
068: int diffAfter = Math.min(actual.length(), diffPos
069: + printRange);
070:
071: String diffStr = actual.substring(diffAhead, diffPos) + '^'
072: + actual.substring(diffPos, diffAfter);
073:
074: // use detailed message
075: String message = "Content not as expected: is\n" + actual + "\nDiffers at pos " + diffPos + ": " + diffStr + "\nexpected:\n" + expected; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
076:
077: Assert.assertEquals(message, expected, actual);
078: }
079: }
080:
081: public static void assertEqualStringIgnoreDelim(String actual,
082: String expected) throws IOException {
083: if (actual == null || expected == null) {
084: if (actual == expected) {
085: return;
086: }
087: if (actual == null) {
088: Assert.assertTrue(
089: "Content not as expected: is 'null' expected: "
090: + expected, false);
091: } else {
092: Assert.assertTrue(
093: "Content not as expected: expected 'null' is: "
094: + actual, false);
095: }
096: }
097:
098: BufferedReader read1 = new BufferedReader(new StringReader(
099: actual));
100: BufferedReader read2 = new BufferedReader(new StringReader(
101: expected));
102:
103: int line = 1;
104: do {
105: String s1 = read1.readLine();
106: String s2 = read2.readLine();
107:
108: if (s1 == null || !s1.equals(s2)) {
109: if (s1 == null && s2 == null) {
110: return;
111: }
112: String diffStr = (s1 == null) ? s2 : s1;
113:
114: String message = "Content not as expected: Content is: \n"
115: + actual
116: + "\nDiffers at line "
117: + line
118: + ": "
119: + diffStr
120: + "\nExpected contents: \n"
121: + expected;
122: Assert.assertEquals(message, expected, actual);
123: }
124: line++;
125: } while (true);
126: }
127:
128: public static void assertEqualStringsIgnoreOrder(String[] actuals,
129: String[] expecteds) {
130: ArrayList list1 = new ArrayList(Arrays.asList(actuals));
131: ArrayList list2 = new ArrayList(Arrays.asList(expecteds));
132:
133: for (int i = list1.size() - 1; i >= 0; i--) {
134: if (list2.remove(list1.get(i))) {
135: list1.remove(i);
136: }
137: }
138:
139: int n1 = list1.size();
140: int n2 = list2.size();
141:
142: if (n1 + n2 > 0) {
143: if (n1 == 1 && n2 == 1) {
144: assertEqualString((String) list1.get(0), (String) list2
145: .get(0));
146: }
147:
148: StringBuffer buf = new StringBuffer();
149: for (int i = 0; i < n1; i++) {
150: String s1 = (String) list1.get(i);
151: if (s1 != null) {
152: buf.append(s1);
153: buf.append("\n");
154: }
155: }
156: String actual = buf.toString();
157:
158: buf = new StringBuffer();
159: for (int i = 0; i < n2; i++) {
160: String s2 = (String) list2.get(i);
161: if (s2 != null) {
162: buf.append(s2);
163: buf.append("\n");
164: }
165: }
166: String expected = buf.toString();
167:
168: String message = "Content not as expected: Content is: \n"
169: + actual + "\nExpected contents: \n" + expected;
170: Assert.assertEquals(message, expected, actual);
171: }
172: }
173:
174: public static void assertExpectedExistInProposals(String[] actuals,
175: String[] expecteds) {
176: ArrayList list1 = new ArrayList(Arrays.asList(actuals));
177: ArrayList list2 = new ArrayList(Arrays.asList(expecteds));
178:
179: for (int i = list1.size() - 1; i >= 0; i--) {
180: if (list2.remove(list1.get(i))) {
181: list1.remove(i);
182: }
183: }
184:
185: int n1 = list1.size();
186: int n2 = list2.size();
187:
188: if (n2 > 0) {
189: if (n1 == 1 && n2 == 1) {
190: assertEqualString((String) list1.get(0), (String) list2
191: .get(0));
192: }
193:
194: StringBuffer buf = new StringBuffer();
195: for (int i = 0; i < n1; i++) {
196: String s1 = (String) list1.get(i);
197: if (s1 != null) {
198: buf.append(s1);
199: buf.append("\n");
200: }
201: }
202: String actual = buf.toString();
203:
204: buf = new StringBuffer();
205: for (int i = 0; i < n2; i++) {
206: String s2 = (String) list2.get(i);
207: if (s2 != null) {
208: buf.append(s2);
209: buf.append("\n");
210: }
211: }
212: String expected = buf.toString();
213:
214: String message = "Content not as expected: Content is: \n"
215: + actual + "\nExpected contents: \n" + expected;
216: Assert.assertEquals(message, expected, actual);
217: }
218: }
219:
220: }
|