KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collections JavaDoc;
26 import org.netbeans.api.java.classpath.ClassPath;
27 import org.netbeans.modules.j2ee.metadata.MetadataUnit;
28 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
29 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
30 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory;
31 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation;
32 import org.netbeans.modules.j2ee.persistence.unit.PUDataObjectTestBase;
33 import org.netbeans.spi.java.classpath.PathResourceImplementation;
34 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.FileUtil;
37
38 /**
39  *
40  * @author Andrei Badea
41  */

42 public class PersistenceUtilsTest extends PUDataObjectTestBase {
43
44     private FileObject dataDir;
45     private FileObject workDir;
46
47     public PersistenceUtilsTest(String JavaDoc testName) {
48         super(testName);
49     }
50
51     public void setUp() throws Exception JavaDoc {
52         clearWorkDir();
53         dataDir = FileUtil.toFileObject(getDataDir());
54         workDir = FileUtil.toFileObject(getWorkDir());
55     }
56
57     private static FileObject copyFile(FileObject source, FileObject destFolder) throws IOException JavaDoc {
58         return FileUtil.copyFile(source, destFolder, source.getName());
59     }
60
61     public void testNoLeaks() throws Exception JavaDoc {
62         FileObject persistenceXml = copyFile(dataDir.getFileObject("persistence.xml"), workDir);
63         assertNotNull(persistenceXml);
64
65         ClassPath classPath = ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
66         PersistenceScopeImpl persistenceScopeImpl = new PersistenceScopeImpl(persistenceXml, classPath);
67         PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl);
68
69         PersistenceUtils.EntityMappingsCache emCache = PersistenceUtils.getEntityMappingsCache(persistenceScope);
70         emCache.getMetadataUnit("em");
71
72         Reference JavaDoc<ClassPath> classPathRef = new WeakReference JavaDoc<ClassPath>(classPath);
73         classPath = null;
74         persistenceScopeImpl.setClassPath(null);
75         assertGC("Should be possible to GC classPath", classPathRef);
76
77         Reference JavaDoc<FileObject> persistenceXmlRef = new WeakReference JavaDoc<FileObject>(persistenceXml);
78         persistenceXml = null;
79         persistenceScopeImpl.setPersistenceXml(null);
80         assertGC("Should be possible to GC persistenceXml", persistenceXmlRef);
81
82         Reference JavaDoc<PersistenceScope> persistenceScopeRef = new WeakReference JavaDoc<PersistenceScope>(persistenceScope);
83         persistenceScope = null;
84         assertGC("Should be possible to GC persistenceScope", persistenceScopeRef);
85     }
86
87     public void testCaching() throws Exception JavaDoc {
88         FileObject persistenceXml = copyFile(dataDir.getFileObject("persistence.xml"), workDir);
89         assertNotNull(persistenceXml);
90
91         ClassPath classPath = ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
92         PersistenceScopeImpl persistenceScopeImpl = new PersistenceScopeImpl(persistenceXml, classPath);
93         PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl);
94
95         // EntityMappingsCache instances are cached
96

97         PersistenceUtils.EntityMappingsCache emCache1 = PersistenceUtils.getEntityMappingsCache(persistenceScope);
98         PersistenceUtils.EntityMappingsCache emCache2 = PersistenceUtils.getEntityMappingsCache(persistenceScope);
99
100         // MetadataUnit's are cached
101

102         assertNotNull(emCache1);
103         assertSame(emCache1, emCache2);
104
105         MetadataUnit emmu1 = emCache1.getMetadataUnit("em");
106         MetadataUnit emmu2 = emCache1.getMetadataUnit("em");
107         assertNotNull(emmu1);
108         assertSame(emmu1, emmu2);
109
110         MetadataUnit em2mu1 = emCache2.getMetadataUnit("em2");
111         MetadataUnit em2mu2 = emCache2.getMetadataUnit("em2");
112
113         assertNotNull(em2mu1);
114         assertSame(emmu1, emmu2);
115
116         assertNotSame(emmu1, em2mu1); // that would be too much caching ;-)
117

