KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > reporter > ArchiveUtilTest


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.reporter;
6
7 import com.tc.config.schema.test.L1ConfigBuilder;
8 import com.tc.config.schema.test.L2ConfigBuilder;
9 import com.tc.config.schema.test.L2SConfigBuilder;
10 import com.tc.config.schema.test.SystemConfigBuilder;
11 import com.tc.config.schema.test.TerracottaConfigBuilder;
12 import com.tc.test.TCTestCase;
13 import com.tc.util.ExternalProcessStreamWriter;
14
15 import java.io.BufferedInputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26 import java.util.zip.ZipInputStream JavaDoc;
27
28 public final class ArchiveUtilTest extends TCTestCase {
29
30   private static final String JavaDoc NONE = "none";
31   private static final String JavaDoc TC_CONFIG = "tc-config.xml";
32   private static final String JavaDoc ARCHIVE = "mock-archive.zip";
33   private static final String JavaDoc MK_DATA_DIR = "mockdata";
34   private static final String JavaDoc MK_CLIENT_DIR = "client-logs";
35   private static final String JavaDoc MK_CLIENT_LOG0 = "terracotta-client.log";
36   private static final String JavaDoc MK_CLIENT_LOG1 = "terracotta-client.log.1";
37   private static final String JavaDoc MK_SERVER_LOG_DIR = "server-logs";
38   private static final String JavaDoc MK_SERVER_LOG0 = "terracotta-server.log";
39   private static final String JavaDoc MK_SERVER_LOG1 = "terracotta-server.log.1";
40   private static final String JavaDoc MK_SERVER_DATA_DIR = "server-data";
41   private static final String JavaDoc MK_SERVER_DATA0 = "objectdb";
42   private static final String JavaDoc MK_SERVER_DATA1 = "startup.lck";
43
44   private File JavaDoc mockDataDir;
45   private File JavaDoc archiveFile;
46
47   public ArchiveUtilTest() {
48     //disableAllUntil("2007-02-28");
49
}
50   
51   public void setUp() throws Exception JavaDoc {
52     generateMockFiles(getTempDirectory());
53     archiveFile = new File JavaDoc(mockDataDir + File.separator + ARCHIVE);
54   }
55   
56   public void tearDown() {
57     clear();
58   }
59
60   private File JavaDoc generateMockFiles(File JavaDoc tmpDir) throws IOException JavaDoc {
61     mockDataDir = new File JavaDoc(tmpDir + File.separator + MK_DATA_DIR);
62     mockDataDir.mkdir();
63     new File JavaDoc(mockDataDir + File.separator + MK_CLIENT_DIR).mkdir();
64     new File JavaDoc(mockDataDir + File.separator + MK_CLIENT_DIR + File.separator + MK_CLIENT_LOG0).createNewFile();
65     new File JavaDoc(mockDataDir + File.separator + MK_CLIENT_DIR + File.separator + MK_CLIENT_LOG1).createNewFile();
66     new File JavaDoc(mockDataDir + File.separator + MK_SERVER_LOG_DIR).mkdir();
67     new File JavaDoc(mockDataDir + File.separator + MK_SERVER_LOG_DIR + File.separator + MK_SERVER_LOG0).createNewFile();
68     new File JavaDoc(mockDataDir + File.separator + MK_SERVER_LOG_DIR + File.separator + MK_SERVER_LOG1).createNewFile();
69     new File JavaDoc(mockDataDir + File.separator + MK_SERVER_DATA_DIR).mkdir();
70     new File JavaDoc(mockDataDir + File.separator + MK_SERVER_DATA_DIR + File.separator + MK_SERVER_DATA0).createNewFile();
71     new File JavaDoc(mockDataDir + File.separator + MK_SERVER_DATA_DIR + File.separator + MK_SERVER_DATA1).createNewFile();
72     return mockDataDir;
73   }
74
75   private String JavaDoc createConfig(String JavaDoc clientLogs, String JavaDoc[] serverLogs, String JavaDoc[] serverData) {
76     TerracottaConfigBuilder builder = new TerracottaConfigBuilder();
77     SystemConfigBuilder sysBuilder = new SystemConfigBuilder();
78     sysBuilder.setConfigurationModel("production");
79     builder.setSystem(sysBuilder);
80     L2ConfigBuilder[] l2Builder = new L2ConfigBuilder[serverLogs.length];
81     for (int i = 0; i < l2Builder.length; i++) {
82       l2Builder[i] = new L2ConfigBuilder();
83       l2Builder[i].setName("localhost-" + i);
84       l2Builder[i].setData(serverData[i]);
85       l2Builder[i].setLogs(serverLogs[i]);
86     }
87     L2SConfigBuilder l2SBuilder = new L2SConfigBuilder();
88     l2SBuilder.setL2s(l2Builder);
89     builder.setServers(l2SBuilder);
90     L1ConfigBuilder l1Builder = new L1ConfigBuilder();
91     l1Builder.setLogs(clientLogs);
92     builder.setClient(l1Builder);
93     return builder.toString();
94   }
95
96   private File JavaDoc writeConfig(byte[] data) throws IOException JavaDoc {
97     System.out.println(new String JavaDoc(data));
98     File JavaDoc configFile = new File JavaDoc(mockDataDir + File.separator + TC_CONFIG);
99     if (configFile.exists()) configFile.delete();
100     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(configFile);
101     out.write(data);
102     out.flush();
103     out.close();
104     return configFile;
105   }
106
107   private Set JavaDoc listArchiveContents(File JavaDoc archive) throws IOException JavaDoc {
108     Set JavaDoc contents = new HashSet JavaDoc();
109     ZipInputStream JavaDoc in = new ZipInputStream JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(archive)));
110     ZipEntry JavaDoc entry;
111     while ((entry = in.getNextEntry()) != null) {
112       contents.add(entry.getName());
113       in.closeEntry();
114     }
115     in.close();
116     return contents;
117   }
118
119   private int executeArchiveUtil(String JavaDoc[] args) throws IOException JavaDoc, InterruptedException JavaDoc {
120     String JavaDoc[] commandLine = new String JavaDoc[args.length + 4];
121     commandLine[0] = "java";
122     commandLine[1] = "-classpath";
123     commandLine[2] = System.getProperty("java.class.path");
124     commandLine[3] = ArchiveUtil.class.getName();
125     System.arraycopy(args, 0, commandLine, 4, args.length);
126     System.out.println("\n***** Executing Java process -- ArchiveUtil *****\n");
127     Runtime JavaDoc runtime = Runtime.getRuntime();
128     Process JavaDoc process = runtime.exec(commandLine, new String JavaDoc[0], mockDataDir);
129
130     ExternalProcessStreamWriter writeSys = new ExternalProcessStreamWriter();
131     writeSys.printSys(process.getInputStream());
132     ExternalProcessStreamWriter writeErr = new ExternalProcessStreamWriter();
133     writeErr.printErr(process.getErrorStream());
134
135     int code = process.waitFor();
136     if (writeSys.hasException()) throw writeSys.getException();
137     if (writeErr.hasException()) throw writeErr.getException();
138     return code;
139   }
140
141   private void clear() {
142     if (archiveFile.exists()) archiveFile.delete();
143   }
144   
145   private void log(String JavaDoc str) {
146     System.out.println("--------------------------------------------------------------------------------");
147     System.out.println("-- " + str);
148     System.out.println("--------------------------------------------------------------------------------");
149   }
150
151   public void testValidServerArchiveContents() throws Exception JavaDoc {
152     clear();
153     log("<server> -n valid archive contents");
154     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
155     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
156     String JavaDoc config = createConfig(NONE, slogs, sdata);
157     File JavaDoc configFile = writeConfig(config.getBytes());
158     String JavaDoc[] args = new String JavaDoc[] { "-n", configFile.toString() };
159     executeArchiveUtil(args);
160     DateFormat JavaDoc df = new SimpleDateFormat JavaDoc("y-M-d");
161     File JavaDoc defaultArchive = new File JavaDoc(mockDataDir + File.separator + "tc-archive" + "_"
162         + df.format(new Date JavaDoc(System.currentTimeMillis())) + ".zip");
163     Set JavaDoc contents = listArchiveContents(defaultArchive);
164     assertTrue(contents.contains(TC_CONFIG));
165     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
166     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG0));
167     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG1));
168     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"));
169     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA0));
170     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA1));
171   }
172   
173   public void testDirectoryArg() throws Exception JavaDoc {
174     clear();
175     log("<server> test directory argument contents");
176     String JavaDoc[] args = new String JavaDoc[] { mockDataDir + File.separator + MK_SERVER_LOG_DIR };
177     executeArchiveUtil(args);
178     DateFormat JavaDoc df = new SimpleDateFormat JavaDoc("y-M-d");
179     File JavaDoc defaultArchive = new File JavaDoc(mockDataDir + File.separator + "tc-archive" + "_"
180         + df.format(new Date JavaDoc(System.currentTimeMillis())) + ".zip");
181     Set JavaDoc contents = listArchiveContents(defaultArchive);
182     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
183     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG0));
184     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG1));
185   }
186   
187   public void testInvalidDirectoryArg() throws Exception JavaDoc {
188     clear();
189     log("<server> test invalid directory argument");
190     String JavaDoc[] args = new String JavaDoc[] { "foo" + File.separator + "baz" };
191     executeArchiveUtil(args);
192     DateFormat JavaDoc df = new SimpleDateFormat JavaDoc("y-M-d");
193     File JavaDoc defaultArchive = new File JavaDoc(mockDataDir + File.separator + "tc-archive" + "_"
194         + df.format(new Date JavaDoc(System.currentTimeMillis())) + ".zip");
195     assertFalse(defaultArchive.exists());
196   }
197
198   public void testFileArg() throws Exception JavaDoc {
199     clear();
200     log("<server> test file argument");
201     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
202     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
203     String JavaDoc config = createConfig(NONE, slogs, sdata);
204     File JavaDoc configFile = writeConfig(config.getBytes());
205     String JavaDoc[] args = new String JavaDoc[] { "-n", configFile.toString(), archiveFile.toString() };
206     executeArchiveUtil(args);
207     Set JavaDoc contents = listArchiveContents(archiveFile);
208     assertTrue(contents.contains(TC_CONFIG));
209     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
210     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG0));
211     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG1));
212     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"));
213     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA0));
214     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA1));
215   }
216
217   public void testValidServerFullArchiveContents() throws Exception JavaDoc {
218     clear();
219     log("<server> valid archive contents");
220     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
221     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
222     String JavaDoc config = createConfig(NONE, slogs, sdata);
223     File JavaDoc configFile = writeConfig(config.getBytes());
224     String JavaDoc[] args = new String JavaDoc[] { configFile.toString(), archiveFile.toString() };
225     executeArchiveUtil(args);
226     Set JavaDoc contents = listArchiveContents(archiveFile);
227     assertTrue(contents.contains(TC_CONFIG));
228     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
229     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG0));
230     assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG1));
231     assertTrue(contents.contains(MK_SERVER_DATA_DIR + "/"));
232     assertTrue(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA0));
233     assertTrue(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA1));
234   }
235
236   public void testValidClientArchiveContents() throws Exception JavaDoc {
237     clear();
238     log("<client> valid archive contents");
239     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
240     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
241     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, sdata);
242     File JavaDoc configFile = writeConfig(config.getBytes());
243     String JavaDoc[] args = new String JavaDoc[] { "-c", configFile.toString(), archiveFile.toString() };
244     executeArchiveUtil(args);
245     Set JavaDoc contents = listArchiveContents(archiveFile);
246     assertTrue(contents.contains(TC_CONFIG));
247     assertTrue(contents.contains(MK_CLIENT_DIR + "/"));
248     assertTrue(contents.contains(MK_CLIENT_DIR + "/" + MK_CLIENT_LOG0));
249     assertTrue(contents.contains(MK_CLIENT_DIR + "/" + MK_CLIENT_LOG1));
250     assertFalse(contents.contains(MK_SERVER_LOG_DIR + "/"));
251     assertFalse(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG0));
252     assertFalse(contents.contains(MK_SERVER_LOG_DIR + "/" + MK_SERVER_LOG1));
253     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"));
254     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA0));
255     assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/" + MK_SERVER_DATA1));
256   }
257
258   public void testIgnoresNOptionForClient() throws Exception JavaDoc {
259     clear();
260     log("<client> ignores -n option");
261     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
262     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
263     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, sdata);
264     File JavaDoc configFile = writeConfig(config.getBytes());
265     executeArchiveUtil(new String JavaDoc[] { "-c", "-n", configFile.toString(), archiveFile.toString() });
266     assertTrue(archiveFile.exists());
267   }
268
269   public void testInvalidArgsOrder1() throws Exception JavaDoc {
270     clear();
271     log("<server> invalid args: <output file> <config>");
272     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
273     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
274     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, sdata);
275     File JavaDoc configFile = writeConfig(config.getBytes());
276     executeArchiveUtil(new String JavaDoc[] { archiveFile.toString(), configFile.toString() });
277     assertFalse(archiveFile.exists());
278   }
279
280   public void testInvalidArgsOrder2() throws Exception JavaDoc {
281     clear();
282     log("<server> invalid args: <config> <output file> -c");
283     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
284     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
285     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, sdata);
286     File JavaDoc configFile = writeConfig(config.getBytes());
287     executeArchiveUtil(new String JavaDoc[] { configFile.toString(), archiveFile.toString(), "-c" });
288     assertFalse(archiveFile.exists());
289   }
290
291   public void testInvalidArgsOrder3() throws Exception JavaDoc {
292     clear();
293     log("<server> invalid args: <config> -c <output file>");
294     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
295     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
296     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, sdata);
297     File JavaDoc configFile = writeConfig(config.getBytes());
298     executeArchiveUtil(new String JavaDoc[] { configFile.toString(), "-c", archiveFile.toString() });
299     assertFalse(archiveFile.exists());
300   }
301
302   public void testInvalidArgs1() throws Exception JavaDoc {
303     clear();
304     log("<server> invalid args: config");
305     executeArchiveUtil(new String JavaDoc[] { "foo", archiveFile.toString() });
306     assertFalse(archiveFile.exists());
307   }
308
309   public void testInvalidArgs2() throws Exception JavaDoc {
310     clear();
311     log("<server> invalid args: -x");
312     String JavaDoc[] slogs = new String JavaDoc[] { MK_SERVER_LOG_DIR };
313     String JavaDoc[] sdata = new String JavaDoc[] { MK_SERVER_DATA_DIR };
314     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, sdata);
315     File JavaDoc configFile = writeConfig(config.getBytes());
316     executeArchiveUtil(new String JavaDoc[] { "-x", configFile.toString(), archiveFile.toString() });
317     assertFalse(archiveFile.exists());
318   }
319
320   public void testInvalidConfig1() throws Exception JavaDoc {
321     clear();
322     log("<server> invalid config: <foo>");
323     File JavaDoc configFile = writeConfig("foo".getBytes());
324     executeArchiveUtil(new String JavaDoc[] { configFile.toString(), archiveFile.toString() });
325     assertFalse(archiveFile.exists());
326   }
327
328   public void testInvalidConfig2() throws Exception JavaDoc {
329     clear();
330     log("<server> invalid config: logs=foo");
331     String JavaDoc[] slogs = new String JavaDoc[] { "foo" };
332     String JavaDoc config = createConfig(MK_CLIENT_DIR, slogs, new String JavaDoc[] { NONE });
333     File JavaDoc configFile = writeConfig(config.getBytes());
334     executeArchiveUtil(new String JavaDoc[] { configFile.toString(), archiveFile.toString() });
335     assertFalse(archiveFile.exists());
336   }
337
338   public void testInvalidConfig3() throws Exception JavaDoc {
339     clear();
340     log("<server> invalid config: data=foo");
341     String JavaDoc[] sdata = new String JavaDoc[] { "foo" };
342     String JavaDoc config = createConfig(MK_CLIENT_DIR, new String JavaDoc[] { NONE }, sdata);
343     File JavaDoc configFile = writeConfig(config.getBytes());
344     executeArchiveUtil(new String JavaDoc[] { configFile.toString(), archiveFile.toString() });
345     assertFalse(archiveFile.exists());
346   }
347
348   public void testInvalidConfig4() throws Exception JavaDoc {
349     clear();
350     log("<client> invalid config: logs=foo");
351     String JavaDoc config = createConfig("foo", new String JavaDoc[] { NONE }, new String JavaDoc[] { NONE });
352     File JavaDoc configFile = writeConfig(config.getBytes());
353     executeArchiveUtil(new String JavaDoc[] { "-c", configFile.toString(), archiveFile.toString() });
354     assertFalse(archiveFile.exists());
355   }
356   
357   // TODO: wildcards will not be tested CDV-93
358
}
Popular Tags