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: * @(#)TestRepository.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.repository;
030:
031: import com.sun.jbi.management.internal.support.DirectoryUtil;
032: import com.sun.jbi.management.system.ScaffoldEnvironmentContext;
033: import com.sun.jbi.management.system.ManagementContext;
034:
035: import java.io.File;
036: import java.util.Calendar;
037: import java.util.jar.JarFile;
038: import java.util.regex.Pattern;
039:
040: public class TestRepository extends junit.framework.TestCase {
041: private static final String JBI_ROOT_PATH = "/bld";
042:
043: private static final String COMPONENT_ARCHIVE_PATH = "/testdata/component.zip";
044:
045: private static final String BINDING_COMPONENT_ARCHIVE_PATH = "/testdata/jmsbinding.jar";
046:
047: private static final String COMPONENT_ARCHIVE_NAME = "SunSequencingEngine";
048:
049: private static final String BINDING_COMPONENT_ARCHIVE_NAME = "SunJMSBinding";
050:
051: private static final String SERVICE_ASSEMBLY_ARCHIVE = "service-assembly.zip";
052:
053: private static final String SA_WITH_BAD_SU_ARCHIVE_PATH = "/testdata/sa-bad-su.zip";
054:
055: private static final String SERVICE_ASSEMBLY_ARCHIVE_PATH = "/testdata/"
056: + SERVICE_ASSEMBLY_ARCHIVE;
057:
058: private static final String SERVICE_ASSEMBLY_NAME = "CompositeApplication";
059:
060: private static final String BOGUS_SERVICE_ASSEMBLY_NAME = "CompositeApp";
061:
062: private static final String SERVICE_UNIT_1_NAME = "ESB_ADMIN_SERVICE_UNIT_1";
063:
064: private static final String SERVICE_UNIT_2_NAME = "ESB_ADMIN_SERVICE_UNIT_2";
065:
066: private static final String SERVICE_UNIT_1_PATH = "su1.jar";
067:
068: private static final String SERVICE_UNIT_2_PATH = "su2.jar";
069:
070: private Repository mRepository;
071: private ManagementContext mCtx;
072: private String mJbiRoot;
073:
074: public TestRepository(String aTestName) throws Exception {
075: super (aTestName);
076:
077: String srcroot = System.getProperty("junit.srcroot");
078: String manage = "/runtime/manage"; // open-esb build
079: String blddir = "/bld/test-classes"; // open-esb build
080:
081: java.io.File f = new java.io.File(srcroot + manage);
082: if (!f.exists()) {
083: manage = "/shasta/manage"; // mainline/whitney build
084: blddir = "/bld/regress"; // mainline/whitney build
085: }
086:
087: mJbiRoot = srcroot + manage + JBI_ROOT_PATH;
088: ScaffoldEnvironmentContext envCtx = new ScaffoldEnvironmentContext();
089: envCtx.setJbiInstanceRoot(mJbiRoot);
090: envCtx.setAppServerInstanceRoot(mJbiRoot);
091: envCtx.setProperty("com.sun.jbi.home", System
092: .getProperty("junit.as8base")
093: + "/jbi");
094: envCtx.setJbiInstallRoot(System.getProperty("junit.as8base")
095: + "/jbi");
096: com.sun.jbi.util.EnvironmentAccess.setContext(envCtx);
097: mJbiRoot = srcroot + manage + blddir;
098: mCtx = new ManagementContext(envCtx);
099:
100: mRepository = new Repository(mCtx);
101: }
102:
103: public void setUp() throws Exception {
104: super .setUp();
105:
106: mRepository.purge();
107: }
108:
109: public void tearDown() throws Exception {
110: mRepository.purge();
111: }
112:
113: public void testAddComponentArchive() throws Exception {
114: String path = mJbiRoot + COMPONENT_ARCHIVE_PATH;
115: Archive archive;
116:
117: archive = mRepository.addArchive(ArchiveType.COMPONENT, path);
118:
119: // verify that the archive path now points to a repository location
120: assertTrue(!archive.getPath().equals(path));
121:
122: // verify upload timestamp is set correctly
123: Calendar up1 = archive.getUploadTimestamp();
124: Thread.sleep(200);
125: Calendar up2 = archive.getUploadTimestamp();
126: assertTrue(up2.getTimeInMillis() == up1.getTimeInMillis());
127: }
128:
129: public void testAddComponentArchiveWrongType() {
130: try {
131: mRepository.addArchive(ArchiveType.SHARED_LIBRARY, mJbiRoot
132: + COMPONENT_ARCHIVE_PATH);
133: } catch (RepositoryException repEx) {
134: // expected
135: return;
136: }
137:
138: fail("Added component to repository as a shared library.");
139: }
140:
141: public void testAddComponentArchiveDuplicate() throws Exception {
142: // add it once
143: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
144: + COMPONENT_ARCHIVE_PATH);
145:
146: try {
147: // try a second time
148: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
149: + COMPONENT_ARCHIVE_PATH);
150: } catch (RepositoryException repEx) {
151: // expected
152: return;
153: }
154:
155: fail("Added duplicate component to repository.");
156: }
157:
158: public void testRemoveComponentArchive() throws Exception {
159: // add the archive
160: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
161: + COMPONENT_ARCHIVE_PATH);
162:
163: // remove the archive
164: mRepository.removeArchive(ArchiveType.COMPONENT,
165: COMPONENT_ARCHIVE_NAME);
166:
167: // attempt to find the archive
168: Archive archive = mRepository.getArchive(ArchiveType.COMPONENT,
169: COMPONENT_ARCHIVE_NAME);
170: assertTrue(archive == null);
171: }
172:
173: public void testRemoveComponentArchiveBad() throws Exception {
174: mRepository.removeArchive(ArchiveType.COMPONENT, "foo");
175: }
176:
177: public void testFindComponentArchive() throws Exception {
178: // add the archive
179: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
180: + COMPONENT_ARCHIVE_PATH);
181:
182: // try and find it
183: String path = mRepository.findArchive(ArchiveType.COMPONENT,
184: COMPONENT_ARCHIVE_NAME);
185:
186: assertTrue(path != null);
187: }
188:
189: public void testAddServiceAssemblyArchive() throws Exception {
190: String path;
191: Archive archive;
192: File sa_file;
193: File su1_dir;
194: File su1_file;
195: File su2_dir;
196: File su2_file;
197:
198: path = mJbiRoot + SERVICE_ASSEMBLY_ARCHIVE_PATH;
199: archive = mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
200: path);
201: sa_file = new File(archive.getPath());
202:
203: // verify that the archive path now points to a repository location
204: assertTrue(!archive.getPath().equals(path));
205:
206: su1_dir = new File(sa_file.getParent(), SERVICE_UNIT_1_NAME);
207: su2_dir = new File(sa_file.getParent(), SERVICE_UNIT_2_NAME);
208:
209: // verify that the service unit directories were created
210: assertTrue(su1_dir.exists());
211: assertTrue(su2_dir.exists());
212:
213: su1_file = new File(su1_dir, SERVICE_UNIT_1_PATH);
214: su2_file = new File(su2_dir, SERVICE_UNIT_2_PATH);
215:
216: // verify that the service units were extracted into directories
217: assertTrue(su1_file.exists());
218: assertTrue(su2_file.exists());
219: }
220:
221: public void testAddServiceAssemblyArchiveWrongType() {
222: try {
223: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
224: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
225: } catch (RepositoryException repEx) {
226: // expected
227: return;
228: }
229:
230: fail("Added service assembly to repository as a component.");
231: }
232:
233: public void testAddServiceAssemblyArchiveDuplicate()
234: throws Exception {
235: // add it once
236: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
237: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
238:
239: try {
240: // try a second time
241: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
242: mJbiRoot + SERVICE_ASSEMBLY_ARCHIVE_PATH);
243: } catch (RepositoryException repEx) {
244: // expected
245: return;
246: }
247:
248: fail("Added duplicate service assembly to repository.");
249: }
250:
251: public void testRemoveServiceAssemblyArchive() throws Exception {
252: // add the archive
253: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
254: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
255:
256: // remove the archive
257: mRepository.removeArchive(ArchiveType.SERVICE_ASSEMBLY,
258: SERVICE_ASSEMBLY_NAME);
259:
260: // attempt to find the archive
261: Archive archive = mRepository.getArchive(
262: ArchiveType.SERVICE_ASSEMBLY, SERVICE_ASSEMBLY_NAME);
263: assertTrue(archive == null);
264: }
265:
266: /**
267: * Test removing a empty service assembly folder
268: */
269: public void testRemoveEmptyServiceAssemblyDirectory()
270: throws Exception {
271: // Create the empty sa dir
272: String dummySA = "emptySA";
273:
274: File emptySADir = new File(mCtx.getEnvironmentContext()
275: .getJbiInstanceRoot()
276: + "/service-assemblies/" + dummySA);
277:
278: emptySADir.mkdirs();
279:
280: assertTrue(emptySADir.exists());
281:
282: // remove the archive
283: mRepository
284: .removeArchive(ArchiveType.SERVICE_ASSEMBLY, dummySA);
285:
286: // attempt to find the archive
287: Archive archive = mRepository.getArchive(
288: ArchiveType.SERVICE_ASSEMBLY, SERVICE_ASSEMBLY_NAME);
289: assertTrue(archive == null);
290: System.out.println(emptySADir.getAbsolutePath());
291: assertFalse(emptySADir.exists());
292: }
293:
294: public void testRemoveServiceAssemblyArchiveBad() throws Exception {
295: // remove an archive that doesn't exist
296: mRepository.removeArchive(ArchiveType.SERVICE_ASSEMBLY, "foo");
297: }
298:
299: public void testFindServiceAssemblyArchive() throws Exception {
300: // add the archive
301: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
302: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
303:
304: // try and find it
305: String path = mRepository.findArchive(
306: ArchiveType.SERVICE_ASSEMBLY, SERVICE_ASSEMBLY_NAME);
307:
308: assertTrue(path.endsWith(SERVICE_ASSEMBLY_ARCHIVE));
309: assertTrue(path != null);
310: }
311:
312: public void testFindServiceUnitArchive() throws Exception {
313: String su1_path;
314: String su2_path;
315:
316: // add the archive
317: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
318: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
319:
320: // try and find service units
321: su1_path = mRepository.findArchive(ArchiveType.SERVICE_UNIT,
322: SERVICE_ASSEMBLY_NAME + "/" + SERVICE_UNIT_1_NAME);
323:
324: su2_path = mRepository.findArchive(ArchiveType.SERVICE_UNIT,
325: SERVICE_ASSEMBLY_NAME + "/" + SERVICE_UNIT_2_NAME);
326:
327: assertTrue(su1_path != null);
328: assertTrue(su2_path != null);
329: }
330:
331: public void testGetServiceUnitArchive() throws Exception {
332: Archive su;
333:
334: // add the archive
335: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
336: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
337:
338: // go and get it
339: su = mRepository.getArchive(ArchiveType.SERVICE_UNIT,
340: SERVICE_ASSEMBLY_NAME + "/" + SERVICE_UNIT_1_NAME);
341:
342: assertTrue(su.getType().equals(ArchiveType.SERVICE_UNIT));
343: assertTrue(su.getSize().intValue() > 0);
344: assertTrue(su.getJbiName().equals(SERVICE_UNIT_1_NAME));
345: }
346:
347: public void testServiceUnitMissingJbiXml() throws Exception {
348: Archive su;
349:
350: // try and add an SA containing an SU that's missing jbi.xml
351: try {
352: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
353: mJbiRoot + SA_WITH_BAD_SU_ARCHIVE_PATH);
354:
355: fail("Archive constructed from invalid source -- missing jbi.xml");
356: } catch (RepositoryException rEx) {
357: }
358: }
359:
360: /** Tests the ability to add an archive to the repository when a
361: * previous entry in the repository already exists but is marked for
362: * deletion.
363: */
364: public void testAddComponentArchiveMarked() throws Exception {
365: String path = mJbiRoot + COMPONENT_ARCHIVE_PATH;
366: Archive archive1;
367: Archive archive2;
368:
369: // add the archive and mark it to simulate a failed removal
370: archive1 = mRepository.addArchive(ArchiveType.COMPONENT, path);
371: assertTrue(mRepository.archiveExists(ArchiveType.COMPONENT,
372: archive1.getJbiName()));
373: DirectoryUtil.markDir(new File(archive1.getPath()).getParent());
374:
375: // check to make sure it counts as deleted
376: assertFalse(mRepository.archiveExists(ArchiveType.COMPONENT,
377: archive1.getJbiName()));
378:
379: // try and add the same thing back in (this should succeed)
380: archive2 = mRepository.addArchive(ArchiveType.COMPONENT, path);
381:
382: // verify that archive1 and archive 2 do not have the same path
383: assertFalse(archive1.getPath().equals(archive2.getPath()));
384:
385: // remove the latest one
386: mRepository.removeArchive(ArchiveType.COMPONENT, archive2
387: .getJbiName());
388: assertFalse(mRepository.archiveExists(ArchiveType.COMPONENT,
389: archive2.getJbiName()));
390: }
391:
392: /** Tests whether the service unit archive path is constructed correctly
393: * when a service assembly is added when a previous service assembly
394: * with the same name is marked for deltetion.
395: */
396: public void testGettingServiceUnitArchiveWhenMarked()
397: throws Exception {
398: String path = mJbiRoot + SERVICE_ASSEMBLY_ARCHIVE_PATH;
399: Archive archive1;
400: Archive archive2;
401: Archive suArchive;
402:
403: // add the archive and mark it to simulate a failed removal
404: archive1 = mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
405: path);
406: assertTrue(mRepository.archiveExists(
407: ArchiveType.SERVICE_ASSEMBLY, archive1.getJbiName()));
408: DirectoryUtil.markDir(new File(archive1.getPath()).getParent());
409:
410: // Add the same service assembly again
411: archive2 = mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
412: path);
413:
414: // Get the path to the service unit
415: suArchive = mRepository.getArchive(ArchiveType.SERVICE_UNIT,
416: SERVICE_ASSEMBLY_NAME + File.separator
417: + SERVICE_UNIT_1_NAME);
418: File suDir = new File(suArchive.getPath());
419: assertTrue(suDir.exists());
420:
421: // remove the latest one
422: mRepository.removeArchive(ArchiveType.SERVICE_ASSEMBLY,
423: archive2.getJbiName());
424: assertFalse(mRepository.archiveExists(
425: ArchiveType.SERVICE_ASSEMBLY, archive2.getJbiName()));
426:
427: // remove the marked service assembly archive
428: DirectoryUtil.removeMarkedDirs(new File(archive1.getPath())
429: .getParent());
430: }
431:
432: /** Tests the ability to remove an archive when an open file reference is
433: * hanging around. Since this only is a problem on Windows, we need to
434: * make sure that this test passes even if the initial removal succeeds.
435: */
436: public void testRemoveServiceAssemblyArchiveMarked()
437: throws Exception {
438: String path = mJbiRoot + SERVICE_ASSEMBLY_ARCHIVE_PATH;
439: Archive archive;
440:
441: // add the archive
442: archive = mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
443: path);
444: assertTrue(mRepository.archiveExists(
445: ArchiveType.SERVICE_ASSEMBLY, archive.getJbiName()));
446:
447: // create a JarFile ref for one of the SUs
448: Archive suArchive = mRepository.getArchive(
449: ArchiveType.SERVICE_UNIT, archive.getJbiName() + "/"
450: + SERVICE_UNIT_1_NAME);
451: JarFile jarFile = new JarFile(new File(suArchive.getPath()));
452:
453: // Remove it and make sure it's gone. Deletion of the folder should fail
454: // on windows, but the directory should be marked and the remove operation
455: // should succeed.
456: mRepository.removeArchive(ArchiveType.SERVICE_ASSEMBLY, archive
457: .getJbiName());
458: assertFalse(mRepository.archiveExists(
459: ArchiveType.SERVICE_ASSEMBLY, archive.getJbiName()));
460:
461: // try and add the same thing back in (this should succeed)
462: archive = mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY,
463: path);
464:
465: // remove the latest one
466: mRepository.removeArchive(ArchiveType.SERVICE_ASSEMBLY, archive
467: .getJbiName());
468: assertFalse(mRepository.archiveExists(ArchiveType.COMPONENT,
469: archive.getJbiName()));
470:
471: // clean up
472: try {
473: jarFile.close();
474: } catch (Exception ex) { /* irrelevant */
475: }
476:
477: // simulate a repository startup to see if the DELETED folder is cleaned up.
478: new Repository(mCtx);
479: }
480:
481: /**
482: * Test the regular expression used to match the component/shared library/service unit
483: * folders.
484: */
485: public void testDirSuffixPattern() throws Exception {
486: String patternSuffix = Repository.DIR_SUFFIX_PATTERN;
487:
488: assertFalse(Pattern.matches("SunFileBinding" + patternSuffix,
489: "SunFileBinding"));
490: assertFalse(Pattern.matches("SunFileBinding" + patternSuffix,
491: "SunFileBinding."));
492: assertFalse(Pattern.matches("SunFileBinding" + patternSuffix,
493: "SunFileBinding_.99"));
494:
495: assertTrue(Pattern.matches("SunFileBinding" + patternSuffix,
496: "SunFileBinding.1"));
497: assertTrue(Pattern.matches("SunFileBinding" + patternSuffix,
498: "SunFileBinding.0"));
499: assertTrue(Pattern.matches("SunFileBinding" + patternSuffix,
500: "SunFileBinding.190"));
501: assertTrue(Pattern.matches("SunFileBinding" + patternSuffix,
502: "SunFileBinding.9999999"));
503: }
504:
505: /**
506: * Test getting a Service Assembly archive with a name which partially matches that
507: * of the added service assembly. The getArchive() should fail.
508: */
509: public void testGetServiceAssemblyArchive() throws Exception {
510: // add the archive
511: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
512: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
513:
514: // attempt to find the archive - should not find a match
515: Archive archive = mRepository.getArchive(
516: ArchiveType.SERVICE_ASSEMBLY,
517: BOGUS_SERVICE_ASSEMBLY_NAME);
518: assertTrue(archive == null);
519:
520: // remove the archive
521: mRepository.removeArchive(ArchiveType.SERVICE_ASSEMBLY,
522: SERVICE_ASSEMBLY_NAME);
523: }
524:
525: /**
526: * Test getting the names of service assemblies in the repository
527: */
528: public void testGetServiceAssemblyNames() throws Exception {
529: // add the archive
530: mRepository.addArchive(ArchiveType.SERVICE_ASSEMBLY, mJbiRoot
531: + SERVICE_ASSEMBLY_ARCHIVE_PATH);
532:
533: // test getting the service assembly names
534: java.util.List<String> saNames = mRepository
535: .getArchiveEntityNames(ArchiveType.SERVICE_ASSEMBLY);
536: assertEquals(1, saNames.size());
537: assertEquals(SERVICE_ASSEMBLY_NAME, saNames.get(0));
538: }
539:
540: /**
541: * Test getting the names of components in the repository
542: */
543: public void testGetComponentNames() throws Exception {
544: // add the archives
545: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
546: + COMPONENT_ARCHIVE_PATH);
547:
548: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
549: + BINDING_COMPONENT_ARCHIVE_PATH);
550:
551: java.util.List<String> components = mRepository
552: .getArchiveEntityNames(ArchiveType.COMPONENT);
553: assertEquals(2, components.size());
554: assertTrue(components.contains(COMPONENT_ARCHIVE_NAME));
555: assertTrue(components.contains(BINDING_COMPONENT_ARCHIVE_NAME));
556: }
557:
558: /**
559: * Test getting the names of components in the repository, with a component
560: * marked for deletion, this component should be omitted from the list.
561: */
562: public void testGetComponentNamesWithMarkedDir() throws Exception {
563: // add the archives
564: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
565: + COMPONENT_ARCHIVE_PATH);
566:
567: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
568: + BINDING_COMPONENT_ARCHIVE_PATH);
569:
570: String archiveDir = mRepository.findArchiveDirectory(
571: ArchiveType.COMPONENT, BINDING_COMPONENT_ARCHIVE_NAME);
572:
573: com.sun.jbi.management.internal.support.DirectoryUtil
574: .markDir(archiveDir);
575:
576: java.util.List<String> components = mRepository
577: .getArchiveEntityNames(ArchiveType.COMPONENT);
578: assertEquals(1, components.size());
579: assertTrue(components.contains(COMPONENT_ARCHIVE_NAME));
580: assertFalse(components.contains(BINDING_COMPONENT_ARCHIVE_NAME));
581: }
582:
583: /**
584: * Test getting the names of components in the repository, with a component
585: * folder having a numeric suffix appended, this component should be added to the list.
586: */
587: public void testGetComponentNamesWithSuffixedDirs()
588: throws Exception {
589: // add the archive
590: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
591: + BINDING_COMPONENT_ARCHIVE_PATH);
592:
593: // mark it for deletion
594: String archiveDir = mRepository.findArchiveDirectory(
595: ArchiveType.COMPONENT, BINDING_COMPONENT_ARCHIVE_NAME);
596:
597: com.sun.jbi.management.internal.support.DirectoryUtil
598: .markDir(archiveDir);
599:
600: // add the archive again
601: mRepository.addArchive(ArchiveType.COMPONENT, mJbiRoot
602: + BINDING_COMPONENT_ARCHIVE_PATH);
603:
604: java.util.List<String> components = mRepository
605: .getArchiveEntityNames(ArchiveType.COMPONENT);
606: assertEquals(1, components.size());
607: assertTrue(components.contains(BINDING_COMPONENT_ARCHIVE_NAME));
608: }
609: }
|