118         // modifying persistence.xml should cause EntityMappingCache to be invalidated
119

120         PersistenceUnit persistenceUnit = new PersistenceUnit();
121         persistenceUnit.setName("em3");
122
123         emCache1.getPUDataObject().addPersistenceUnit(persistenceUnit);
124         emCache1.getPUDataObject().save();
125
126         // the EntityMappingsCache should have been invalidated by now
127
// it still should be the same instance...
128

129         PersistenceUtils.EntityMappingsCache emCache3 = PersistenceUtils.getEntityMappingsCache(persistenceScope);
130         assertSame(emCache1, emCache3);
131
132         // ... and the same MetadataUnit's will be retrieved from it...
133

134         MetadataUnit emmu3 = emCache1.getMetadataUnit("em");
135         MetadataUnit emmu4 = emCache1.getMetadataUnit("em");
136         assertSame(emmu3, emmu4);
137         assertSame(emmu1, emmu3);
138
139         // ... and it will contain a new MetadataUnit for em3
140

141         MetadataUnit em3mu1 = emCache1.getMetadataUnit("em3");
142         MetadataUnit em3mu2 = emCache1.getMetadataUnit("em3");
143         assertNotNull(em3mu1);
144         assertSame(em3mu1, em3mu2);
145
146         // removing persistence.xml should cause EntityMappingCache to be removed from the cache
147

148         emCache1.getPUDataObject().delete();
149
150         PersistenceUtils.EntityMappingsCache emCache4 = PersistenceUtils.getEntityMappingsCache(persistenceScope);
151         assertNull(emCache4);
152     }
153
154     public void testMetadataUnitContents() throws Exception JavaDoc {
155         FileObject persistenceXml = copyFile(dataDir.getFileObject("persistence.xml"), workDir);
156         assertNotNull(persistenceXml);
157         FileObject ormXml = copyFile(dataDir.getFileObject("orm.xml"), workDir);
158         assertNotNull(ormXml);
159
160         ClassPath classPath = ClassPathSupport.createClassPath(Collections.<PathResourceImplementation>emptyList());
161         PersistenceScopeImpl persistenceScopeImpl = new PersistenceScopeImpl(persistenceXml, classPath);
162         PersistenceScope persistenceScope = PersistenceScopeFactory.createPersistenceScope(persistenceScopeImpl);
163
164         PersistenceUtils.EntityMappingsCache emCache1 = PersistenceUtils.getEntityMappingsCache(persistenceScope);
165         MetadataUnit mu = emCache1.getMetadataUnit("em");
166
167         assertSame(classPath, mu.getClassPath());
168         assertSame(ormXml, mu.getDeploymentDescriptor());
169
170         // getDeploymentDescriptor() should be null when there is no orm.xml
171

172         ormXml.delete();
173         MetadataUnit mu2 = emCache1.getMetadataUnit("em2");
174
175         assertSame(classPath, mu2.getClassPath());
176         assertNull(mu2.getDeploymentDescriptor());
177     }
178
179     private static final class PersistenceScopeImpl implements PersistenceScopeImplementation {
180
181         private FileObject persistenceXml;
182         private ClassPath classPath;
183
184         public PersistenceScopeImpl(FileObject persistenceXml, ClassPath classPath) {
185             this.persistenceXml = persistenceXml;
186             this.classPath = classPath;
187         }
188
189         public FileObject getPersistenceXml() {
190             return persistenceXml;
191         }
192
193         public ClassPath getClassPath() {
194             return classPath;
195         }
196
197         public void setPersistenceXml(FileObject persistenceXml) {
198             this.persistenceXml = persistenceXml;
199         }
200
201         public void setClassPath(ClassPath classPath) {
202             this.classPath = classPath;
203         }
204     }
205 }
206
Popular Tags