KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > FileSharingTestApp


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.ObjectID;
9 import com.tc.object.TCObject;
10 import com.tc.object.bytecode.Manageable;
11 import com.tc.object.config.ConfigVisitor;
12 import com.tc.object.config.DSOClientConfigHelper;
13 import com.tc.object.config.TransparencyClassSpec;
14 import com.tc.object.dna.api.DNAWriter;
15 import com.tc.object.dna.api.PhysicalAction;
16 import com.tc.simulator.app.ApplicationConfig;
17 import com.tc.simulator.listener.ListenerProvider;
18 import com.tc.util.Assert;
19 import com.tctest.runner.AbstractTransparentApp;
20
21 import java.io.File JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 public class FileSharingTestApp extends AbstractTransparentApp {
27   private final static String JavaDoc UNIX_STYLE_MOCK_FILE_NAME = "\\home\\test\\file1";
28
29   private final CyclicBarrier barrier;
30   private File JavaDoc fileRoot;
31
32   public FileSharingTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
33     super(appId, cfg, listenerProvider);
34     barrier = new CyclicBarrier(getParticipantCount());
35   }
36
37   public void run() {
38     try {
39       int index = barrier.barrier();
40
41       basicTest(index);
42       fileDehydrateTest(index);
43     } catch (Throwable JavaDoc t) {
44       notifyError(t);
45     }
46   }
47
48   /**
49    * This is the basic test which only makes sure that the file object is shared. It
50    * does not test the cross platform test.
51    */

52   private void basicTest(int index) throws Exception JavaDoc {
53     if (index == 0) {
54       fileRoot = new File JavaDoc(UNIX_STYLE_MOCK_FILE_NAME);
55     }
56
57     barrier.barrier();
58
59     if (index != 0) {
60       Assert.assertEquals(UNIX_STYLE_MOCK_FILE_NAME, fileRoot.getPath());
61     }
62
63     barrier.barrier();
64   }
65
66   /**
67    * This test makes sure that the file separator is contained in the dna via dehydration.
68    */

69   private void fileDehydrateTest(int index) throws Exception JavaDoc {
70     if (index == 0) {
71       Manageable managed = (Manageable)fileRoot;
72       TCObject tcObject = managed.__tc_managed();
73       MockDNAWriter dnaWriter = new MockDNAWriter();
74       tcObject.dehydrate(dnaWriter);
75
76       List JavaDoc dna = dnaWriter.getDNA();
77       boolean separatorFound = false;
78       for (Iterator JavaDoc i=dna.iterator(); i.hasNext(); ) {
79         PhysicalAction action = (PhysicalAction)i.next();
80         Assert.assertTrue(action.isTruePhysical());
81         if ("File.fileSeparator".equals(action.getFieldName())) {
82           separatorFound = true;
83         }
84       }
85       Assert.assertTrue(separatorFound);
86     }
87   }
88
89   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
90     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
91     config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)");
92
93     String JavaDoc testClass = FileSharingTestApp.class.getName();
94     spec = config.getOrCreateSpec(testClass);
95
96     config.addIncludePattern(testClass + "$*");
97
98     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
99     config.addWriteAutolock(methodExpression);
100
101     spec.addRoot("fileRoot", "fileRoot");
102     spec.addRoot("barrier", "barrier");
103   }
104
105   private static class MockDNAWriter implements DNAWriter {
106
107     public List JavaDoc dna = new ArrayList JavaDoc();
108
109     public MockDNAWriter() {
110       //
111
}
112
113     public void addLogicalAction(int method, Object JavaDoc[] parameters) {
114       //
115
}
116
117     public void addPhysicalAction(String JavaDoc fieldName, Object JavaDoc value) {
118       //dna.add(new PhysicalAction(fieldName, value));
119
addPhysicalAction(fieldName, value, true);
120     }
121
122     public void finalizeDNA() {
123       //
124
}
125
126     public void addArrayElementAction(int index, Object JavaDoc value) {
127       //
128
}
129
130     public void addEntireArray(Object JavaDoc value) {
131       //
132
}
133
134     public void addLiteralValue(Object JavaDoc value) {
135       //
136
}
137
138     public void setParentObjectID(ObjectID id) {
139       //
140
}
141
142     public void setArrayLength(int length) {
143       //
144
}
145
146     public void addPhysicalAction(String JavaDoc fieldName, Object JavaDoc value, boolean canBeReference) {
147       dna.add(new PhysicalAction(fieldName, value, canBeReference));
148     }
149
150     public List JavaDoc getDNA() {
151       return dna;
152     }
153
154     public void addClassLoaderAction(String JavaDoc classLoaderFieldName, Object JavaDoc value) {
155       //
156

157     }
158
159     public void addSubArrayAction(int start, Object JavaDoc array, int length) {
160       //
161
}
162   }
163
164 }
165
Popular Tags