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;
006:
007: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009: import com.tc.object.ObjectID;
010: import com.tc.object.TCObject;
011: import com.tc.object.bytecode.Manageable;
012: import com.tc.object.config.ConfigVisitor;
013: import com.tc.object.config.DSOClientConfigHelper;
014: import com.tc.object.config.TransparencyClassSpec;
015: import com.tc.object.dna.api.DNAWriter;
016: import com.tc.object.dna.api.PhysicalAction;
017: import com.tc.simulator.app.ApplicationConfig;
018: import com.tc.simulator.listener.ListenerProvider;
019: import com.tc.util.Assert;
020: import com.tctest.runner.AbstractTransparentApp;
021:
022: import java.io.File;
023: import java.lang.reflect.Field;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: public class FileSharingTestApp extends AbstractTransparentApp {
029: private final static String MOCK_FILE_NAME = "/home/teck/.bashrc";
030:
031: private final CyclicBarrier barrier;
032: private File fileRoot;
033:
034: public FileSharingTestApp(String appId, ApplicationConfig cfg,
035: ListenerProvider listenerProvider) {
036: super (appId, cfg, listenerProvider);
037: barrier = new CyclicBarrier(getParticipantCount());
038: }
039:
040: public void run() {
041: try {
042: int index = barrier.barrier();
043:
044: basicTest(index);
045: fileDehydrateTest(index);
046:
047: mutateFileTest(index);
048:
049: } catch (Throwable t) {
050: notifyError(t);
051: }
052: }
053:
054: private void mutateFileTest(int index) throws Exception {
055: File root = fileRoot; // ensure the root is faulted, so that the mutation will be broadcast
056:
057: barrier.barrier();
058:
059: if (index == 0) {
060: synchronized (root) {
061: Field field = File.class.getDeclaredField("path");
062: field.setAccessible(true);
063: field.set(root, "timmy");
064: }
065: }
066:
067: barrier.barrier();
068:
069: synchronized (root) {
070: Assert.assertEquals("timmy", root.getPath());
071: }
072: }
073:
074: private void basicTest(int index) throws Exception {
075: if (index == 0) {
076: fileRoot = new File(MOCK_FILE_NAME);
077: }
078:
079: barrier.barrier();
080:
081: if (index != 0) {
082: Assert.assertEquals(new File(MOCK_FILE_NAME).getPath(),
083: fileRoot.getPath());
084: }
085:
086: barrier.barrier();
087: }
088:
089: /**
090: * This test makes sure that the file separator is contained in the dna via dehydration.
091: */
092: private void fileDehydrateTest(int index) throws Exception {
093: if (index == 0) {
094: Manageable managed = (Manageable) fileRoot;
095: TCObject tcObject = managed.__tc_managed();
096: MockDNAWriter dnaWriter = new MockDNAWriter();
097: tcObject.setIsNew();
098: tcObject.dehydrateIfNew(dnaWriter);
099:
100: List dna = dnaWriter.getDNA();
101:
102: Assert.assertEquals(2, dna.size());
103:
104: boolean separatorFound = false;
105: for (Iterator i = dna.iterator(); i.hasNext();) {
106: PhysicalAction action = (PhysicalAction) i.next();
107: Assert.assertTrue(action.isTruePhysical());
108: if ("java.io.File._tcFileSeparator".equals(action
109: .getFieldName())) {
110: separatorFound = true;
111: }
112: }
113: Assert.assertTrue(separatorFound);
114: }
115: }
116:
117: public static void visitL1DSOConfig(ConfigVisitor visitor,
118: DSOClientConfigHelper config) {
119: TransparencyClassSpec spec = config
120: .getOrCreateSpec(CyclicBarrier.class.getName());
121: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
122: + "*.*(..)");
123:
124: String testClass = FileSharingTestApp.class.getName();
125: spec = config.getOrCreateSpec(testClass);
126:
127: config.addIncludePattern(testClass + "$*");
128:
129: String methodExpression = "* " + testClass + "*.*(..)";
130: config.addWriteAutolock(methodExpression);
131:
132: spec.addRoot("fileRoot", "fileRoot");
133: spec.addRoot("barrier", "barrier");
134: }
135:
136: private static class MockDNAWriter implements DNAWriter {
137:
138: public List dna = new ArrayList();
139:
140: public MockDNAWriter() {
141: //
142: }
143:
144: public void addLogicalAction(int method, Object[] parameters) {
145: //
146: }
147:
148: public void addPhysicalAction(String fieldName, Object value) {
149: // dna.add(new PhysicalAction(fieldName, value));
150: addPhysicalAction(fieldName, value, true);
151: }
152:
153: public void finalizeDNA(boolean isDeltaDNA) {
154: //
155: }
156:
157: public void addArrayElementAction(int index, Object value) {
158: //
159: }
160:
161: public void addEntireArray(Object value) {
162: //
163: }
164:
165: public void addLiteralValue(Object value) {
166: //
167: }
168:
169: public void setParentObjectID(ObjectID id) {
170: //
171: }
172:
173: public void setArrayLength(int length) {
174: //
175: }
176:
177: public void addPhysicalAction(String fieldName, Object value,
178: boolean canBeReference) {
179: dna
180: .add(new PhysicalAction(fieldName, value,
181: canBeReference));
182: }
183:
184: public List getDNA() {
185: return dna;
186: }
187:
188: public void addClassLoaderAction(String classLoaderFieldName,
189: Object value) {
190: //
191:
192: }
193:
194: public void addSubArrayAction(int start, Object array,
195: int length) {
196: //
197: }
198: }
199:
200: }
|