001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util;
038:
039: import edu.rice.cs.drjava.DrJavaTestCase;
040:
041: import edu.rice.cs.plt.tuple.Pair;
042: import java.io.PrintWriter;
043:
044: /**
045: * Test functions of StringOps.
046: */
047: public class StringOpsTest extends DrJavaTestCase {
048: /**
049: * Test the replace() method of StringOps class
050: */
051: public void testReplace() {
052: String test = "aabbccdd";
053: assertEquals("testReplace:", "aab12cdd", StringOps.replace(
054: test, "bc", "12"));
055: test = "cabcabc";
056: assertEquals("testReplace:", "cabc", StringOps.replace(test,
057: "cabc", "c"));
058: }
059:
060: /**
061: * Test the getOffsetAndLength() method of StringOps class
062: */
063: public void testGetOffsetAndLength() {
064: String test = "123456789\n123456789\n123456789\n";
065:
066: // The offset is always one less than the first row/col
067: // The length includes the start and end positions
068: Pair<Integer, Integer> oAndL = StringOps.getOffsetAndLength(
069: test, 1, 1, 1, 9);
070: assertEquals("testGetOffsetAndLength- offSet:", new Integer(0),
071: oAndL.first());
072: assertEquals("testGetOffsetAndLength- length:", new Integer(9),
073: oAndL.second());
074:
075: oAndL = StringOps.getOffsetAndLength(test, 1, 1, 2, 3);
076: assertEquals("testGetOffsetAndLength- offSet:", new Integer(0),
077: oAndL.first());
078: assertEquals("testGetOffsetAndLength- length:",
079: new Integer(13), oAndL.second());
080:
081: oAndL = StringOps.getOffsetAndLength(test, 1, 5, 2, 3);
082: assertEquals("testGetOffsetAndLength- offSet:", new Integer(4),
083: oAndL.first());
084: assertEquals("testGetOffsetAndLength- length:", new Integer(9),
085: oAndL.second());
086:
087: oAndL = StringOps.getOffsetAndLength(test, 1, 1, 1, 1);
088: assertEquals("testGetOffsetAndLength- offSet:", new Integer(0),
089: oAndL.first());
090: assertEquals("testGetOffsetAndLength- length:", new Integer(1),
091: oAndL.second());
092:
093: oAndL = StringOps.getOffsetAndLength(test, 3, 5, 3, 5);
094: assertEquals("testGetOffsetAndLength- offSet:",
095: new Integer(24), oAndL.first());
096: assertEquals("testGetOffsetAndLength- length:", new Integer(1),
097: oAndL.second());
098:
099: oAndL = StringOps.getOffsetAndLength(test, 2, 3, 3, 6);
100: assertEquals("testGetOffsetAndLength- offSet:",
101: new Integer(12), oAndL.first());
102: assertEquals("testGetOffsetAndLength- length:",
103: new Integer(14), oAndL.second());
104:
105: try {
106: StringOps.getOffsetAndLength(test, 3, 2, 2, 3);
107: fail("Should not have been able to compute offset where startRow > endRow");
108: } catch (IllegalArgumentException ex) {
109: // correct behavior
110: }
111:
112: try {
113: StringOps.getOffsetAndLength(test, 2, 4, 2, 3);
114: fail("Should not have been able to compute offset where start > end");
115: } catch (IllegalArgumentException ex) {
116: // correct behavior
117: }
118:
119: try {
120: StringOps.getOffsetAndLength(test, 4, 4, 5, 5);
121: fail("Should not have been able to compute offset where the\n"
122: + "given coordinates are not contained within the string");
123: } catch (IllegalArgumentException ex) {
124: // correct behavior
125: }
126:
127: try {
128: StringOps.getOffsetAndLength(test, 3, 4, 3, 12);
129: fail("Should not have been able to compute offset where the\n"
130: + "given coordinates are not contained within the string");
131: } catch (IllegalArgumentException ex) {
132: // correct behavior
133: }
134:
135: try {
136: StringOps.getOffsetAndLength(test, 2, 15, 3, 1);
137: fail("Should not have been able to compute offset where the\n"
138: + "given coordinates are not contained within the string");
139: } catch (IllegalArgumentException ex) {
140: // correct behavior
141: }
142: }
143:
144: /**
145: * Tests that getting the stack trace of a throwable works correctly.
146: */
147: public void testGetStackTrace() {
148: final String trace = "hello";
149: Throwable t = new Throwable() {
150: public void printStackTrace(PrintWriter w) {
151: w.print(trace);
152: }
153: };
154: assertEquals("Should have returned the correct stack trace!",
155: trace, StringOps.getStackTrace(t));
156: }
157:
158: /**
159: * Tests converting a string to a literal
160: */
161: public void testConvertToLiteral() {
162: String toConvert = " a b c d";
163: String expResult = "\" a b c d\"";
164: String actResult = StringOps.convertToLiteral(toConvert);
165: assertEquals("converting " + toConvert + " should yield "
166: + expResult, expResult, actResult);
167:
168: toConvert = "\\ hello world \\";
169: expResult = "\"\\\\ hello world \\\\\"";
170: actResult = StringOps.convertToLiteral(toConvert);
171: assertEquals("converting " + toConvert + " should yield "
172: + expResult, expResult, actResult);
173:
174: toConvert = "\\\n\\n";
175: expResult = "\"\\\\\\n\\\\n\"";
176: actResult = StringOps.convertToLiteral(toConvert);
177: assertEquals("converting " + toConvert + " should yield "
178: + expResult, expResult, actResult);
179:
180: toConvert = "\\\"\t\\t";
181: expResult = "\"\\\\\\\"\\t\\\\t\"";
182: actResult = StringOps.convertToLiteral(toConvert);
183: assertEquals("converting " + toConvert + " should yield "
184: + expResult, expResult, actResult);
185:
186: toConvert = "\"\\\"\t\\n\n\\\n\"";
187: expResult = "\"\\\"\\\\\\\"\\t\\\\n\\n\\\\\\n\\\"\"";
188: actResult = StringOps.convertToLiteral(toConvert);
189: assertEquals("converting " + toConvert + " should yield "
190: + expResult, expResult, actResult);
191:
192: toConvert = " ";
193: expResult = "\" \"";
194: actResult = StringOps.convertToLiteral(toConvert);
195: assertEquals("converting " + toConvert + " should yield "
196: + expResult, expResult, actResult);
197: }
198:
199: private static class TestGetSimpleNameInner {
200: public static class Nested {
201: public static Class anonClass() {
202: java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
203: public void actionPerformed(
204: java.awt.event.ActionEvent e) {
205: }
206: };
207: return l.getClass();
208: }
209: }
210:
211: public class Inner {
212: public Class anonClass() {
213: java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
214: public void actionPerformed(
215: java.awt.event.ActionEvent e) {
216: }
217: };
218: return l.getClass();
219: }
220: }
221:
222: public Inner getInner() {
223: return new Inner();
224: }
225:
226: public static Class anonClass() {
227: java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
228: public void actionPerformed(java.awt.event.ActionEvent e) {
229: }
230: };
231: return l.getClass();
232: }
233:
234: public static Lambda<Class, Object> getLambda() {
235: return new Lambda<Class, Object>() {
236: public Class apply(Object param) {
237: java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
238: public void actionPerformed(
239: java.awt.event.ActionEvent e) {
240: }
241: };
242: return l.getClass();
243: }
244: };
245: }
246: }
247:
248: /**
249: * Tests for getting the simple name of a class.
250: */
251: public void testGetSimpleName() {
252: String exp = "Integer";
253: String act = StringOps.getSimpleName(java.lang.Integer.class);
254: assertEquals("Wrong simple name for java.lang.Integer, exp="
255: + exp + ", act=" + act, exp, act);
256:
257: exp = "TestGetSimpleNameInner";
258: act = StringOps.getSimpleName(TestGetSimpleNameInner.class);
259: assertEquals(
260: "Wrong simple name for TestGetSimpleNameInner, exp="
261: + exp + ", act=" + act, exp, act);
262:
263: exp = "Nested";
264: act = StringOps
265: .getSimpleName(TestGetSimpleNameInner.Nested.class);
266: assertEquals(
267: "Wrong simple name for TestGetSimpleNameInner.Nested, exp="
268: + exp + ", act=" + act, exp, act);
269:
270: exp = "Inner";
271: act = StringOps
272: .getSimpleName(TestGetSimpleNameInner.Inner.class);
273: assertEquals(
274: "Wrong simple name for TestGetSimpleNameInner.Inner, exp="
275: + exp + ", act=" + act, exp, act);
276:
277: java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
278: public void actionPerformed(java.awt.event.ActionEvent e) {
279: }
280: };
281:
282: exp = "";
283: act = StringOps.getSimpleName(l.getClass());
284: assertEquals(
285: "Wrong simple name for anonymous inner class, exp="
286: + exp + ", act=" + act, exp, act);
287:
288: exp = "";
289: act = StringOps.getSimpleName(TestGetSimpleNameInner
290: .anonClass());
291: assertEquals(
292: "Wrong simple name for anonymous inner class, exp="
293: + exp + ", act=" + act, exp, act);
294:
295: exp = "";
296: act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested
297: .anonClass());
298: assertEquals(
299: "Wrong simple name for anonymous inner class, exp="
300: + exp + ", act=" + act, exp, act);
301:
302: exp = "";
303: act = StringOps.getSimpleName((new TestGetSimpleNameInner())
304: .getInner().anonClass());
305: assertEquals(
306: "Wrong simple name for anonymous inner class, exp="
307: + exp + ", act=" + act, exp, act);
308:
309: exp = "";
310: act = StringOps.getSimpleName(TestGetSimpleNameInner
311: .getLambda().apply(null));
312: assertEquals(
313: "Wrong simple name for anonymous inner class, exp="
314: + exp + ", act=" + act, exp, act);
315: }
316:
317: /** Tests for getting the simple name of a class. Works by comparing with Java 1.5.0's Class.getSimpleName().
318: * This test is commented out to remove it from regular unit testing because it is incompatible with Java 1.4.
319: */
320: // public void testGetSimpleName15() {
321: // String exp = java.lang.Integer.class.getSimpleName();
322: // String act = StringOps.getSimpleName(java.lang.Integer.class);
323: // assertEquals("Wrong simple name for java.lang.Integer, exp="+exp+", act="+act,
324: // exp,
325: // act);
326: //
327: // exp = TestGetSimpleNameInner.class.getSimpleName();
328: // act = StringOps.getSimpleName(TestGetSimpleNameInner.class);
329: // assertEquals("Wrong simple name for TestGetSimpleNameInner, exp="+exp+", act="+act,
330: // exp,
331: // act);
332: //
333: // exp = TestGetSimpleNameInner.Nested.class.getSimpleName();
334: // act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.class);
335: // assertEquals("Wrong simple name for TestGetSimpleNameInner.Nested, exp="+exp+", act="+act,
336: // exp,
337: // act);
338: //
339: // exp = TestGetSimpleNameInner.Inner.class.getSimpleName();
340: // act = StringOps.getSimpleName(TestGetSimpleNameInner.Inner.class);
341: // assertEquals("Wrong simple name for TestGetSimpleNameInner.Inner, exp="+exp+", act="+act,
342: // exp,
343: // act);
344: //
345: // java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
346: // public void actionPerformed(java.awt.event.ActionEvent e) { }
347: // };
348: //
349: // exp = l.getClass().getSimpleName();
350: // act = StringOps.getSimpleName(l.getClass());
351: // assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
352: // exp,
353: // act);
354: //
355: // exp = TestGetSimpleNameInner.anonClass().getSimpleName();
356: // act = StringOps.getSimpleName(TestGetSimpleNameInner.anonClass());
357: // assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
358: // exp,
359: // act);
360: //
361: // exp = TestGetSimpleNameInner.Nested.anonClass().getSimpleName();
362: // act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.anonClass());
363: // assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
364: // exp,
365: // act);
366: //
367: // exp = (new TestGetSimpleNameInner()).getInner().anonClass().getSimpleName();
368: // act = StringOps.getSimpleName((new TestGetSimpleNameInner()).getInner().anonClass());
369: // assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
370: // exp,
371: // act);
372: //
373: // exp = TestGetSimpleNameInner.getLambda().apply(null).getSimpleName();
374: // act = StringOps.getSimpleName(TestGetSimpleNameInner.getLambda().apply(null));
375: // assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
376: // exp,
377: // act);
378: // }
379: public void testToStringLong() {
380: long[] a = null;
381: assertEquals("null", StringOps.toString(a));
382: assertEquals("[]", StringOps.toString(new long[] {}));
383: assertEquals("[1]", StringOps.toString(new long[] { 1 }));
384: assertEquals("[1, 2]", StringOps.toString(new long[] { 1, 2 }));
385: }
386:
387: public void testToStringInt() {
388: int[] a = null;
389: assertEquals("null", StringOps.toString(a));
390: assertEquals("[]", StringOps.toString(new int[] {}));
391: assertEquals("[1]", StringOps.toString(new int[] { 1 }));
392: assertEquals("[1, 2]", StringOps.toString(new int[] { 1, 2 }));
393: }
394:
395: public void testToStringShort() {
396: short[] a = null;
397: assertEquals("null", StringOps.toString(a));
398: assertEquals("[]", StringOps.toString(new short[] {}));
399: assertEquals("[1]", StringOps.toString(new short[] { 1 }));
400: assertEquals("[1, 2]", StringOps.toString(new short[] { 1, 2 }));
401: }
402:
403: public void testToStringChar() {
404: char[] a = null;
405: assertEquals("null", StringOps.toString(a));
406: assertEquals("[]", StringOps.toString(new char[] {}));
407: assertEquals("[a]", StringOps.toString(new char[] { 'a' }));
408: assertEquals("[a, b]", StringOps
409: .toString(new char[] { 'a', 'b' }));
410: }
411:
412: public void testToStringByte() {
413: byte[] a = null;
414: assertEquals("null", StringOps.toString(a));
415: assertEquals("[]", StringOps.toString(new byte[] {}));
416: assertEquals("[1]", StringOps.toString(new byte[] { 1 }));
417: assertEquals("[1, 2]", StringOps.toString(new byte[] { 1, 2 }));
418: }
419:
420: public void testToStringBoolean() {
421: boolean[] a = null;
422: assertEquals("null", StringOps.toString(a));
423: assertEquals("[]", StringOps.toString(new boolean[] {}));
424: assertEquals("[true]", StringOps
425: .toString(new boolean[] { true }));
426: assertEquals("[true, false]", StringOps.toString(new boolean[] {
427: true, false }));
428: }
429:
430: public void testToStringFloat() {
431: float[] a = null;
432: assertEquals("null", StringOps.toString(a));
433: assertEquals("[]", StringOps.toString(new float[] {}));
434: assertEquals("[1.23]", StringOps
435: .toString(new float[] { 1.23f }));
436: assertEquals("[1.23, 4.56]", StringOps.toString(new float[] {
437: 1.23f, 4.56f }));
438: }
439:
440: public void testToStringDouble() {
441: double[] a = null;
442: assertEquals("null", StringOps.toString(a));
443: assertEquals("[]", StringOps.toString(new double[] {}));
444: assertEquals("[1.23]", StringOps
445: .toString(new double[] { 1.23 }));
446: assertEquals("[1.23, 4.56]", StringOps.toString(new double[] {
447: 1.23, 4.56 }));
448: }
449:
450: public void testToStringObject() {
451: Object[] a = null;
452: assertEquals("null", StringOps.toString(a));
453: assertEquals("[]", StringOps.toString(new Object[] {}));
454: assertEquals("[123]", StringOps
455: .toString(new Object[] { "123" }));
456: assertEquals("[123, 123]", StringOps.toString(new Object[] {
457: "123", new Integer(123) }));
458: }
459:
460: public void testMemSizeToString() {
461: assertEquals("<1024", "500 bytes", StringOps
462: .memSizeToString(500));
463: assertEquals("1KB", "1 kilobyte", StringOps
464: .memSizeToString(1024));
465: assertEquals("2KB", "2 kilobytes", StringOps
466: .memSizeToString(1024 * 2));
467: assertEquals("1.5KB", "1.50 kilobytes", StringOps
468: .memSizeToString((long) (1024 * 1.5)));
469: assertEquals("1MB", "1 megabyte", StringOps
470: .memSizeToString((1024 * 1024)));
471: assertEquals("2MB", "2 megabytes", StringOps
472: .memSizeToString((1024 * 1024 * 2)));
473: assertEquals("1.1MB", "1.10 megabytes", StringOps
474: .memSizeToString((long) (1024 * 1024 * 1.1)));
475: assertEquals("1GB", "1 gigabyte", StringOps
476: .memSizeToString((1024 * 1024 * 1024)));
477: assertEquals("1.25GB", "1.25 gigabytes", StringOps
478: .memSizeToString((long) (1024 * 1024 * 1024 * 1.25)));
479: }
480: }
|