KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > dd > WeakMetadataUnitTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.dd;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30
31 /**
32  *
33  * @author Andrei Badea
34  */

35 public class WeakMetadataUnitTest extends NbTestCase {
36
37     private FileObject dataDir;
38     private FileObject workDir;
39
40     public WeakMetadataUnitTest(String JavaDoc testName) {
41         super(testName);
42     }
43
44     public void setUp() throws Exception JavaDoc {
45         clearWorkDir();
46         dataDir = FileUtil.toFileObject(getDataDir());
47         workDir = FileUtil.toFileObject(getWorkDir());
48     }
49
50     private static FileObject copyFile(FileObject source, FileObject destFolder) throws IOException JavaDoc {
51         return FileUtil.copyFile(source, destFolder, source.getName());
52     }
53
54     public void testWeakMetadataUnit() throws Exception JavaDoc {
55         FileObject cpRoot = workDir.createFolder("root");
56         assertNotNull(cpRoot);
57         FileObject dd = copyFile(dataDir.getFileObject("orm.xml"), workDir);
58         assertNotNull(dd);
59
60         ClassPath classPath = ClassPathSupport.createClassPath(new FileObject[] { cpRoot });
61         WeakMetadataUnit metadataUnit = new WeakMetadataUnit(dd, classPath);
62
63         assertSame(classPath, metadataUnit.getClassPath());
64         assertSame(dd, metadataUnit.getDeploymentDescriptor());
65
66         Reference JavaDoc<ClassPath> classPathRef = new WeakReference JavaDoc<ClassPath>(classPath);
67         classPath = null;
68         assertGC("Should be possible to GC classPath", classPathRef);
69
70         // should return an empty classpath after the original one was GCd
71

72         ClassPath newClassPath = metadataUnit.getClassPath();
73         assertNotNull(newClassPath);
74         assertEquals(0, newClassPath.entries().size());
75     }
76
77     public void testConstructorParametersCanBeNull() {
78         WeakMetadataUnit metadataUnit = new WeakMetadataUnit(null, null);
79
80         assertNull(metadataUnit.getDeploymentDescriptor());
81
82         ClassPath classPath = metadataUnit.getClassPath();
83         assertNotNull(classPath);
84         assertEquals(0, classPath.entries().size());
85     }
86 }
87
Popular Tags