KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > StringArrayCopyMethodsTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.TransparencyClassSpec;
10 import com.tc.object.config.spec.CyclicBarrierSpec;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tc.util.Assert;
14 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
15
16 import java.util.Arrays JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 public class StringArrayCopyMethodsTestApp extends AbstractErrorCatchingTransparentApp {
21
22   private final Map JavaDoc map = new HashMap JavaDoc();
23   private final CyclicBarrier barrier;
24
25   public StringArrayCopyMethodsTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
26     super(appId, cfg, listenerProvider);
27
28     barrier = new CyclicBarrier(getParticipantCount());
29   }
30
31   protected void runTest() throws Throwable JavaDoc {
32
33     synchronized (map) {
34       if (map.isEmpty()) {
35         map.put("byteArray", new byte[5]);
36         map.put("charArray", new char[5]);
37       }
38     }
39
40     char[] ca = (char[]) map.get("charArray");
41     byte[] ba = (byte[]) map.get("byteArray");
42
43     int num = barrier.barrier();
44     if (num == 0) {
45       synchronized (ba) {
46         // copy into the managed byte array
47
"Hi Tim, you are a golden god".getBytes(3, 6, ba, 1);
48       }
49
50       synchronized (ca) {
51         // copy into the managed char array
52
"Gotta head to Santa Cruz for a haircut".getChars(6, 10, ca, 1);
53       }
54     }
55
56     barrier.barrier();
57
58     Assert.assertTrue(Arrays.equals(new byte[] { 0, (byte) 'T', (byte) 'i', (byte) 'm', 0 }, ba));
59     Assert.assertTrue(Arrays.equals(new char[] { 0, 'h', 'e', 'a', 'd' }, ca));
60   }
61
62   public static void visitL1DSOConfig(ConfigVisitor visitor, com.tc.object.config.DSOClientConfigHelper config) {
63     String JavaDoc testClass = StringArrayCopyMethodsTestApp.class.getName();
64     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
65
66     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
67     config.addWriteAutolock(methodExpression);
68     spec.addRoot("barrier", "barrier");
69     spec.addRoot("map", "map");
70
71     new CyclicBarrierSpec().visit(visitor, config);
72   }
73
74 }
75
Popular Tags