001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestAutoAdminTask.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.system;
030:
031: import java.io.File;
032: import javax.management.ObjectName;
033: import javax.management.StandardMBean;
034:
035: import com.sun.jbi.management.MBeanNames;
036:
037: import com.sun.jbi.management.system.AutoAdminTask;
038: import com.sun.jbi.management.system.ManagementContext;
039: import com.sun.jbi.management.system.ScaffoldEnvironmentContext;
040:
041: import com.sun.jbi.management.util.FileHelper;
042:
043: /**
044: * Tests some methods in AutoAdminTask
045: * @author Sun Microsystems, Inc.
046: */
047:
048: public class TestAutoAdminTask extends junit.framework.TestCase {
049:
050: /**
051: * Current test name.
052: */
053: private String mTestName;
054:
055: /**
056: * Helper class to hold created Environment Context
057: */
058: private ScaffoldEnvironmentContext mContext;
059:
060: /**
061: * Helper class to hold management context
062: */
063: private ManagementContext mCtx;
064:
065: /**
066: * the actual installation configuration service MBean
067: */
068: private ObjectName mInstallConfigSvcMBean;
069:
070: /**
071: * the actual deployment configuration service MBean
072: */
073: private ObjectName mDeployConfigSvcMBean;
074:
075: /**
076: * The AutoAdminTask class being tested
077: */
078: private AutoAdminTask autoAdminTask;
079:
080: /**
081: * A temporary directory to use for autoinstall
082: */
083: private String mAutoInstallDir;
084:
085: /**
086: * A temporary directory to use for autoinstall
087: */
088: private String mAutoDeployDir;
089:
090: /**
091: * Holders of various files created during the directory population
092: */
093: static private File newF1; // these two only exist in the top directory
094: static private File newF2;
095: static private File deletedF1; // these two only exist in the status dir.
096: static private File deletedF2;
097: static private File oldF1; // these exist in both directories, with
098: static private File tsF1; // the same timestamp.
099: static private File oldF2;
100: static private File tsF2;
101: static private File updatedF1; // these exist in both directories, with
102: static private File updatedF2; // the top version "newer" the the status
103: static private File tsuF1; // directory version.
104: static private File tsuF2;
105:
106: /**
107: * The constructor for this testcase, forwards the test name to
108: * the jUnit TestCase base class.
109: * @param aTestName String with the name of this test.
110: */
111: public TestAutoAdminTask(String aTestName) throws Exception {
112: super (aTestName);
113: mTestName = aTestName;
114: }
115:
116: /**
117: * Setup for the test.
118: * @throws Exception when setup fails for any reason.
119: */
120: public void setUp() throws Exception {
121: super .setUp();
122: System.err.println("***** Start of test " + mTestName);
123:
124: // create and set up the Environment Context
125: mContext = new ScaffoldEnvironmentContext();
126:
127: mCtx = new ManagementContext(mContext);
128:
129: mInstallConfigSvcMBean = mCtx
130: .getMBeanNames()
131: .getSystemServiceMBeanName(
132: MBeanNames.ServiceName.ConfigurationService,
133: MBeanNames.ServiceType.Installation,
134: mContext.getPlatformContext().getInstanceName());
135:
136: mDeployConfigSvcMBean = mCtx
137: .getMBeanNames()
138: .getSystemServiceMBeanName(
139: MBeanNames.ServiceName.ConfigurationService,
140: MBeanNames.ServiceType.Deployment,
141: mContext.getPlatformContext().getInstanceName());
142:
143: mAutoInstallDir = System.getProperty("junit.srcroot")
144: + "/bld/autoinstall";
145: mAutoDeployDir = System.getProperty("junit.srcroot")
146: + "/bld/autodeploy";
147:
148: autoAdminTask = new AutoAdminTask(mCtx, mAutoInstallDir,
149: mAutoDeployDir, true, false);
150: }
151:
152: /**
153: * Cleanup for the test.
154: * @throws Exception when cleanup fails for any reason.
155: */
156: public void tearDown() throws Exception {
157: super .tearDown();
158: System.err.println("***** End of test " + mTestName);
159: }
160:
161: // =============================== test methods ===============================
162:
163: /*
164: * Test that autoDirectories does not create directories when told not to.
165: * @throws Exception when test fails for any reason.
166: */
167: public void testInitDirectoriesFalseFalse() throws Exception {
168: File instDir = new File(mAutoInstallDir);
169: assertFalse("auto install directory(" + mAutoInstallDir
170: + ") already exists.", instDir.exists());
171:
172: autoAdminTask.initDirectories(false, false);
173: assertFalse("auto install directory(" + mAutoInstallDir
174: + ") was created.", instDir.exists());
175: }
176:
177: /*
178: * Test that autoDirectories does not create directories when told not to.
179: * @throws Exception when test fails for any reason.
180: */
181: public void testInitDirectoriesTrueFalse() throws Exception {
182: File instDir = new File(mAutoInstallDir);
183: File deplDir = new File(mAutoDeployDir);
184: assertFalse("auto install directory(" + mAutoInstallDir
185: + ") already exists.", instDir.exists());
186: assertFalse("auto deploy directory(" + mAutoDeployDir
187: + ") already exists.", deplDir.exists());
188:
189: autoAdminTask.initDirectories(true, false);
190: assertTrue("auto install directory(" + mAutoInstallDir
191: + ") was not created.", instDir.exists());
192: assertFalse("auto deploy directory(" + mAutoDeployDir
193: + ") was wrongly created.", deplDir.exists());
194: }
195:
196: /*
197: * Test that autoDirectories does not create directories when told not to.
198: * @throws Exception when test fails for any reason.
199: */
200: public void testInitDirectoriesFalseTrue() throws Exception {
201: File instDir = new File(mAutoInstallDir);
202: File deplDir = new File(mAutoDeployDir);
203: assertTrue("auto install directory(" + mAutoInstallDir
204: + ") doesn't exist.", instDir.exists());
205: assertFalse("auto deploy directory(" + mAutoDeployDir
206: + ") already exists.", deplDir.exists());
207:
208: autoAdminTask.initDirectories(false, true);
209: assertTrue("auto install directory(" + mAutoInstallDir
210: + ") was deleted.", instDir.exists());
211: assertTrue("auto install directory(" + mAutoInstallDir
212: + ") was not created.", deplDir.exists());
213: }
214:
215: /*
216: * Test that pollAutoDirectory returns null when no files are present.
217: * @throws Exception when test fails for any reason.
218: */
219: public void testPollAutoDirectoryNewEmpty() throws Exception {
220: File instDir = new File(mAutoInstallDir);
221: File statusDir = new File(mAutoInstallDir, ".autoinstallstatus");
222: assertTrue(".autoinstallstatus directory does not exist.",
223: statusDir.exists());
224:
225: File results = autoAdminTask.pollAutoDirectory(
226: AutoAdminTask.PollFunction.NEW_FILES, instDir,
227: statusDir);
228: assertNull("pollAutoDirectory returned a non-null file",
229: results);
230: }
231:
232: /*
233: * Test that pollAutoDirectory returns null when no files are present.
234: * @throws Exception when test fails for any reason.
235: */
236: public void testPollAutoDirectoryDeletedEmpty() throws Exception {
237: File instDir = new File(mAutoInstallDir);
238: File statusDir = new File(mAutoInstallDir, ".autoinstallstatus");
239: assertTrue(".autoinstallstatus directory does not exist.",
240: statusDir.exists());
241:
242: File results = autoAdminTask.pollAutoDirectory(
243: AutoAdminTask.PollFunction.DELETED_FILES, instDir,
244: statusDir);
245: assertNull("pollAutoDirectory returned a non-null file",
246: results);
247: }
248:
249: /*
250: * Test that pollAutoDirectory returns null when no files are present.
251: * @throws Exception when test fails for any reason.
252: */
253: public void testPollAutoDirectoryUpdatedEmpty() throws Exception {
254: File instDir = new File(mAutoInstallDir);
255: File statusDir = new File(mAutoInstallDir, ".autoinstallstatus");
256: assertTrue(".autoinstallstatus directory does not exist.",
257: statusDir.exists());
258:
259: File results = autoAdminTask.pollAutoDirectory(
260: AutoAdminTask.PollFunction.UPDATED_FILES, instDir,
261: statusDir);
262: assertNull("pollAutoDirectory returned a non-null file",
263: results);
264: }
265:
266: /*
267: * Test that pollAutoDirectory returns the right files under a complex
268: * scenario with new and processed files in place.
269: * @throws Exception when test fails for any reason.
270: */
271: public void testPollAutoDirectoryNewFilled() throws Exception {
272: File instDir = new File(mAutoInstallDir);
273: File statusDir = new File(mAutoInstallDir, ".autoinstallstatus");
274: assertTrue(".autoinstallstatus directory does not exist.",
275: statusDir.exists());
276:
277: // create some "new" files
278: newF1 = new File(instDir, "new1.jar");
279: assertTrue("could not create new file1", newF1.createNewFile());
280: newF2 = new File(instDir, "new2.jar");
281: assertTrue("could not create new file2", newF2.createNewFile());
282:
283: // create some "deleted" files
284: deletedF1 = new File(statusDir, "deleted1.jar");
285: assertTrue("could not create deleted file1", deletedF1
286: .createNewFile());
287: deletedF2 = new File(statusDir, "deleted2.jar");
288: assertTrue("could not create deleted file2", deletedF2
289: .createNewFile());
290:
291: // create some "old" (already installed) files
292: oldF1 = new File(instDir, "old1.jar");
293: assertTrue("could not create old file1", oldF1.createNewFile());
294: tsF1 = new File(statusDir, "old1.jar");
295: assertTrue("could not create timestamp file1", tsF1
296: .createNewFile());
297: oldF2 = new File(instDir, "old2.jar");
298: assertTrue("could not create old file2", oldF2.createNewFile());
299: tsF2 = new File(statusDir, "old2.jar");
300: assertTrue("could not create timestamp file2", tsF2
301: .createNewFile());
302: tsF1.setLastModified(oldF1.lastModified());
303: tsF2.setLastModified(oldF2.lastModified());
304:
305: // create some "updated" (already installed, then touched) files
306: updatedF1 = new File(instDir, "updated1.jar");
307: assertTrue("could not create updated file1", updatedF1
308: .createNewFile());
309: tsuF1 = new File(statusDir, "updated1.jar");
310: assertTrue("could not create timestamp file1", tsuF1
311: .createNewFile());
312: updatedF2 = new File(instDir, "updated2.jar");
313: assertTrue("could not create updated file2", updatedF2
314: .createNewFile());
315: tsuF2 = new File(statusDir, "updated2.jar");
316: assertTrue("could not create timestamp file2", tsuF2
317: .createNewFile());
318: tsuF1.setLastModified(updatedF1.lastModified() - 300);
319: tsuF2.setLastModified(updatedF2.lastModified() - 300);
320:
321: File results = autoAdminTask.pollAutoDirectory(
322: AutoAdminTask.PollFunction.NEW_FILES, instDir,
323: statusDir);
324: assertFalse(
325: "pollAutoDirectory returned null -- expecting F1 or F2",
326: (results == null));
327:
328: boolean foundOne = results.equals(newF1);
329: boolean foundTwo = results.equals(newF2);
330: if (foundOne) {
331: System.err.println(" (found F1)");
332: }
333: if (foundTwo) {
334: System.err.println(" (found F2)");
335: }
336: assertTrue("pollAutoDirectory found " + results.getName()
337: + " instead of F1 or F2.", foundOne | foundTwo);
338:
339: // remove the file, so we can find the other file
340: assertTrue("could not remove file " + results.getName() + ".",
341: results.delete());
342:
343: // second call to pollAutoDirectory
344: results = autoAdminTask.pollAutoDirectory(
345: AutoAdminTask.PollFunction.NEW_FILES, instDir,
346: statusDir);
347: foundOne = foundOne | newF1.equals(results);
348: foundTwo = foundTwo | newF2.equals(results);
349: assertTrue("pollAutoDirectory found " + results.getName()
350: + " instead of F1 and F2.", foundOne & foundTwo);
351:
352: // remove the file, so we can be sure that we are done
353: assertTrue("could not remove file " + results.getName() + ".",
354: results.delete());
355:
356: // third and last call to pollAutoDirectory
357: results = autoAdminTask.pollAutoDirectory(
358: AutoAdminTask.PollFunction.NEW_FILES, instDir,
359: statusDir);
360: assertNull("pollAutoDirectory returned a non-null file",
361: results);
362: }
363:
364: // when phase 5 is completed, there will be a new Filled() test here
365: // when phase 6 is completed, there will be a new Filled() test here
366:
367: /*
368: * Test that pollAutoDirectory returns the right files under a complex
369: * scenario with new and processed files in place.
370: * @throws Exception when test fails for any reason.
371: */
372: public void testPollAutoDirectoryDeletedFilled() throws Exception {
373: File instDir = new File(mAutoInstallDir);
374: File statusDir = new File(mAutoInstallDir, ".autoinstallstatus");
375:
376: File results = autoAdminTask.pollAutoDirectory(
377: AutoAdminTask.PollFunction.DELETED_FILES, instDir,
378: statusDir);
379: assertFalse(
380: "pollAutoDirectory returned null -- expecting D1 or D2",
381: (results == null));
382:
383: boolean foundOne = results.equals(deletedF1);
384: boolean foundTwo = results.equals(deletedF2);
385: if (foundOne) {
386: System.err.println(" (found D1)");
387: }
388: if (foundTwo) {
389: System.err.println(" (found D2)");
390: }
391: assertTrue("pollAutoDirectory found " + results.getName()
392: + " instead of D1 or D2.", foundOne | foundTwo);
393:
394: // remove the file, so we can find the other file
395: assertTrue("could not remove file " + results.getName() + ".",
396: results.delete());
397:
398: // second call to pollAutoDirectory
399: results = autoAdminTask.pollAutoDirectory(
400: AutoAdminTask.PollFunction.DELETED_FILES, instDir,
401: statusDir);
402: foundOne = foundOne | deletedF1.equals(results);
403: foundTwo = foundTwo | deletedF2.equals(results);
404: assertTrue("pollAutoDirectory found " + results.getName()
405: + " instead of D1 and D2.", foundOne & foundTwo);
406:
407: // remove the file, so we can be sure that we are done
408: assertTrue("could not remove file " + results.getName() + ".",
409: results.delete());
410:
411: // third and last call to pollAutoDirectory
412: results = autoAdminTask.pollAutoDirectory(
413: AutoAdminTask.PollFunction.DELETED_FILES, instDir,
414: statusDir);
415: assertNull("pollAutoDirectory returned a non-null file",
416: results);
417: }
418:
419: /*
420: * Test that pollAutoDirectory returns the right files under a complex
421: * scenario with new and processed files in place.
422: * @throws Exception when test fails for any reason.
423: */
424: public void testPollAutoDirectoryUpdatedFilled() throws Exception {
425: File instDir = new File(mAutoInstallDir);
426: File statusDir = new File(mAutoInstallDir, ".autoinstallstatus");
427:
428: File results = autoAdminTask.pollAutoDirectory(
429: AutoAdminTask.PollFunction.UPDATED_FILES, instDir,
430: statusDir);
431: assertFalse(
432: "pollAutoDirectory returned null -- expecting U1 or U2",
433: (results == null));
434:
435: boolean foundOne = results.equals(updatedF1);
436: boolean foundTwo = results.equals(updatedF2);
437: if (foundOne) {
438: System.err.println(" (found U1)");
439: }
440: if (foundTwo) {
441: System.err.println(" (found U2)");
442: }
443: assertTrue("pollAutoDirectory found " + results.getName()
444: + " instead of U1 or U2.", foundOne | foundTwo);
445:
446: // remove the file, so we can find the other file
447: assertTrue("could not remove file " + results.getName() + ".",
448: results.delete());
449:
450: // second call to pollAutoDirectory
451: results = autoAdminTask.pollAutoDirectory(
452: AutoAdminTask.PollFunction.UPDATED_FILES, instDir,
453: statusDir);
454: foundOne = foundOne | updatedF1.equals(results);
455: foundTwo = foundTwo | updatedF2.equals(results);
456: assertTrue("pollAutoDirectory found " + results.getName()
457: + " instead of U1 and U2.", foundOne & foundTwo);
458:
459: // remove the file, so we can be sure that we are done
460: assertTrue("could not remove file " + results.getName() + ".",
461: results.delete());
462:
463: // third and last call to pollAutoDirectory
464: results = autoAdminTask.pollAutoDirectory(
465: AutoAdminTask.PollFunction.UPDATED_FILES, instDir,
466: statusDir);
467: assertNull("pollAutoDirectory returned a non-null file",
468: results);
469: }
470:
471: /*
472: * Test that exists to clean up the auto* directories
473: * @throws Exception when test fails for any reason.
474: */
475: public void testCleanUp() throws Exception {
476: File instDir = new File(mAutoInstallDir);
477: File deplDir = new File(mAutoDeployDir);
478: assertTrue("could not clean directory(" + mAutoInstallDir
479: + ").", FileHelper.cleanDirectory(instDir));
480: assertTrue("could not delete directory(" + mAutoInstallDir
481: + ").", instDir.delete());
482: assertTrue(
483: "could not clean directory(" + mAutoDeployDir + ").",
484: FileHelper.cleanDirectory(deplDir));
485: assertTrue("could not delete directory(" + mAutoDeployDir
486: + ").", deplDir.delete());
487: }
488:
489: // the following methods of AutoApplyTask actually do perform installs and
490: // deploys (and require the App Server to be running), and so are not
491: // appropriate for testing with junit:
492: // performAutoDeploy
493: // performAutoInstall
494: // performAutoFunctions
495:
496: }
|