KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > simulator > distrunner > SetupTest


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.tc.simulator.distrunner;
5
6 import org.apache.commons.io.FileUtils;
7
8 import com.tc.object.config.ConfigVisitor;
9 import com.tc.object.config.DSOApplicationConfig;
10 import com.tc.test.TCTestCase;
11 import com.tc.util.concurrent.NoExceptionLinkedQueue;
12 import com.tcsimulator.ClientSpecImpl;
13 import com.tcsimulator.ConfigWriter;
14 import com.tcsimulator.Sandbox;
15 import com.tcsimulator.Setup;
16 import com.tcsimulator.distrunner.ServerSpec;
17 import com.tcsimulator.distrunner.ServerSpecImpl;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 public class SetupTest extends TCTestCase {
29   private static final String JavaDoc LICENSE_FILENAME = "license.lic";
30
31   public void testBasic() throws Throwable JavaDoc {
32     Collection JavaDoc clientSpecs = new ArrayList JavaDoc();
33     String JavaDoc clientHostname = "myClientHostname";
34     String JavaDoc clientTestHome = getTempDirectory().getAbsolutePath();
35     int vmCount = 0;
36     int executionCount = 0;
37     int intensity = 1;
38     clientSpecs.add(new ClientSpecImpl(clientHostname, clientTestHome, vmCount, executionCount, new ArrayList JavaDoc()));
39
40     InetAddress JavaDoc host = InetAddress.getLocalHost();
41     String JavaDoc localHostName = host.getHostName();
42
43     String JavaDoc serverHostname = localHostName;
44     String JavaDoc serverTestHome = getTempDirectory().getAbsolutePath();
45
46     File JavaDoc licenseFile = new File JavaDoc(serverTestHome, LICENSE_FILENAME);
47     FileUtils.touch(licenseFile);
48
49     ServerSpec serverSpec = new ServerSpecImpl(serverHostname, serverTestHome, -1, -1, -1, new ArrayList JavaDoc(),
50                                                ServerSpec.TEST_SERVER);
51
52     String JavaDoc testAppClassName = TestApp.class.getName();
53     /* Setup setup = */new Setup(new String JavaDoc[] {}, serverSpec, testAppClassName, clientSpecs, intensity, false);
54
55     Sandbox sandbox = new Sandbox(new File JavaDoc(serverTestHome), Sandbox.TEST_SERVER);
56
57     File JavaDoc configFile = new File JavaDoc(serverTestHome, sandbox.getConfigFile().getName());
58     assertFalse(configFile.exists());
59     assertNull(TestApp.visitCalls.poll(0));
60
61     Collection JavaDoc classesToVisit = new ArrayList JavaDoc();
62     classesToVisit.add(TestApp.class);
63     ConfigWriter configWriter = new ConfigWriter(serverSpec, classesToVisit, sandbox);
64     configWriter.writeConfigFile();
65
66     assertNotNull(TestApp.visitCalls.poll(0));
67     assertTrue(configFile.exists());
68     dumpConfigFile(configFile);
69
70   }
71
72   private void dumpConfigFile(File JavaDoc configFile) throws IOException JavaDoc {
73     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(configFile)));
74     String JavaDoc line;
75     while ((line = reader.readLine()) != null) {
76       System.out.println(line);
77     }
78   }
79
80   private static final class TestApp {
81     public static final NoExceptionLinkedQueue visitCalls = new NoExceptionLinkedQueue();
82
83     public static void visitDSOApplicationConfig(ConfigVisitor visitor, DSOApplicationConfig cfg) {
84       visitCalls.put(new Object JavaDoc[] { visitor, cfg });
85     }
86   }
87
88 }
89
Popular Tags