1 package org.netbeans.modules.j2ee.persistence.spi.support; 2 3 import java.beans.PropertyChangeEvent ; 4 import java.beans.PropertyChangeListener ; 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.util.Arrays ; 8 import org.netbeans.api.java.classpath.ClassPath; 9 import org.netbeans.junit.NbTestCase; 10 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 11 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 12 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeFactory; 13 import org.netbeans.modules.j2ee.persistence.spi.PersistenceScopeImplementation; 14 import org.openide.filesystems.FileObject; 15 import org.openide.filesystems.FileUtil; 16 17 21 public class PersistenceScopesHelperTest extends NbTestCase { 22 23 private FileObject dataDir; 24 private FileObject workDir; 25 26 private PersistenceScope persistenceScope; 27 28 public PersistenceScopesHelperTest(String testName) { 29 super(testName); 30 } 31 32 public void setUp() throws Exception { 33 clearWorkDir(); 34 dataDir = FileUtil.toFileObject(getDataDir()); 35 workDir = FileUtil.toFileObject(getWorkDir()); 36 37 persistenceScope = PersistenceScopeFactory.createPersistenceScope(new PersistenceScopeImpl()); 38 } 39 40 private static FileObject copyFile(FileObject source, FileObject destFolder) throws IOException { 41 return FileUtil.copyFile(source, destFolder, source.getName()); 42 } 43 44 public void testBasic() throws Exception { 45 PersistenceScope persistenceScope2 = PersistenceScopeFactory.createPersistenceScope(new PersistenceScopeImpl()); 46 47 FileObject parent = workDir.createFolder("parent"); 48 File persistenceXmlFile = new File (FileUtil.toFile(parent), "persistence.xml"); 49 50 PersistenceScopesHelper helper = new PersistenceScopesHelper(); 51 PersistenceScopes persistenceScopes = helper.getPersistenceScopes(); 52 PCL listener = new PCL(); 53 persistenceScopes.addPropertyChangeListener(listener); 54 55 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 56 assertEquals(0, listener.changeCount); 57 58 helper.changePersistenceScope(persistenceScope, persistenceXmlFile); 59 60 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 61 assertEquals(0, listener.changeCount); 62 63 65 try { 66 helper.changePersistenceScope(null, persistenceXmlFile); 67 fail(); 68 } catch (IllegalArgumentException e) { } 69 70 72 helper.changePersistenceScope(null, null); 73 74 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 75 assertEquals(0, listener.changeCount); 76 77 79 helper.changePersistenceScope(persistenceScope2, persistenceXmlFile); 80 81 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 82 assertEquals(0, listener.changeCount); 83 84 86 helper.changePersistenceScope(persistenceScope2, null); 87 88 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 89 assertEquals(0, listener.changeCount); 90 91 93 helper.changePersistenceScope(persistenceScope2, persistenceXmlFile); 94 95 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 96 assertEquals(0, listener.changeCount); 97 98 100 copyFile(dataDir.getFileObject("persistence.xml"), parent); 101 102 assertEquals(1, listener.changeCount); 103 assertSame(persistenceScope2, persistenceScopes.getPersistenceScopes()[0]); 104 105 107 helper.changePersistenceScope(persistenceScope, persistenceXmlFile); 108 109 assertEquals(2, listener.changeCount); 110 assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]); 111 112 114 helper.changePersistenceScope(persistenceScope, persistenceXmlFile); 115 116 assertEquals(2, listener.changeCount); 117 assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]); 118 119 121 helper.changePersistenceScope(persistenceScope, null); 122 123 assertEquals(3, listener.changeCount); 124 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 125 126 128 helper.changePersistenceScope(persistenceScope, persistenceXmlFile); 129 130 assertEquals(4, listener.changeCount); 131 assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]); 132 133 135 parent.getFileObject("persistence.xml").delete(); 136 137 assertEquals(5, listener.changeCount); 138 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 139 140 142 assertSame(persistenceScopes, helper.getPersistenceScopes()); 143 } 144 145 public void testPropertyChangeWhenFirstTimeSettingExistingPersistenceXml() throws Exception { 146 FileObject persistenceXml = copyFile(dataDir.getFileObject("persistence.xml"), workDir); 147 148 PersistenceScopesHelper helper = new PersistenceScopesHelper(); 149 PersistenceScopes persistenceScopes = helper.getPersistenceScopes(); 150 PCL listener = new PCL(); 151 persistenceScopes.addPropertyChangeListener(listener); 152 153 helper.changePersistenceScope(persistenceScope, FileUtil.toFile(persistenceXml)); 154 155 assertEquals(1, listener.changeCount); 156 assertSame(persistenceScope, helper.getPersistenceScopes().getPersistenceScopes()[0]); 157 } 158 159 private static final class PersistenceScopeImpl implements PersistenceScopeImplementation { 160 161 public FileObject getPersistenceXml() { 162 return null; 163 } 164 165 public ClassPath getClassPath() { 166 return null; 167 } 168 } 169 170 private static final class PCL implements PropertyChangeListener { 171 172 private int changeCount; 173 174 public void propertyChange(PropertyChangeEvent event) { 175 assertEquals(PersistenceScopes.PROP_PERSISTENCE_SCOPES, event.getPropertyName()); 176 changeCount++; 177 } 178 } 179 } 180 | Popular Tags |