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.tcverify;
006:
007: import org.apache.commons.io.output.TeeOutputStream;
008: import org.apache.commons.lang.ArrayUtils;
009:
010: import com.tc.config.schema.builder.InstrumentedClassConfigBuilder;
011: import com.tc.config.schema.builder.LockConfigBuilder;
012: import com.tc.config.schema.builder.RootConfigBuilder;
013: import com.tc.config.schema.setup.FatalIllegalConfigurationChangeHandler;
014: import com.tc.config.schema.setup.StandardTVSConfigurationSetupManagerFactory;
015: import com.tc.config.schema.setup.TVSConfigurationSetupManagerFactory;
016: import com.tc.config.schema.test.InstrumentedClassConfigBuilderImpl;
017: import com.tc.config.schema.test.L2ConfigBuilder;
018: import com.tc.config.schema.test.LockConfigBuilderImpl;
019: import com.tc.config.schema.test.RootConfigBuilderImpl;
020: import com.tc.config.schema.test.TerracottaConfigBuilder;
021: import com.tc.process.LinkedJavaProcess;
022: import com.tc.process.StreamCopier;
023: import com.tc.server.TCServerImpl;
024: import com.tc.test.TCTestCase;
025: import com.tc.test.TestConfigObject;
026: import com.tc.util.PortChooser;
027:
028: import java.io.BufferedReader;
029: import java.io.ByteArrayOutputStream;
030: import java.io.File;
031: import java.io.FileOutputStream;
032: import java.io.FileWriter;
033: import java.io.IOException;
034: import java.io.StringReader;
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Collections;
038: import java.util.List;
039:
040: /**
041: * A quick/shallow test that uses the DSOVerifier client program
042: */
043: public class DSOVerifierTest extends TCTestCase {
044:
045: TCServerImpl server;
046:
047: public static void main(String[] args) throws Exception {
048: DSOVerifierTest t = new DSOVerifierTest();
049: t.setUp();
050: t.test();
051: t.tearDown();
052: }
053:
054: public DSOVerifierTest() {
055: // disableAllUntil("2007-09-11");
056: }
057:
058: protected void setUp() throws Exception {
059: super .setUp();
060:
061: File configFile = writeConfigFile();
062:
063: StandardTVSConfigurationSetupManagerFactory config;
064: config = new StandardTVSConfigurationSetupManagerFactory(
065: new String[] {
066: StandardTVSConfigurationSetupManagerFactory.CONFIG_SPEC_ARGUMENT_WORD,
067: configFile.getAbsolutePath() }, true,
068: new FatalIllegalConfigurationChangeHandler());
069:
070: server = new TCServerImpl(config
071: .createL2TVSConfigurationSetupManager(null));
072: server.start();
073: }
074:
075: protected void tearDown() throws Exception {
076: if (server != null) {
077: server.stop();
078: }
079: super .tearDown();
080: }
081:
082: private boolean verifyOutput(String output, String desiredPrefix)
083: throws IOException {
084: BufferedReader reader = new BufferedReader(new StringReader(
085: output));
086:
087: String line;
088: while ((line = reader.readLine()) != null) {
089: if (line.startsWith(desiredPrefix)) {
090: return true;
091: }
092: }
093:
094: return false;
095: }
096:
097: public void test() throws Exception {
098: LinkedJavaProcess p1 = new LinkedJavaProcess(getMainClass(),
099: new String[] { "1", "2" });
100: p1.setJavaArguments(getJvmArgs());
101:
102: LinkedJavaProcess p2 = new LinkedJavaProcess(getMainClass(),
103: new String[] { "2", "1" });
104: p2.setJavaArguments(getJvmArgs());
105:
106: p1.start();
107: p2.start();
108:
109: p1.getOutputStream().close();
110: p2.getOutputStream().close();
111:
112: FileOutputStream p1o = new FileOutputStream(
113: getTempFile("p1out.log"));
114: FileOutputStream p1e = new FileOutputStream(
115: getTempFile("p1err.log"));
116: FileOutputStream p2o = new FileOutputStream(
117: getTempFile("p2out.log"));
118: FileOutputStream p2e = new FileOutputStream(
119: getTempFile("p2err.log"));
120:
121: ByteArrayOutputStream p1oBytes = new ByteArrayOutputStream();
122: ByteArrayOutputStream p1eBytes = new ByteArrayOutputStream();
123: ByteArrayOutputStream p2oBytes = new ByteArrayOutputStream();
124: ByteArrayOutputStream p2eBytes = new ByteArrayOutputStream();
125:
126: StreamCopier p1StdOut = new StreamCopier(p1.getInputStream(),
127: new TeeOutputStream(p1oBytes, p1o));
128: StreamCopier p1StdErr = new StreamCopier(p1.getErrorStream(),
129: new TeeOutputStream(p1eBytes, p1e));
130: StreamCopier p2StdOut = new StreamCopier(p2.getInputStream(),
131: new TeeOutputStream(p2oBytes, p2o));
132: StreamCopier p2StdErr = new StreamCopier(p2.getErrorStream(),
133: new TeeOutputStream(p2eBytes, p2e));
134: p1StdOut.start();
135: p1StdErr.start();
136: p2StdOut.start();
137: p2StdErr.start();
138:
139: p1.waitFor();
140: p2.waitFor();
141:
142: p1StdOut.join();
143: p1StdErr.join();
144: p2StdOut.join();
145: p2StdErr.join();
146:
147: String p1Out = new String(p1oBytes.toByteArray());
148: String p1Err = new String(p1eBytes.toByteArray());
149: String p2Out = new String(p2oBytes.toByteArray());
150: String p2Err = new String(p2eBytes.toByteArray());
151:
152: String compound = "Process 1 output:\n\n" + p1Out
153: + "\n\nProcess 1 error:\n\n" + p1Err
154: + "\n\nProcess 2 output:\n\n" + p2Out
155: + "\n\nProcess 2 error:\n\n" + p2Err + "\n\n";
156:
157: String output = "L2-DSO-OK:";
158: if (!verifyOutput(p1Out, output)) {
159: fail(compound);
160: }
161:
162: if (!verifyOutput(p2Out, output)) {
163: fail(compound);
164: }
165: }
166:
167: protected String getMainClass() {
168: return DSOVerifier.class.getName();
169: }
170:
171: protected String[] getJvmArgs() {
172: String bootclasspath = "-Xbootclasspath/p:"
173: + TestConfigObject.getInstance().normalBootJar();
174:
175: List<String> args = new ArrayList<String>();
176: args.add(bootclasspath);
177: args
178: .add("-D"
179: + TVSConfigurationSetupManagerFactory.CONFIG_FILE_PROPERTY_NAME
180: + "=localhost:" + server.getDSOListenPort());
181:
182: args.addAll(getExtraJvmArgs());
183:
184: String[] rv = args.toArray(new String[args.size()]);
185: System.out.println("JVM args: " + ArrayUtils.toString(rv));
186: return rv;
187: }
188:
189: protected Collection<String> getExtraJvmArgs() {
190: return Collections.EMPTY_LIST;
191: }
192:
193: protected boolean isSynchronousWrite() {
194: return false;
195: }
196:
197: private File writeConfigFile() throws IOException {
198: TerracottaConfigBuilder config = TerracottaConfigBuilder
199: .newMinimalInstance();
200:
201: PortChooser portChooser = new PortChooser();
202: L2ConfigBuilder l2Builder = L2ConfigBuilder
203: .newMinimalInstance();
204: l2Builder.setDSOPort(portChooser.chooseRandomPort());
205: l2Builder.setJMXPort(portChooser.chooseRandomPort());
206: l2Builder.setName("localhost");
207:
208: config.getServers().setL2s(new L2ConfigBuilder[] { l2Builder });
209:
210: InstrumentedClassConfigBuilder instrumentedClass = new InstrumentedClassConfigBuilderImpl();
211: instrumentedClass.setClassExpression("com.tcverify..*");
212: config
213: .getApplication()
214: .getDSO()
215: .setInstrumentedClasses(
216: new InstrumentedClassConfigBuilder[] { instrumentedClass });
217:
218: LockConfigBuilder lock1 = new LockConfigBuilderImpl(
219: LockConfigBuilder.TAG_NAMED_LOCK);
220: lock1
221: .setMethodExpression("java.lang.Object com.tcverify.DSOVerifier.getValue(int)");
222: lock1.setLockName("verifierMap");
223: if (isSynchronousWrite()) {
224: lock1
225: .setLockLevel(LockConfigBuilder.LEVEL_SYNCHRONOUS_WRITE);
226: } else {
227: lock1.setLockLevel(LockConfigBuilder.LEVEL_WRITE);
228: }
229:
230: LockConfigBuilder lock2 = new LockConfigBuilderImpl(
231: LockConfigBuilder.TAG_NAMED_LOCK);
232: lock2
233: .setMethodExpression("void com.tcverify.DSOVerifier.setValue(int)");
234: lock2.setLockName("verifierMap");
235: if (isSynchronousWrite()) {
236: lock2
237: .setLockLevel(LockConfigBuilder.LEVEL_SYNCHRONOUS_WRITE);
238: } else {
239: lock2.setLockLevel(LockConfigBuilder.LEVEL_WRITE);
240: }
241:
242: config.getApplication().getDSO().setLocks(
243: new LockConfigBuilder[] { lock1, lock2 });
244:
245: RootConfigBuilder root = new RootConfigBuilderImpl();
246: root.setFieldName("com.tcverify.DSOVerifier.verifierMap");
247: root.setRootName("verifierMap");
248:
249: config.getApplication().getDSO().setRoots(
250: new RootConfigBuilder[] { root });
251:
252: File configDir = this .getTempDirectory();
253: File configFile = new File(configDir, "dso-verifier-config.xml");
254: FileWriter cfgOut = new FileWriter(configFile);
255: cfgOut.write(config.toString());
256: cfgOut.flush();
257: cfgOut.close();
258: return configFile;
259: }
260: }
|