01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tctest;
05:
06: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
07:
08: import com.tc.object.config.ConfigVisitor;
09: 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;
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: public class StringArrayCopyMethodsTestApp extends
21: AbstractErrorCatchingTransparentApp {
22:
23: private final Map map = new HashMap();
24: private final CyclicBarrier barrier;
25:
26: public StringArrayCopyMethodsTestApp(String appId,
27: ApplicationConfig cfg, ListenerProvider listenerProvider) {
28: super (appId, cfg, listenerProvider);
29:
30: barrier = new CyclicBarrier(getParticipantCount());
31: }
32:
33: protected void runTest() throws Throwable {
34:
35: synchronized (map) {
36: if (map.isEmpty()) {
37: map.put("byteArray", new byte[5]);
38: map.put("charArray", new char[5]);
39: }
40: }
41:
42: char[] ca = (char[]) map.get("charArray");
43: byte[] ba = (byte[]) map.get("byteArray");
44:
45: int num = barrier.barrier();
46: if (num == 0) {
47: synchronized (ba) {
48: // copy into the managed byte array
49: "Hi Tim, you are a golden god".getBytes(3, 6, ba, 1);
50: }
51:
52: synchronized (ca) {
53: // copy into the managed char array
54: "Gotta head to Santa Cruz for a haircut".getChars(6,
55: 10, ca, 1);
56: }
57: }
58:
59: barrier.barrier();
60:
61: Assert.assertTrue(Arrays.equals(new byte[] { 0, (byte) 'T',
62: (byte) 'i', (byte) 'm', 0 }, ba));
63: Assert.assertTrue(Arrays.equals(new char[] { 0, 'h', 'e', 'a',
64: 'd' }, ca));
65: }
66:
67: public static void visitL1DSOConfig(ConfigVisitor visitor,
68: com.tc.object.config.DSOClientConfigHelper config) {
69: String testClass = StringArrayCopyMethodsTestApp.class
70: .getName();
71: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
72:
73: String methodExpression = "* " + testClass + "*.*(..)";
74: config.addWriteAutolock(methodExpression);
75: spec.addRoot("barrier", "barrier");
76: spec.addRoot("map", "map");
77:
78: new CyclicBarrierSpec().visit(visitor, config);
79: }
80:
81: }
|