001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.stringbuffer;
006:
007: import org.apache.commons.lang.ClassUtils;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.ListenerProvider;
013: import com.tc.util.Assert;
014: import com.tctest.GenericTestApp;
015:
016: import java.security.SecureRandom;
017: import java.util.ArrayList;
018: import java.util.Arrays;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Random;
022:
023: public class StringBufferTestApp extends GenericTestApp {
024:
025: public StringBufferTestApp(String appId, ApplicationConfig cfg,
026: ListenerProvider listenerProvider) {
027: super (appId, cfg, listenerProvider, StringBuddy.class);
028: }
029:
030: void testOp001(StringBuddy buffer, boolean validate) {
031: synchronized (buffer) {
032: if (validate) {
033: Assert.assertEquals("true", buffer.toString());
034: } else {
035: buffer.append(true);
036: }
037: }
038: }
039:
040: void testOp002(StringBuddy buffer, boolean validate) {
041: synchronized (buffer) {
042: if (validate) {
043: Assert.assertEquals("x", buffer.toString());
044: } else {
045: buffer.append('x');
046: }
047: }
048: }
049:
050: void testOp003(StringBuddy buffer, boolean validate) {
051: synchronized (buffer) {
052: if (validate) {
053: Assert.assertEquals("tim", buffer.toString());
054: } else {
055: buffer.append(new char[] { 't', 'i', 'm' });
056: }
057: }
058: }
059:
060: void testOp004(StringBuddy buffer, boolean validate) {
061: synchronized (buffer) {
062: if (validate) {
063: Assert.assertEquals("666", buffer.toString());
064: } else {
065: buffer.append(new char[] { 'a', '6', '6', '6' }, 1, 3);
066: }
067: }
068: }
069:
070: void testOp005(StringBuddy buffer, boolean validate) {
071: synchronized (buffer) {
072: if (validate) {
073: Assert.assertEquals(String.valueOf(Math.PI), buffer
074: .toString());
075: } else {
076: buffer.append(Math.PI);
077: }
078: }
079: }
080:
081: void testOp006(StringBuddy buffer, boolean validate) {
082: synchronized (buffer) {
083: if (validate) {
084: Assert.assertEquals(String.valueOf((float) 2.0), buffer
085: .toString());
086: } else {
087: buffer.append((float) 2.0);
088: }
089: }
090: }
091:
092: void testOp007(StringBuddy buffer, boolean validate) {
093: synchronized (buffer) {
094: if (validate) {
095: Assert.assertEquals(String.valueOf(0x7fffffff), buffer
096: .toString());
097: } else {
098: buffer.append(Integer.MAX_VALUE);
099: }
100: }
101: }
102:
103: void testOp008(StringBuddy buffer, boolean validate) {
104: synchronized (buffer) {
105: if (validate) {
106: Assert.assertEquals("42", buffer.toString());
107: } else {
108: buffer.append(42L);
109: }
110: }
111: }
112:
113: void testOp009(StringBuddy buffer, boolean validate) {
114: String testString = "fetch me blocks, o' block provider";
115:
116: synchronized (buffer) {
117: if (validate) {
118: Assert.assertEquals(testString, buffer.toString());
119: } else {
120: // make sure this append WILL grow the buffer.
121: Assert.assertTrue("buffer too large: "
122: + buffer.capacity(),
123: buffer.capacity() < testString.length());
124:
125: buffer.append(testString);
126: }
127: }
128: }
129:
130: void testOp010(StringBuddy buffer, boolean validate) {
131: synchronized (buffer) {
132: if (validate) {
133: Assert.assertEquals("timmy", buffer.toString());
134: } else {
135: // make sure this append will NOT grow the buffer.
136: Assert.assertTrue("buffer too small: "
137: + buffer.capacity(),
138: buffer.capacity() > "timmy".length());
139: buffer.append("timmy");
140: }
141: }
142: }
143:
144: void testOp011(StringBuddy buffer, boolean validate) {
145: synchronized (buffer) {
146: if (validate) {
147: Assert.assertEquals("timmy", buffer.toString());
148: } else {
149: buffer.append(new ToStringObject("timmy"));
150: }
151: }
152: }
153:
154: void testOp012(StringBuddy buffer, boolean validate) {
155: synchronized (buffer) {
156: if (validate) {
157: Assert.assertEquals("0xcafebabe 0xdecafbad 0xdeadbeef",
158: buffer.toString());
159: } else {
160: buffer.append(new StringBuffer(
161: "0xcafebabe 0xdecafbad 0xdeadbeef"));
162: }
163: }
164: }
165:
166: void testOp013(StringBuddy buffer, boolean validate) {
167: synchronized (buffer) {
168: if (validate) {
169: StringBuffer defaultStringBuffer = new StringBuffer();
170: Assert.eval(buffer.capacity() == defaultStringBuffer
171: .capacity());
172: } else {
173: // no mutate
174: }
175: }
176: }
177:
178: void testOp014(StringBuddy buffer, boolean validate) {
179: synchronized (buffer) {
180: if (validate) {
181: Assert.assertEquals(78, buffer.capacity());
182: } else {
183: buffer
184: .append("sdfjkhrkj2h34kj32h4jk2ejknb2r902jfuoinkjfb252l3u54hb2kl5hb2l35i235ou82h34ku234");
185: }
186: }
187: }
188:
189: void testOp015(StringBuddy buffer, boolean validate) {
190: synchronized (buffer) {
191: if (validate) {
192: Assert.assertEquals(69, buffer.capacity());
193: } else {
194: // no mutate
195: }
196: }
197: }
198:
199: void testOp016(StringBuddy buffer, boolean validate) {
200: synchronized (buffer) {
201: if (validate) {
202: Assert.assertEquals('z', buffer.charAt(7));
203: } else {
204: buffer.append("aaaaaa zebra");
205: }
206: }
207: }
208:
209: void testOp017(StringBuddy buffer, boolean validate) {
210: synchronized (buffer) {
211: if (validate) {
212: Assert.assertEquals("toy", buffer.toString());
213: } else {
214: buffer.append("tommy");
215: buffer.delete(2, 4);
216: }
217: }
218: }
219:
220: void testOp018(StringBuddy buffer, boolean validate) {
221: synchronized (buffer) {
222: if (validate) {
223: Assert.assertEquals("joebob", buffer.toString());
224: } else {
225: buffer.append("joe-bob");
226: buffer.deleteCharAt(3);
227: }
228: }
229: }
230:
231: void testOp019(StringBuddy buffer, boolean validate) {
232: synchronized (buffer) {
233: if (validate) {
234: Assert.assertEquals(356, buffer.capacity());
235: } else {
236: buffer.ensureCapacity(356);
237: }
238: }
239: }
240:
241: void testOp020(StringBuddy buffer, boolean validate) {
242: synchronized (buffer) {
243: if (validate) {
244: char[] chars = new char[buffer.length()];
245: buffer.getChars(0, buffer.length(), chars, 0);
246: Assert.eval(Arrays.equals(chars,
247: "steve is fuzzy and blue".toCharArray()));
248: } else {
249: buffer.append("steve is fuzzy and blue");
250: }
251: }
252: }
253:
254: void testOp021(StringBuddy buffer, boolean validate) {
255: synchronized (buffer) {
256: if (validate) {
257: Assert.assertEquals(5, buffer.indexOf("bam"));
258: } else {
259: buffer.append("wham bam");
260: }
261: }
262: }
263:
264: void testOp022(StringBuddy buffer, boolean validate) {
265: synchronized (buffer) {
266: if (validate) {
267: Assert.assertEquals(33, buffer.indexOf("Java", 4));
268: } else {
269: buffer
270: .append("why can't I add methods to null? Java sucks");
271: }
272: }
273: }
274:
275: void testOp023(StringBuddy buffer, boolean validate) {
276: synchronized (buffer) {
277: if (validate) {
278: Assert
279: .assertEquals(
280: "Terracotta's distributed StringBuffer will not change the face of software",
281: buffer.toString());
282: } else {
283: buffer
284: .append("Terracotta's distributed StringBuffer will change the face of software");
285: buffer.insert(43, "not ".toCharArray(), 0, 4);
286: }
287: }
288: }
289:
290: void testOp024(StringBuddy buffer, boolean validate) {
291: synchronized (buffer) {
292: if (validate) {
293: Assert.assertEquals("Q. Is Steve dead sexy? true",
294: buffer.toString());
295: } else {
296: buffer.append("Q. Is Steve dead sexy? ");
297: buffer.insert(buffer.length(), true);
298: }
299: }
300: }
301:
302: void testOp025(StringBuddy buffer, boolean validate) {
303: synchronized (buffer) {
304: if (validate) {
305: Assert.assertEquals("@55FACE", buffer.toString());
306: } else {
307: buffer.append("55FACE");
308: buffer.insert(0, '@');
309: }
310: }
311: }
312:
313: void testOp026(StringBuddy buffer, boolean validate) {
314: synchronized (buffer) {
315: if (validate) {
316: Assert.assertEquals(
317: "Dude, where's my character array?", buffer
318: .toString());
319: } else {
320: buffer.append("Dude, my character array?");
321: buffer.insert(6, "where's ".toCharArray());
322: }
323: }
324: }
325:
326: void testOp027(StringBuddy buffer, boolean validate) {
327: synchronized (buffer) {
328: if (validate) {
329: Assert.assertEquals(String.valueOf(Double.MAX_VALUE),
330: buffer.toString());
331: } else {
332: buffer.insert(0, Double.MAX_VALUE);
333: }
334: }
335: }
336:
337: void testOp028(StringBuddy buffer, boolean validate) {
338: synchronized (buffer) {
339: if (validate) {
340: Assert.assertEquals(String.valueOf(Float.NaN), buffer
341: .toString());
342: } else {
343: buffer.insert(0, Float.NaN);
344: }
345: }
346: }
347:
348: void testOp029(StringBuddy buffer, boolean validate) {
349: synchronized (buffer) {
350: if (validate) {
351: Assert.assertEquals("123456789", buffer.toString());
352: } else {
353: buffer.insert(0, 123456789);
354: }
355: }
356: }
357:
358: void testOp030(StringBuddy buffer, boolean validate) {
359: synchronized (buffer) {
360: if (validate) {
361: Assert.assertEquals("123456789123456789", buffer
362: .toString());
363: } else {
364: buffer.insert(0, 123456789123456789L);
365: }
366: }
367: }
368:
369: void testOp031(StringBuddy buffer, boolean validate) {
370: synchronized (buffer) {
371: if (validate) {
372: Assert.assertEquals("yer mom", buffer.toString());
373: } else {
374: buffer.insert(0, new ToStringObject("yer mom"));
375: }
376: }
377: }
378:
379: void testOp032(StringBuddy buffer, boolean validate) {
380: synchronized (buffer) {
381: if (validate) {
382: Assert.assertEquals("chicks dig unix, but not macs",
383: buffer.toString());
384: } else {
385: buffer.insert(0, "chicks dig unix, but not macs");
386: }
387: }
388: }
389:
390: void testOp033(StringBuddy buffer, boolean validate) {
391: synchronized (buffer) {
392: if (validate) {
393: Assert.assertEquals(12, buffer.lastIndexOf("ball"));
394: } else {
395: buffer.append("ball1 ball2 ball3");
396: }
397: }
398: }
399:
400: void testOp034(StringBuddy buffer, boolean validate) {
401: synchronized (buffer) {
402: if (validate) {
403: Assert.assertEquals(4, buffer.lastIndexOf("1", 4));
404: } else {
405: buffer.append("ball1 ball2 ball3");
406: }
407: }
408: }
409:
410: void testOp035(StringBuddy buffer, boolean validate) {
411: synchronized (buffer) {
412: if (validate) {
413: Assert.assertEquals(0, buffer.length());
414: } else {
415: // nothing
416: }
417: }
418: }
419:
420: void testOp036(StringBuddy buffer, boolean validate) {
421: synchronized (buffer) {
422: if (validate) {
423: Assert.assertEquals(77, buffer.length());
424: } else {
425: buffer
426: .append("I was in a HOT TUB! I was NORMAL! I was ITALIAN!! I enjoyed the EARTHQUAKE");
427: }
428: }
429: }
430:
431: void testOp037(StringBuddy buffer, boolean validate) {
432: synchronized (buffer) {
433: if (validate) {
434: Assert.assertEquals(
435: "PUNK FUNK!! DISCO DUCK!! BIRTH CONTROL!!",
436: buffer.toString());
437: } else {
438: buffer
439: .append("PUNK ROCK!! DISCO DUCK!! BIRTH CONTROL!!");
440: buffer.replace(5, 8, "FUN");
441: }
442: }
443: }
444:
445: void testOp038(StringBuddy buffer, boolean validate) {
446: synchronized (buffer) {
447: if (validate) {
448: Assert.assertEquals("StringBuffer is da bomb", buffer
449: .toString());
450: } else {
451: buffer.append("YO");
452: buffer.replace(0, 2, "StringBuffer is da bomb");
453: }
454: }
455: }
456:
457: void testOp039(StringBuddy buffer, boolean validate) {
458: synchronized (buffer) {
459: if (validate) {
460: Assert.assertEquals("redrum", buffer.toString());
461: } else {
462: buffer.append("murder");
463: buffer.reverse();
464: }
465: }
466: }
467:
468: void testOp040(StringBuddy buffer, boolean validate) {
469: synchronized (buffer) {
470: if (validate) {
471: Assert
472: .assertEquals(
473: "writing StringBuffer test rates about a 0 on a scale of 10",
474: buffer.toString());
475: } else {
476: buffer
477: .append("writing StringBuffer test rates about a 8 on a scale of 10");
478: buffer.setCharAt(40, '0');
479: }
480: }
481: }
482:
483: void testOp041(StringBuddy buffer, boolean validate) {
484: synchronized (buffer) {
485: if (validate) {
486: Assert.assertEquals("no change", buffer.toString());
487: } else {
488: buffer.append("no change");
489: buffer.setLength(buffer.length());
490: }
491: }
492: }
493:
494: void testOp042(StringBuddy buffer, boolean validate) {
495: synchronized (buffer) {
496: if (validate) {
497: Assert.assertEquals("changed? YES!", buffer.toString());
498: } else {
499: buffer.append("changed? NO");
500: buffer.setLength(9);
501: buffer.append("YES!");
502: }
503: }
504: }
505:
506: void testOp043(StringBuddy buffer, boolean validate) {
507: synchronized (buffer) {
508: if (validate) {
509: // subSeqeunce and substring are the same operations
510: Assert.assertEquals("almost", buffer.subSequence(0, 6));
511: Assert.assertEquals("almost", buffer.substring(0, 6));
512:
513: Assert.assertEquals("tests", buffer.substring(20));
514: } else {
515: buffer.append("almost done writing tests");
516: }
517: }
518: }
519:
520: void testOp044(StringBuddy buffer, boolean validate) {
521: synchronized (buffer) {
522: if (validate) {
523: StringBuffer compare = new StringBuffer();
524: List vals = (List) sharedMap.get("random vals "
525: + buffer.getClass().getName());
526: for (Iterator i = vals.iterator(); i.hasNext();) {
527: Integer r = (Integer) i.next();
528: compare.append(r.intValue());
529: compare.append(',');
530: }
531:
532: Assert.assertEquals(compare.toString(), buffer
533: .toString());
534: } else {
535: Random rnd = new SecureRandom();
536: List vals = new ArrayList();
537:
538: final int n = 37 + rnd.nextInt(100);
539: for (int i = 0; i < n; i++) {
540: int r = rnd.nextInt();
541: vals.add(new Integer(r));
542: buffer.append(r);
543: buffer.append(',');
544: }
545:
546: synchronized (sharedMap) {
547: sharedMap.put("random vals "
548: + buffer.getClass().getName(), vals);
549: }
550: }
551: }
552: }
553:
554: void testOp045(StringBuddy buffer, boolean validate) {
555: synchronized (buffer) {
556: // test that StringBuffer.getChars() to a managed array works
557: String str = "these are the characters jerky";
558:
559: if (validate) {
560: char[] compare = (char[]) sharedMap
561: .get("getChars destination "
562: + buffer.getClass());
563: Assert.assertTrue(Arrays.equals(str.toCharArray(),
564: compare));
565: } else {
566: buffer.append(str);
567: char[] dest = new char[str.length()];
568: synchronized (sharedMap) {
569: sharedMap.put("getChars destination "
570: + buffer.getClass(), dest);
571: }
572: buffer.getChars(0, buffer.length(), dest, 0);
573: }
574: }
575: }
576:
577: void testOp046(StringBuddy buffer, boolean validate) {
578: synchronized (buffer) {
579: if (validate) {
580: Assert.assertEquals(String.valueOf(0x80000000), buffer
581: .toString());
582: } else {
583: buffer.append(Integer.MIN_VALUE);
584: }
585: }
586: }
587:
588: private static final String GET_CHARS_STRING = "make me slow";
589:
590: void testOp047(StringBuddy buffer, boolean validate) {
591: synchronized (buffer) {
592: // test that StringBuffer.getChars() works when the target array is managed *and* StringBuffer is managed
593:
594: char[] target = (char[]) sharedMap.get("target array "
595: + ClassUtils.getShortClassName(buffer.getClass()));
596:
597: if (validate) {
598: Assert.assertTrue(Arrays.equals(GET_CHARS_STRING
599: .toCharArray(), target));
600: } else {
601: buffer.append(GET_CHARS_STRING);
602: buffer.getChars(0, buffer.length(), target, 0);
603: }
604: }
605: }
606:
607: void testOp048(StringBuddy buffer, boolean validate) {
608: synchronized (buffer) {
609: // test that StringBuffer.getChars() works when the target array is managed *and* StringBuffer is NOT managed
610:
611: char[] target = (char[]) sharedMap.get("target array "
612: + ClassUtils.getShortClassName(buffer.getClass()));
613:
614: if (validate) {
615: Assert.assertTrue(Arrays.equals(GET_CHARS_STRING
616: .toCharArray(), target));
617: } else {
618: StringBuffer unshared = new StringBuffer();
619: unshared.append(GET_CHARS_STRING);
620: synchronized (target) {
621: unshared.getChars(0, unshared.length(), target, 0);
622: }
623: }
624: }
625: }
626:
627: void testStringBuilderToStringPerf(StringBuddy buddy,
628: boolean validate) {
629: if (!validate)
630: return;
631: if (!(buddy instanceof StringBuilderBuddy)) {
632: return;
633: }
634:
635: Random r = new Random();
636: int rnd = r.nextInt();
637:
638: StringBuilder builder = new StringBuilder();
639:
640: byte[] bytes = new byte[100];
641: r.nextBytes(bytes);
642: for (int i = 0; i < bytes.length; i++) {
643: builder.append(bytes[i]);
644: }
645:
646: if (validate) {
647: for (int i = 0; i < 250000; i++) {
648: builder = xorChars(builder);
649: String string = builder.toString();
650:
651: // not that I think it matters, but this code should defeat any runtime optimization of the above toString()
652: if (string.hashCode() == rnd) {
653: System.err.println();
654: }
655: }
656: }
657: }
658:
659: private static StringBuilder xorChars(StringBuilder builder) {
660: char[] dest = new char[builder.length()];
661: builder.getChars(0, builder.length(), dest, 0);
662: for (int i = 0; i < dest.length; i++) {
663: dest[i] = (char) (dest[dest.length - 1 - i] ^ dest[i]);
664: }
665: return new StringBuilder(new String(dest));
666: }
667:
668: protected Object getTestObject(String test) {
669: List l = new ArrayList();
670: l.add(sharedMap.get("buffer"));
671: l.add(sharedMap.get("builder"));
672: return l.iterator();
673: }
674:
675: protected void setupTestObject(String test) {
676: final StringBuddy buffer;
677: final StringBuddy builder;
678:
679: if ("Op015".equals(test)) {
680: buffer = new StringBufferBuddy(new StringBuffer(69));
681: builder = new StringBuilderBuddy(new StringBuilder(69));
682: } else {
683: buffer = new StringBufferBuddy(new StringBuffer());
684: builder = new StringBuilderBuddy(new StringBuilder());
685: }
686:
687: sharedMap.put("buffer", buffer);
688: sharedMap.put("builder", builder);
689:
690: sharedMap.put("target array StringBuilderBuddy",
691: new char[GET_CHARS_STRING.length()]);
692: sharedMap.put("target array StringBufferBuddy",
693: new char[GET_CHARS_STRING.length()]);
694: }
695:
696: private static final String BUFFER_TARGET_ARRAY = "buffer target array";
697: private static final String BUILDER_TARGET_ARRAY = "builder target array";
698:
699: public static void visitL1DSOConfig(ConfigVisitor visitor,
700: DSOClientConfigHelper config) {
701: String testClass = StringBufferTestApp.class.getName();
702: config.getOrCreateSpec(testClass);
703: String methodExpression = "* " + testClass + "*.*(..)";
704: config.addWriteAutolock(methodExpression);
705:
706: config.addIncludePattern(StringBufferBuddy.class.getName());
707: config.addIncludePattern(StringBuilderBuddy.class.getName());
708:
709: }
710:
711: private static class ToStringObject {
712:
713: private final String string;
714:
715: ToStringObject(String string) {
716: super ();
717: this .string = string;
718: }
719:
720: public String toString() {
721: return this.string;
722: }
723:
724: }
725:
726: }
|