KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > StringOpsTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import edu.rice.cs.drjava.DrJavaTestCase;
37
38 import edu.rice.cs.plt.tuple.Pair;
39 import java.io.PrintWriter JavaDoc;
40
41 /**
42  * Test functions of StringOps.
43  */

44 public class StringOpsTest extends DrJavaTestCase {
45   /**
46    * Test the replace() method of StringOps class
47    */

48   public void testReplace() {
49     String JavaDoc test = "aabbccdd";
50     assertEquals("testReplace:", "aab12cdd", StringOps.replace(test, "bc", "12"));
51     test = "cabcabc";
52     assertEquals("testReplace:", "cabc", StringOps.replace(test, "cabc", "c"));
53   }
54
55   /**
56    * Test the getOffsetAndLength() method of StringOps class
57    */

58   public void testGetOffsetAndLength() {
59     String JavaDoc test = "123456789\n123456789\n123456789\n";
60
61     // The offset is always one less than the first row/col
62
// The length includes the start and end positions
63
Pair<Integer JavaDoc,Integer JavaDoc> oAndL = StringOps.getOffsetAndLength(test, 1, 1, 1, 9);
64     assertEquals("testGetOffsetAndLength- offSet:", new Integer JavaDoc(0), oAndL.first());
65     assertEquals("testGetOffsetAndLength- length:", new Integer JavaDoc(9), oAndL.second());
66
67     oAndL = StringOps.getOffsetAndLength(test, 1, 1, 2, 3);
68     assertEquals("testGetOffsetAndLength- offSet:", new Integer JavaDoc(0), oAndL.first());
69     assertEquals("testGetOffsetAndLength- length:", new Integer JavaDoc(13), oAndL.second());
70
71     oAndL = StringOps.getOffsetAndLength(test, 1, 5, 2, 3);
72     assertEquals("testGetOffsetAndLength- offSet:", new Integer JavaDoc(4), oAndL.first());
73     assertEquals("testGetOffsetAndLength- length:", new Integer JavaDoc(9), oAndL.second());
74
75     oAndL = StringOps.getOffsetAndLength(test, 1, 1, 1, 1);
76     assertEquals("testGetOffsetAndLength- offSet:", new Integer JavaDoc(0), oAndL.first());
77     assertEquals("testGetOffsetAndLength- length:", new Integer JavaDoc(1), oAndL.second());
78
79     oAndL = StringOps.getOffsetAndLength(test, 3, 5, 3, 5);
80     assertEquals("testGetOffsetAndLength- offSet:", new Integer JavaDoc(24), oAndL.first());
81     assertEquals("testGetOffsetAndLength- length:", new Integer JavaDoc(1), oAndL.second());
82
83     oAndL = StringOps.getOffsetAndLength(test, 2, 3, 3, 6);
84     assertEquals("testGetOffsetAndLength- offSet:", new Integer JavaDoc(12), oAndL.first());
85     assertEquals("testGetOffsetAndLength- length:", new Integer JavaDoc(14), oAndL.second());
86
87     try {
88       StringOps.getOffsetAndLength(test, 3, 2, 2, 3);
89       fail("Should not have been able to compute offset where startRow > endRow");
90     }
91     catch (IllegalArgumentException JavaDoc ex) {
92       // correct behavior
93
}
94
95     try {
96       StringOps.getOffsetAndLength(test, 2, 4, 2, 3);
97       fail("Should not have been able to compute offset where start > end");
98     }
99     catch (IllegalArgumentException JavaDoc ex) {
100       // correct behavior
101
}
102
103     try {
104       StringOps.getOffsetAndLength(test, 4, 4, 5, 5);
105       fail("Should not have been able to compute offset where the\n" +
106            "given coordinates are not contained within the string");
107     }
108     catch (IllegalArgumentException JavaDoc ex) {
109       // correct behavior
110
}
111
112     try {
113       StringOps.getOffsetAndLength(test, 3, 4, 3, 12);
114       fail("Should not have been able to compute offset where the\n" +
115            "given coordinates are not contained within the string");
116     }
117     catch (IllegalArgumentException JavaDoc ex) {
118       // correct behavior
119
}
120
121     try {
122       StringOps.getOffsetAndLength(test, 2, 15, 3, 1);
123       fail("Should not have been able to compute offset where the\n" +
124            "given coordinates are not contained within the string");
125     }
126     catch (IllegalArgumentException JavaDoc ex) {
127       // correct behavior
128
}
129   }
130
131   /**
132    * Tests that getting the stack trace of a throwable works correctly.
133    */

134   public void testGetStackTrace() {
135     final String JavaDoc trace = "hello";
136     Throwable JavaDoc t = new Throwable JavaDoc() {
137       public void printStackTrace(PrintWriter JavaDoc w) {
138         w.print(trace);
139       }
140     };
141     assertEquals("Should have returned the correct stack trace!", trace, StringOps.getStackTrace(t));
142   }
143
144   /**
145    * Tests converting a string to a literal
146    */

147   public void testConvertToLiteral() {
148     String JavaDoc toConvert = " a b c d";
149     String JavaDoc expResult = "\" a b c d\"";
150     String JavaDoc actResult = StringOps.convertToLiteral(toConvert);
151     assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult);
152
153     toConvert = "\\ hello world \\";
154     expResult = "\"\\\\ hello world \\\\\"";
155     actResult = StringOps.convertToLiteral(toConvert);
156     assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult);
157
158     toConvert = "\\\n\\n";
159     expResult = "\"\\\\\\n\\\\n\"";
160     actResult = StringOps.convertToLiteral(toConvert);
161     assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult);
162
163     toConvert = "\\\"\t\\t";
164     expResult = "\"\\\\\\\"\\t\\\\t\"";
165     actResult = StringOps.convertToLiteral(toConvert);
166     assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult);
167
168     toConvert = "\"\\\"\t\\n\n\\\n\"";
169     expResult = "\"\\\"\\\\\\\"\\t\\\\n\\n\\\\\\n\\\"\"";
170     actResult = StringOps.convertToLiteral(toConvert);
171     assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult);
172
173     toConvert = " ";
174     expResult = "\" \"";
175     actResult = StringOps.convertToLiteral(toConvert);
176     assertEquals("converting "+toConvert+" should yield "+ expResult, expResult, actResult);
177   }
178   
179   private static class TestGetSimpleNameInner {
180     public static class Nested {
181       public static Class JavaDoc anonClass() {
182         java.awt.event.ActionListener JavaDoc l = new java.awt.event.ActionListener JavaDoc() {
183           public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) { }
184         };
185         return l.getClass();
186       }
187     }
188     public class Inner {
189       public Class JavaDoc anonClass() {
190         java.awt.event.ActionListener JavaDoc l = new java.awt.event.ActionListener JavaDoc() {
191           public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) { }
192         };
193         return l.getClass();
194       }
195     }
196     public Inner getInner() {
197       return new Inner();
198     }
199     public static Class JavaDoc anonClass() {
200       java.awt.event.ActionListener JavaDoc l = new java.awt.event.ActionListener JavaDoc() {
201         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) { }
202       };
203       return l.getClass();
204     }
205     public static Lambda<Class JavaDoc, Object JavaDoc> getLambda() {
206       return new Lambda<Class JavaDoc, Object JavaDoc>() {
207         public Class JavaDoc apply(Object JavaDoc param) {
208           java.awt.event.ActionListener JavaDoc l = new java.awt.event.ActionListener JavaDoc() {
209             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) { }
210           };
211           return l.getClass();
212         }
213       };
214     }
215   }
216
217   /**
218    * Tests for getting the simple name of a class.
219    */

