1 19 20 package org.netbeans.modules.j2ee.persistence.dd; 21 22 import java.io.IOException ; 23 import java.lang.ref.Reference ; 24 import java.lang.ref.WeakReference ; 25 import java.util.Collections ; 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 42 public class PersistenceUtilsTest extends PUDataObjectTestBase { 43 44 private FileObject dataDir; 45 private FileObject workDir; 46 47 public PersistenceUtilsTest(String testName) { 48 super(testName); 49 } 50 51 public void setUp() throws Exception { 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 { 58 return FileUtil.copyFile(source, destFolder, source.getName()); 59 } 60 61 public void testNoLeaks() throws Exception { 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 <ClassPath> classPathRef = new WeakReference <ClassPath>(classPath); 73 classPath = null; 74 persistenceScopeImpl.setClassPath(null); 75 assertGC("Should be possible to GC classPath", classPathRef); 76 77 Reference <FileObject> persistenceXmlRef = new WeakReference <FileObject>(persistenceXml); 78 persistenceXml = null; 79 persistenceScopeImpl.setPersistenceXml(null); 80 assertGC("Should be possible to GC persistenceXml", persistenceXmlRef); 81 82 Reference <PersistenceScope> persistenceScopeRef = new WeakReference <PersistenceScope>(persistenceScope); 83 persistenceScope = null; 84 assertGC("Should be possible to GC persistenceScope", persistenceScopeRef); 85 } 86 87 public void testCaching() throws Exception { 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 97 PersistenceUtils.EntityMappingsCache emCache1 = PersistenceUtils.getEntityMappingsCache(persistenceScope); 98 PersistenceUtils.EntityMappingsCache emCache2 = PersistenceUtils.getEntityMappingsCache(persistenceScope); 99 100 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); 118 120 PersistenceUnit persistenceUnit = new PersistenceUnit(); 121 persistenceUnit.setName("em3"); 122 123 emCache1.getPUDataObject().addPersistenceUnit(persistenceUnit); 124 emCache1.getPUDataObject().save(); 125 126 129 PersistenceUtils.EntityMappingsCache emCache3 = PersistenceUtils.getEntityMappingsCache(persistenceScope); 130 assertSame(emCache1, emCache3); 131 132 134 MetadataUnit emmu3 = emCache1.getMetadataUnit("em"); 135 MetadataUnit emmu4 = emCache1.getMetadataUnit("em"); 136 assertSame(emmu3, emmu4); 137 assertSame(emmu1, emmu3); 138 139 141 MetadataUnit em3mu1 = emCache1.getMetadataUnit("em3"); 142 MetadataUnit em3mu2 = emCache1.getMetadataUnit("em3"); 143 assertNotNull(em3mu1); 144 assertSame(em3mu1, em3mu2); 145 146 148 emCache1.getPUDataObject().delete(); 149 150 PersistenceUtils.EntityMappingsCache emCache4 = PersistenceUtils.getEntityMappingsCache(persistenceScope); 151 assertNull(emCache4); 152 } 153 154 public void testMetadataUnitContents() throws Exception { 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 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 |