1 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 ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.text.DateFormat ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 import java.util.zip.ZipEntry ; 26 import java.util.zip.ZipInputStream ; 27 28 public final class ArchiveUtilTest extends TCTestCase { 29 30 private static final String NONE = "none"; 31 private static final String TC_CONFIG = "tc-config.xml"; 32 private static final String ARCHIVE = "mock-archive.zip"; 33 private static final String MK_DATA_DIR = "mockdata"; 34 private static final String MK_CLIENT_DIR = "client-logs"; 35 private static final String MK_CLIENT_LOG0 = "terracotta-client.log"; 36 private static final String MK_CLIENT_LOG1 = "terracotta-client.log.1"; 37 private static final String MK_SERVER_LOG_DIR = "server-logs"; 38 private static final String MK_SERVER_LOG0 = "terracotta-server.log"; 39 private static final String MK_SERVER_LOG1 = "terracotta-server.log.1"; 40 private static final String MK_SERVER_DATA_DIR = "server-data"; 41 private static final String MK_SERVER_DATA0 = "objectdb"; 42 private static final String MK_SERVER_DATA1 = "startup.lck"; 43 44 private File mockDataDir; 45 private File archiveFile; 46 47 public ArchiveUtilTest() { 48 } 50 51 public void setUp() throws Exception { 52 generateMockFiles(getTempDirectory()); 53 archiveFile = new File (mockDataDir + File.separator + ARCHIVE); 54 } 55 56 public void tearDown() { 57 clear(); 58 } 59 60 private File generateMockFiles(File tmpDir) throws IOException { 61 mockDataDir = new File (tmpDir + File.separator + MK_DATA_DIR); 62 mockDataDir.mkdir(); 63 new File (mockDataDir + File.separator + MK_CLIENT_DIR).mkdir(); 64 new File (mockDataDir + File.separator + MK_CLIENT_DIR + File.separator + MK_CLIENT_LOG0).createNewFile(); 65 new File (mockDataDir + File.separator + MK_CLIENT_DIR + File.separator + MK_CLIENT_LOG1).createNewFile(); 66 new File (mockDataDir + File.separator + MK_SERVER_LOG_DIR).mkdir(); 67 new File (mockDataDir + File.separator + MK_SERVER_LOG_DIR + File.separator + MK_SERVER_LOG0).createNewFile(); 68 new File (mockDataDir + File.separator + MK_SERVER_LOG_DIR + File.separator + MK_SERVER_LOG1).createNewFile(); 69 new File (mockDataDir + File.separator + MK_SERVER_DATA_DIR).mkdir(); 70 new File (mockDataDir + File.separator + MK_SERVER_DATA_DIR + File.separator + MK_SERVER_DATA0).createNewFile(); 71 new File (mockDataDir + File.separator + MK_SERVER_DATA_DIR + File.separator + MK_SERVER_DATA1).createNewFile(); 72 return mockDataDir; 73 } 74 75 private String createConfig(String clientLogs, String [] serverLogs, String [] 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 writeConfig(byte[] data) throws IOException { 97 System.out.println(new String (data)); 98 File configFile = new File (mockDataDir + File.separator + TC_CONFIG); 99 if (configFile.exists()) configFile.delete(); 100 FileOutputStream out = new FileOutputStream (configFile); 101 out.write(data); 102 out.flush(); 103 out.close(); 104 return configFile; 105 } 106 107 private Set listArchiveContents(File archive) throws IOException { 108 Set contents = new HashSet (); 109 ZipInputStream in = new ZipInputStream (new BufferedInputStream (new FileInputStream (archive))); 110 ZipEntry 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 [] args) throws IOException , InterruptedException { 120 String [] commandLine = new String [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 runtime = Runtime.getRuntime(); 128 Process process = runtime.exec(commandLine, new String [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 str) { 146 System.out.println("--------------------------------------------------------------------------------"); 147 System.out.println("-- " + str); 148 System.out.println("--------------------------------------------------------------------------------"); 149 } 150 151 public void testValidServerArchiveContents() throws Exception { 152 clear(); 153 log("<server> -n valid archive contents"); 154 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 155 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 156 String config = createConfig(NONE, slogs, sdata); 157 File configFile = writeConfig(config.getBytes()); 158 String [] args = new String [] { "-n", configFile.toString() }; 159 executeArchiveUtil(args); 160 DateFormat df = new SimpleDateFormat ("y-M-d"); 161 File defaultArchive = new File (mockDataDir + File.separator + "tc-archive" + "_" 162 + df.format(new Date (System.currentTimeMillis())) + ".zip"); 163 Set 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 { 174 clear(); 175 log("<server> test directory argument contents"); 176 String [] args = new String [] { mockDataDir + File.separator + MK_SERVER_LOG_DIR }; 177 executeArchiveUtil(args); 178 DateFormat df = new SimpleDateFormat ("y-M-d"); 179 File defaultArchive = new File (mockDataDir + File.separator + "tc-archive" + "_" 180 + df.format(new Date (System.currentTimeMillis())) + ".zip"); 181 Set 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 { 188 clear(); 189 log("<server> test invalid directory argument"); 190 String [] args = new String [] { "foo" + File.separator + "baz" }; 191 executeArchiveUtil(args); 192 DateFormat df = new SimpleDateFormat ("y-M-d"); 193 File defaultArchive = new File (mockDataDir + File.separator + "tc-archive" + "_" 194 + df.format(new Date (System.currentTimeMillis())) + ".zip"); 195 assertFalse(defaultArchive.exists()); 196 } 197 198 public void testFileArg() throws Exception { 199 clear(); 200 log("<server> test file argument"); 201 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 202 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 203 String config = createConfig(NONE, slogs, sdata); 204 File configFile = writeConfig(config.getBytes()); 205 String [] args = new String [] { "-n", configFile.toString(), archiveFile.toString() }; 206 executeArchiveUtil(args); 207 Set 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 { 218 clear(); 219 log("<server> valid archive contents"); 220 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 221 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 222 String config = createConfig(NONE, slogs, sdata); 223 File configFile = writeConfig(config.getBytes()); 224 String [] args = new String [] { configFile.toString(), archiveFile.toString() }; 225 executeArchiveUtil(args); 226 Set 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 { 237 clear(); 238 log("<client> valid archive contents"); 239 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 240 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 241 String config = createConfig(MK_CLIENT_DIR, slogs, sdata); 242 File configFile = writeConfig(config.getBytes()); 243 String [] args = new String [] { "-c", configFile.toString(), archiveFile.toString() }; 244 executeArchiveUtil(args); 245 Set 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 { 259 clear(); 260 log("<client> ignores -n option"); 261 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 262 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 263 String config = createConfig(MK_CLIENT_DIR, slogs, sdata); 264 File configFile = writeConfig(config.getBytes()); 265 executeArchiveUtil(new String [] { "-c", "-n", configFile.toString(), archiveFile.toString() }); 266 assertTrue(archiveFile.exists()); 267 } 268 269 public void testInvalidArgsOrder1() throws Exception { 270 clear(); 271 log("<server> invalid args: <output file> <config>"); 272 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 273 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 274 String config = createConfig(MK_CLIENT_DIR, slogs, sdata); 275 File configFile = writeConfig(config.getBytes()); 276 executeArchiveUtil(new String [] { archiveFile.toString(), configFile.toString() }); 277 assertFalse(archiveFile.exists()); 278 } 279 280 public void testInvalidArgsOrder2() throws Exception { 281 clear(); 282 log("<server> invalid args: <config> <output file> -c"); 283 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 284 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 285 String config = createConfig(MK_CLIENT_DIR, slogs, sdata); 286 File configFile = writeConfig(config.getBytes()); 287 executeArchiveUtil(new String [] { configFile.toString(), archiveFile.toString(), "-c" }); 288 assertFalse(archiveFile.exists()); 289 } 290 291 public void testInvalidArgsOrder3() throws Exception { 292 clear(); 293 log("<server> invalid args: <config> -c <output file>"); 294 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 295 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 296 String config = createConfig(MK_CLIENT_DIR, slogs, sdata); 297 File configFile = writeConfig(config.getBytes()); 298 executeArchiveUtil(new String [] { configFile.toString(), "-c", archiveFile.toString() }); 299 assertFalse(archiveFile.exists()); 300 } 301 302 public void testInvalidArgs1() throws Exception { 303 clear(); 304 log("<server> invalid args: config"); 305 executeArchiveUtil(new String [] { "foo", archiveFile.toString() }); 306 assertFalse(archiveFile.exists()); 307 } 308 309 public void testInvalidArgs2() throws Exception { 310 clear(); 311 log("<server> invalid args: -x"); 312 String [] slogs = new String [] { MK_SERVER_LOG_DIR }; 313 String [] sdata = new String [] { MK_SERVER_DATA_DIR }; 314 String config = createConfig(MK_CLIENT_DIR, slogs, sdata); 315 File configFile = writeConfig(config.getBytes()); 316 executeArchiveUtil(new String [] { "-x", configFile.toString(), archiveFile.toString() }); 317 assertFalse(archiveFile.exists()); 318 } 319 320 public void testInvalidConfig1() throws Exception { 321 clear(); 322 log("<server> invalid config: <foo>"); 323 File configFile = writeConfig("foo".getBytes()); 324 executeArchiveUtil(new String [] { configFile.toString(), archiveFile.toString() }); 325 assertFalse(archiveFile.exists()); 326 } 327 328 public void testInvalidConfig2() throws Exception { 329 clear(); 330 log("<server> invalid config: logs=foo"); 331 String [] slogs = new String [] { "foo" }; 332 String config = createConfig(MK_CLIENT_DIR, slogs, new String [] { NONE }); 333 File configFile = writeConfig(config.getBytes()); 334 executeArchiveUtil(new String [] { configFile.toString(), archiveFile.toString() }); 335 assertFalse(archiveFile.exists()); 336 } 337 338 public void testInvalidConfig3() throws Exception { 339 clear(); 340 log("<server> invalid config: data=foo"); 341 String [] sdata = new String [] { "foo" }; 342 String config = createConfig(MK_CLIENT_DIR, new String [] { NONE }, sdata); 343 File configFile = writeConfig(config.getBytes()); 344 executeArchiveUtil(new String [] { configFile.toString(), archiveFile.toString() }); 345 assertFalse(archiveFile.exists()); 346 } 347 348 public void testInvalidConfig4() throws Exception { 349 clear(); 350 log("<client> invalid config: logs=foo"); 351 String config = createConfig("foo", new String [] { NONE }, new String [] { NONE }); 352 File configFile = writeConfig(config.getBytes()); 353 executeArchiveUtil(new String [] { "-c", configFile.toString(), archiveFile.toString() }); 354 assertFalse(archiveFile.exists()); 355 } 356 357 } | Popular Tags |