220   public void testGetSimpleName() {
221     String JavaDoc exp = "Integer";
222     String JavaDoc act = StringOps.getSimpleName(java.lang.Integer JavaDoc.class);
223     assertEquals("Wrong simple name for java.lang.Integer, exp="+exp+", act="+act,
224                  exp,
225                  act);
226     
227     exp = "TestGetSimpleNameInner";
228     act = StringOps.getSimpleName(TestGetSimpleNameInner.class);
229     assertEquals("Wrong simple name for TestGetSimpleNameInner, exp="+exp+", act="+act,
230                  exp,
231                  act);
232     
233     exp = "Nested";
234     act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.class);
235     assertEquals("Wrong simple name for TestGetSimpleNameInner.Nested, exp="+exp+", act="+act,
236                  exp,
237                  act);
238     
239     exp = "Inner";
240     act = StringOps.getSimpleName(TestGetSimpleNameInner.Inner.class);
241     assertEquals("Wrong simple name for TestGetSimpleNameInner.Inner, exp="+exp+", act="+act,
242                  exp,
243                  act);
244     
245     java.awt.event.ActionListener JavaDoc l = new java.awt.event.ActionListener JavaDoc() {
246       public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) { }
247     };
248     
249     exp = "";
250     act = StringOps.getSimpleName(l.getClass());
251     assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
252                  exp,
253                  act);
254     
255     exp = "";
256     act = StringOps.getSimpleName(TestGetSimpleNameInner.anonClass());
257     assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
258                  exp,
259                  act);
260     
261     exp = "";
262     act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.anonClass());
263     assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
264                  exp,
265                  act);
266     
267     exp = "";
268     act = StringOps.getSimpleName((new TestGetSimpleNameInner()).getInner().anonClass());
269     assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
270                  exp,
271                  act);
272     
273     exp = "";
274     act = StringOps.getSimpleName(TestGetSimpleNameInner.getLambda().apply(null));
275     assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
276                  exp,
277                  act);
278   }
279   
280   /** Tests for getting the simple name of a class. Works by comparing with Java 1.5.0's Class.getSimpleName().
281    * This test is commented out to remove it from regular unit testing because it is incompatible with Java 1.4.
282    */

