KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > RandomIOClassesTest


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.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
11 import com.tc.object.config.spec.CyclicBarrierSpec;
12 import com.tc.simulator.app.ApplicationConfig;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tc.util.Assert;
15 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
16
17 import java.io.BufferedWriter JavaDoc;
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.DataInputStream JavaDoc;
21 import java.io.DataOutputStream JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 public class RandomIOClassesTest extends TransparentTestBase {
27
28   private static final int NODE_COUNT = 2;
29
30   protected void setUp() throws Exception JavaDoc {
31     super.setUp();
32     getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1);
33     initializeTestRunner();
34   }
35
36   protected Class JavaDoc getApplicationClass() {
37     return RandomIOClassesTestApp.class;
38   }
39
40   public static class RandomIOClassesTestApp extends AbstractErrorCatchingTransparentApp {
41
42     private final Map JavaDoc map = new HashMap JavaDoc();
43     private final CyclicBarrier barrier;
44
45     public RandomIOClassesTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
46       super(appId, cfg, listenerProvider);
47       this.barrier = new CyclicBarrier(getParticipantCount());
48     }
49
50     protected void runTest() throws Throwable JavaDoc {
51       int index = barrier.barrier();
52
53       if (index == 0) {
54         StringWriter JavaDoc sw = new StringWriter JavaDoc();
55         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
56
57         synchronized (map) {
58           map.put("sw", sw);
59           map.put("bw", new BufferedWriter JavaDoc(sw));
60           map.put("baos", baos);
61           map.put("dos", new DataOutputStream JavaDoc(baos));
62         }
63       }
64
65       barrier.barrier();
66
67       final StringWriter JavaDoc sw = (StringWriter JavaDoc) map.get("sw");
68       final BufferedWriter JavaDoc bw = (BufferedWriter JavaDoc) map.get("bw");
69       final ByteArrayOutputStream JavaDoc baos = (ByteArrayOutputStream JavaDoc) map.get("baos");
70       final DataOutputStream JavaDoc dos = (DataOutputStream JavaDoc) map.get("dos");
71
72       if (index == 0) {
73         synchronized (bw) {
74           bw.write("Hello");
75           bw.write(" there");
76           bw.flush();
77         }
78
79         synchronized (dos) {
80           dos.writeUTF("Nothing beats a distributed DataOutputStream");
81           dos.writeInt(42);
82           dos.writeBoolean(false);
83         }
84       }
85
86       barrier.barrier();
87
88       if (index == 1) {
89         Assert.assertEquals("Hello there", sw.toString());
90
91         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(baos.toByteArray()));
92         Assert.assertEquals(dis.readUTF(), "Nothing beats a distributed DataOutputStream");
93         Assert.assertEquals(42, dis.readInt());
94         Assert.assertFalse(dis.readBoolean());
95       }
96     }
97
98     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
99       String JavaDoc testClass = RandomIOClassesTestApp.class.getName();
100       TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
101
102       spec.addRoot("map", "map");
103       spec.addRoot("barrier", "barrier");
104
105       String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
106       config.addWriteAutolock(methodExpression);
107
108       new CyclicBarrierSpec().visit(visitor, config);
109     }
110
111   }
112
113 }
114
Popular Tags