001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.reporter;
006:
007: import com.tc.config.schema.test.L1ConfigBuilder;
008: import com.tc.config.schema.test.L2ConfigBuilder;
009: import com.tc.config.schema.test.L2SConfigBuilder;
010: import com.tc.config.schema.test.SystemConfigBuilder;
011: import com.tc.config.schema.test.TerracottaConfigBuilder;
012: import com.tc.test.TCTestCase;
013: import com.tc.util.ExternalProcessStreamWriter;
014:
015: import java.io.BufferedInputStream;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.FileOutputStream;
019: import java.io.IOException;
020: import java.text.DateFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023: import java.util.HashSet;
024: import java.util.Set;
025: import java.util.zip.ZipEntry;
026: import java.util.zip.ZipInputStream;
027:
028: public final class ArchiveUtilTest extends TCTestCase {
029:
030: private static final String NONE = "none";
031: private static final String TC_CONFIG = "tc-config.xml";
032: private static final String ARCHIVE = "mock-archive.zip";
033: private static final String MK_DATA_DIR = "mockdata";
034: private static final String MK_CLIENT_DIR = "client-logs";
035: private static final String MK_CLIENT_LOG0 = "terracotta-client.log";
036: private static final String MK_CLIENT_LOG1 = "terracotta-client.log.1";
037: private static final String MK_SERVER_LOG_DIR = "server-logs";
038: private static final String MK_SERVER_LOG0 = "terracotta-server.log";
039: private static final String MK_SERVER_LOG1 = "terracotta-server.log.1";
040: private static final String MK_SERVER_DATA_DIR = "server-data";
041: private static final String MK_SERVER_DATA0 = "objectdb";
042: private static final String MK_SERVER_DATA1 = "startup.lck";
043:
044: private File mockDataDir;
045: private File archiveFile;
046:
047: public ArchiveUtilTest() {
048: //disableAllUntil("2007-02-28");
049: }
050:
051: public void setUp() throws Exception {
052: generateMockFiles(getTempDirectory());
053: archiveFile = new File(mockDataDir + File.separator + ARCHIVE);
054: }
055:
056: public void tearDown() {
057: clear();
058: }
059:
060: private File generateMockFiles(File tmpDir) throws IOException {
061: mockDataDir = new File(tmpDir + File.separator + MK_DATA_DIR);
062: mockDataDir.mkdir();
063: new File(mockDataDir + File.separator + MK_CLIENT_DIR).mkdir();
064: new File(mockDataDir + File.separator + MK_CLIENT_DIR
065: + File.separator + MK_CLIENT_LOG0).createNewFile();
066: new File(mockDataDir + File.separator + MK_CLIENT_DIR
067: + File.separator + MK_CLIENT_LOG1).createNewFile();
068: new File(mockDataDir + File.separator + MK_SERVER_LOG_DIR)
069: .mkdir();
070: new File(mockDataDir + File.separator + MK_SERVER_LOG_DIR
071: + File.separator + MK_SERVER_LOG0).createNewFile();
072: new File(mockDataDir + File.separator + MK_SERVER_LOG_DIR
073: + File.separator + MK_SERVER_LOG1).createNewFile();
074: new File(mockDataDir + File.separator + MK_SERVER_DATA_DIR)
075: .mkdir();
076: new File(mockDataDir + File.separator + MK_SERVER_DATA_DIR
077: + File.separator + MK_SERVER_DATA0).createNewFile();
078: new File(mockDataDir + File.separator + MK_SERVER_DATA_DIR
079: + File.separator + MK_SERVER_DATA1).createNewFile();
080: return mockDataDir;
081: }
082:
083: private String createConfig(String clientLogs, String[] serverLogs,
084: String[] serverData) {
085: TerracottaConfigBuilder builder = new TerracottaConfigBuilder();
086: SystemConfigBuilder sysBuilder = new SystemConfigBuilder();
087: sysBuilder.setConfigurationModel("production");
088: builder.setSystem(sysBuilder);
089: L2ConfigBuilder[] l2Builder = new L2ConfigBuilder[serverLogs.length];
090: for (int i = 0; i < l2Builder.length; i++) {
091: l2Builder[i] = new L2ConfigBuilder();
092: l2Builder[i].setName("localhost-" + i);
093: l2Builder[i].setData(serverData[i]);
094: l2Builder[i].setLogs(serverLogs[i]);
095: }
096: L2SConfigBuilder l2SBuilder = new L2SConfigBuilder();
097: l2SBuilder.setL2s(l2Builder);
098: builder.setServers(l2SBuilder);
099: L1ConfigBuilder l1Builder = new L1ConfigBuilder();
100: l1Builder.setLogs(clientLogs);
101: builder.setClient(l1Builder);
102: return builder.toString();
103: }
104:
105: private File writeConfig(byte[] data) throws IOException {
106: System.out.println(new String(data));
107: File configFile = new File(mockDataDir + File.separator
108: + TC_CONFIG);
109: if (configFile.exists())
110: configFile.delete();
111: FileOutputStream out = new FileOutputStream(configFile);
112: out.write(data);
113: out.flush();
114: out.close();
115: return configFile;
116: }
117:
118: private Set listArchiveContents(File archive) throws IOException {
119: Set contents = new HashSet();
120: ZipInputStream in = new ZipInputStream(new BufferedInputStream(
121: new FileInputStream(archive)));
122: ZipEntry entry;
123: while ((entry = in.getNextEntry()) != null) {
124: contents.add(entry.getName());
125: in.closeEntry();
126: }
127: in.close();
128: return contents;
129: }
130:
131: private int executeArchiveUtil(String[] args) throws IOException,
132: InterruptedException {
133: String[] commandLine = new String[args.length + 4];
134: commandLine[0] = "java";
135: commandLine[1] = "-classpath";
136: commandLine[2] = System.getProperty("java.class.path");
137: commandLine[3] = ArchiveUtil.class.getName();
138: System.arraycopy(args, 0, commandLine, 4, args.length);
139: System.out
140: .println("\n***** Executing Java process -- ArchiveUtil *****\n");
141: Runtime runtime = Runtime.getRuntime();
142: Process process = runtime.exec(commandLine, new String[0],
143: mockDataDir);
144:
145: ExternalProcessStreamWriter writeSys = new ExternalProcessStreamWriter();
146: writeSys.printSys(process.getInputStream());
147: ExternalProcessStreamWriter writeErr = new ExternalProcessStreamWriter();
148: writeErr.printErr(process.getErrorStream());
149:
150: int code = process.waitFor();
151: if (writeSys.hasException())
152: throw writeSys.getException();
153: if (writeErr.hasException())
154: throw writeErr.getException();
155: return code;
156: }
157:
158: private void clear() {
159: if (archiveFile.exists())
160: archiveFile.delete();
161: }
162:
163: private void log(String str) {
164: System.out
165: .println("--------------------------------------------------------------------------------");
166: System.out.println("-- " + str);
167: System.out
168: .println("--------------------------------------------------------------------------------");
169: }
170:
171: public void testValidServerArchiveContents() throws Exception {
172: clear();
173: log("<server> -n valid archive contents");
174: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
175: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
176: String config = createConfig(NONE, slogs, sdata);
177: File configFile = writeConfig(config.getBytes());
178: String[] args = new String[] { "-n", configFile.toString() };
179: executeArchiveUtil(args);
180: DateFormat df = new SimpleDateFormat("y-M-d");
181: File defaultArchive = new File(mockDataDir + File.separator
182: + "tc-archive" + "_"
183: + df.format(new Date(System.currentTimeMillis()))
184: + ".zip");
185: Set contents = listArchiveContents(defaultArchive);
186: assertTrue(contents.contains(TC_CONFIG));
187: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
188: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
189: + MK_SERVER_LOG0));
190: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
191: + MK_SERVER_LOG1));
192: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"));
193: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"
194: + MK_SERVER_DATA0));
195: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"
196: + MK_SERVER_DATA1));
197: }
198:
199: public void testDirectoryArg() throws Exception {
200: clear();
201: log("<server> test directory argument contents");
202: String[] args = new String[] { mockDataDir + File.separator
203: + MK_SERVER_LOG_DIR };
204: executeArchiveUtil(args);
205: DateFormat df = new SimpleDateFormat("y-M-d");
206: File defaultArchive = new File(mockDataDir + File.separator
207: + "tc-archive" + "_"
208: + df.format(new Date(System.currentTimeMillis()))
209: + ".zip");
210: Set contents = listArchiveContents(defaultArchive);
211: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
212: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
213: + MK_SERVER_LOG0));
214: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
215: + MK_SERVER_LOG1));
216: }
217:
218: public void testInvalidDirectoryArg() throws Exception {
219: clear();
220: log("<server> test invalid directory argument");
221: String[] args = new String[] { "foo" + File.separator + "baz" };
222: executeArchiveUtil(args);
223: DateFormat df = new SimpleDateFormat("y-M-d");
224: File defaultArchive = new File(mockDataDir + File.separator
225: + "tc-archive" + "_"
226: + df.format(new Date(System.currentTimeMillis()))
227: + ".zip");
228: assertFalse(defaultArchive.exists());
229: }
230:
231: public void testFileArg() throws Exception {
232: clear();
233: log("<server> test file argument");
234: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
235: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
236: String config = createConfig(NONE, slogs, sdata);
237: File configFile = writeConfig(config.getBytes());
238: String[] args = new String[] { "-n", configFile.toString(),
239: archiveFile.toString() };
240: executeArchiveUtil(args);
241: Set contents = listArchiveContents(archiveFile);
242: assertTrue(contents.contains(TC_CONFIG));
243: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
244: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
245: + MK_SERVER_LOG0));
246: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
247: + MK_SERVER_LOG1));
248: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"));
249: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"
250: + MK_SERVER_DATA0));
251: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"
252: + MK_SERVER_DATA1));
253: }
254:
255: public void testValidServerFullArchiveContents() throws Exception {
256: clear();
257: log("<server> valid archive contents");
258: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
259: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
260: String config = createConfig(NONE, slogs, sdata);
261: File configFile = writeConfig(config.getBytes());
262: String[] args = new String[] { configFile.toString(),
263: archiveFile.toString() };
264: executeArchiveUtil(args);
265: Set contents = listArchiveContents(archiveFile);
266: assertTrue(contents.contains(TC_CONFIG));
267: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"));
268: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
269: + MK_SERVER_LOG0));
270: assertTrue(contents.contains(MK_SERVER_LOG_DIR + "/"
271: + MK_SERVER_LOG1));
272: assertTrue(contents.contains(MK_SERVER_DATA_DIR + "/"));
273: assertTrue(contents.contains(MK_SERVER_DATA_DIR + "/"
274: + MK_SERVER_DATA0));
275: assertTrue(contents.contains(MK_SERVER_DATA_DIR + "/"
276: + MK_SERVER_DATA1));
277: }
278:
279: public void testValidClientArchiveContents() throws Exception {
280: clear();
281: log("<client> valid archive contents");
282: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
283: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
284: String config = createConfig(MK_CLIENT_DIR, slogs, sdata);
285: File configFile = writeConfig(config.getBytes());
286: String[] args = new String[] { "-c", configFile.toString(),
287: archiveFile.toString() };
288: executeArchiveUtil(args);
289: Set contents = listArchiveContents(archiveFile);
290: assertTrue(contents.contains(TC_CONFIG));
291: assertTrue(contents.contains(MK_CLIENT_DIR + "/"));
292: assertTrue(contents.contains(MK_CLIENT_DIR + "/"
293: + MK_CLIENT_LOG0));
294: assertTrue(contents.contains(MK_CLIENT_DIR + "/"
295: + MK_CLIENT_LOG1));
296: assertFalse(contents.contains(MK_SERVER_LOG_DIR + "/"));
297: assertFalse(contents.contains(MK_SERVER_LOG_DIR + "/"
298: + MK_SERVER_LOG0));
299: assertFalse(contents.contains(MK_SERVER_LOG_DIR + "/"
300: + MK_SERVER_LOG1));
301: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"));
302: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"
303: + MK_SERVER_DATA0));
304: assertFalse(contents.contains(MK_SERVER_DATA_DIR + "/"
305: + MK_SERVER_DATA1));
306: }
307:
308: public void testIgnoresNOptionForClient() throws Exception {
309: clear();
310: log("<client> ignores -n option");
311: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
312: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
313: String config = createConfig(MK_CLIENT_DIR, slogs, sdata);
314: File configFile = writeConfig(config.getBytes());
315: executeArchiveUtil(new String[] { "-c", "-n",
316: configFile.toString(), archiveFile.toString() });
317: assertTrue(archiveFile.exists());
318: }
319:
320: public void testInvalidArgsOrder1() throws Exception {
321: clear();
322: log("<server> invalid args: <output file> <config>");
323: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
324: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
325: String config = createConfig(MK_CLIENT_DIR, slogs, sdata);
326: File configFile = writeConfig(config.getBytes());
327: executeArchiveUtil(new String[] { archiveFile.toString(),
328: configFile.toString() });
329: assertFalse(archiveFile.exists());
330: }
331:
332: public void testInvalidArgsOrder2() throws Exception {
333: clear();
334: log("<server> invalid args: <config> <output file> -c");
335: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
336: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
337: String config = createConfig(MK_CLIENT_DIR, slogs, sdata);
338: File configFile = writeConfig(config.getBytes());
339: executeArchiveUtil(new String[] { configFile.toString(),
340: archiveFile.toString(), "-c" });
341: assertFalse(archiveFile.exists());
342: }
343:
344: public void testInvalidArgsOrder3() throws Exception {
345: clear();
346: log("<server> invalid args: <config> -c <output file>");
347: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
348: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
349: String config = createConfig(MK_CLIENT_DIR, slogs, sdata);
350: File configFile = writeConfig(config.getBytes());
351: executeArchiveUtil(new String[] { configFile.toString(), "-c",
352: archiveFile.toString() });
353: assertFalse(archiveFile.exists());
354: }
355:
356: public void testInvalidArgs1() throws Exception {
357: clear();
358: log("<server> invalid args: config");
359: executeArchiveUtil(new String[] { "foo", archiveFile.toString() });
360: assertFalse(archiveFile.exists());
361: }
362:
363: public void testInvalidArgs2() throws Exception {
364: clear();
365: log("<server> invalid args: -x");
366: String[] slogs = new String[] { MK_SERVER_LOG_DIR };
367: String[] sdata = new String[] { MK_SERVER_DATA_DIR };
368: String config = createConfig(MK_CLIENT_DIR, slogs, sdata);
369: File configFile = writeConfig(config.getBytes());
370: executeArchiveUtil(new String[] { "-x", configFile.toString(),
371: archiveFile.toString() });
372: assertFalse(archiveFile.exists());
373: }
374:
375: public void testInvalidConfig1() throws Exception {
376: clear();
377: log("<server> invalid config: <foo>");
378: File configFile = writeConfig("foo".getBytes());
379: executeArchiveUtil(new String[] { configFile.toString(),
380: archiveFile.toString() });
381: assertFalse(archiveFile.exists());
382: }
383:
384: public void testInvalidConfig2() throws Exception {
385: clear();
386: log("<server> invalid config: logs=foo");
387: String[] slogs = new String[] { "foo" };
388: String config = createConfig(MK_CLIENT_DIR, slogs,
389: new String[] { NONE });
390: File configFile = writeConfig(config.getBytes());
391: executeArchiveUtil(new String[] { configFile.toString(),
392: archiveFile.toString() });
393: assertFalse(archiveFile.exists());
394: }
395:
396: public void testInvalidConfig3() throws Exception {
397: clear();
398: log("<server> invalid config: data=foo");
399: String[] sdata = new String[] { "foo" };
400: String config = createConfig(MK_CLIENT_DIR,
401: new String[] { NONE }, sdata);
402: File configFile = writeConfig(config.getBytes());
403: executeArchiveUtil(new String[] { configFile.toString(),
404: archiveFile.toString() });
405: assertFalse(archiveFile.exists());
406: }
407:
408: public void testInvalidConfig4() throws Exception {
409: clear();
410: log("<client> invalid config: logs=foo");
411: String config = createConfig("foo", new String[] { NONE },
412: new String[] { NONE });
413: File configFile = writeConfig(config.getBytes());
414: executeArchiveUtil(new String[] { "-c", configFile.toString(),
415: archiveFile.toString() });
416: assertFalse(archiveFile.exists());
417: }
418:
419: // TODO: wildcards will not be tested CDV-93
420: }
|