283 // public void testGetSimpleName15() {
284
// String exp = java.lang.Integer.class.getSimpleName();
285
// String act = StringOps.getSimpleName(java.lang.Integer.class);
286
// assertEquals("Wrong simple name for java.lang.Integer, exp="+exp+", act="+act,
287
// exp,
288
// act);
289
//
290
// exp = TestGetSimpleNameInner.class.getSimpleName();
291
// act = StringOps.getSimpleName(TestGetSimpleNameInner.class);
292
// assertEquals("Wrong simple name for TestGetSimpleNameInner, exp="+exp+", act="+act,
293
// exp,
294
// act);
295
//
296
// exp = TestGetSimpleNameInner.Nested.class.getSimpleName();
297
// act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.class);
298
// assertEquals("Wrong simple name for TestGetSimpleNameInner.Nested, exp="+exp+", act="+act,
299
// exp,
300
// act);
301
//
302
// exp = TestGetSimpleNameInner.Inner.class.getSimpleName();
303
// act = StringOps.getSimpleName(TestGetSimpleNameInner.Inner.class);
304
// assertEquals("Wrong simple name for TestGetSimpleNameInner.Inner, exp="+exp+", act="+act,
305
// exp,
306
// act);
307
//
308
// java.awt.event.ActionListener l = new java.awt.event.ActionListener() {
309
// public void actionPerformed(java.awt.event.ActionEvent e) { }
310
// };
311
//
312
// exp = l.getClass().getSimpleName();
313
// act = StringOps.getSimpleName(l.getClass());
314
// assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
315
// exp,
316
// act);
317
//
318
// exp = TestGetSimpleNameInner.anonClass().getSimpleName();
319
// act = StringOps.getSimpleName(TestGetSimpleNameInner.anonClass());
320
// assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
321
// exp,
322
// act);
323
//
324
// exp = TestGetSimpleNameInner.Nested.anonClass().getSimpleName();
325
// act = StringOps.getSimpleName(TestGetSimpleNameInner.Nested.anonClass());
326
// assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
327
// exp,
328
// act);
329
//
330
// exp = (new TestGetSimpleNameInner()).getInner().anonClass().getSimpleName();
331
// act = StringOps.getSimpleName((new TestGetSimpleNameInner()).getInner().anonClass());
332
// assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
333
// exp,
334
// act);
335
//
336
// exp = TestGetSimpleNameInner.getLambda().apply(null).getSimpleName();
337
// act = StringOps.getSimpleName(TestGetSimpleNameInner.getLambda().apply(null));
338
// assertEquals("Wrong simple name for anonymous inner class, exp="+exp+", act="+act,
339
// exp,
340
// act);
341
// }
342

343   public void testToStringLong() {
344     long[] a = null;
345     assertEquals("null", StringOps.toString(a));
346     assertEquals("[]", StringOps.toString(new long[] {}));
347     assertEquals("[1]", StringOps.toString(new long[] {1}));
348     assertEquals("[1, 2]", StringOps.toString(new long[] {1, 2}));
349   }
350   
351   public void testToStringInt() {
352     int[] a = null;
353     assertEquals("null", StringOps.toString(a));
354     assertEquals("[]", StringOps.toString(new int[] {}));
355     assertEquals("[1]", StringOps.toString(new int[] {1}));
356     assertEquals("[1, 2]", StringOps.toString(new int[] {1, 2}));
357   }
358   
359   public void testToStringShort() {
360     short[] a = null;
361     assertEquals("null", StringOps.toString(a));
362     assertEquals("[]", StringOps.toString(new short[] {}));
363     assertEquals("[1]", StringOps.toString(new short[] {1}));
364     assertEquals("[1, 2]", StringOps.toString(new short[] {1, 2}));
365   }
366   
367   public void testToStringChar() {
368     char[] a = null;
369     assertEquals("null", StringOps.toString(a));
370     assertEquals("[]", StringOps.toString(new char[] {}));
371     assertEquals("[a]", StringOps.toString(new char[] {'a'}));
372     assertEquals("[a, b]", StringOps.toString(new char[] {'a', 'b'}));
373   }
374   
375   public void testToStringByte() {
376     byte[] a = null;
377     assertEquals("null", StringOps.toString(a));
378     assertEquals("[]", StringOps.toString(new byte[] {}));
379     assertEquals("[1]", StringOps.toString(new byte[] {1}));
380     assertEquals("[1, 2]", StringOps.toString(new byte[] {1, 2}));
381   }
382   
383   public void testToStringBoolean() {
384     boolean[] a = null;
385     assertEquals("null", StringOps.toString(a));
386     assertEquals("[]", StringOps.toString(new boolean[] {}));
387     assertEquals("[true]", StringOps.toString(new boolean[] {true}));
388     assertEquals("[true, false]", StringOps.toString(new boolean[] {true, false}));
389   }
390   
391   public void testToStringFloat() {
392     float[] a = null;
393     assertEquals("null", StringOps.toString(a));
394     assertEquals("[]", StringOps.toString(new float[] {}));
395     assertEquals("[1.23]", StringOps.toString(new float[] {1.23f}));
396     assertEquals("[1.23, 4.56]", StringOps.toString(new float[] {1.23f, 4.56f}));
397   }
398   
399   public void testToStringDouble() {
400     double[] a = null;
401     assertEquals("null", StringOps.toString(a));
402     assertEquals("[]", StringOps.toString(new double[] {}));
403     assertEquals("[1.23]", StringOps.toString(new double[] {1.23}));
404     assertEquals("[1.23, 4.56]", StringOps.toString(new double[] {1.23, 4.56}));
405   }
406   
407   public void testToStringObject() {
408     Object JavaDoc[] a = null;
409     assertEquals("null", StringOps.toString(a));
410     assertEquals("[]", StringOps.toString(new Object JavaDoc[] {}));
411     assertEquals("[123]", StringOps.toString(new Object JavaDoc[] {"123"}));
412     assertEquals("[123, 123]", StringOps.toString(new Object JavaDoc[] {"123", new Integer JavaDoc(123)}));
413   }
414   
415   public void testMemSizeToString() {
416     assertEquals("<1024", "500 bytes", StringOps.memSizeToString(500));
417     assertEquals("1KB", "1 kilobyte", StringOps.memSizeToString(1024));
418     assertEquals("2KB", "2 kilobytes", StringOps.memSizeToString(1024*2));
419     assertEquals("1.5KB", "1.50 kilobytes", StringOps.memSizeToString((long)(1024*1.5)));
420     assertEquals("1MB", "1 megabyte", StringOps.memSizeToString((1024*1024)));
421     assertEquals("2MB", "2 megabytes", StringOps.memSizeToString((1024*1024*2)));
422     assertEquals("1.1MB", "1.10 megabytes", StringOps.memSizeToString((long)(1024*1024*1.1)));
423     assertEquals("1GB", "1 gigabyte", StringOps.memSizeToString((1024*1024*1024)));
424     assertEquals("1.25GB", "1.25 gigabytes", StringOps.memSizeToString((long)(1024*1024*1024*1.25)));
425   }
426 }
427
Popular Tags