1 19 20 package org.netbeans.modules.web.project; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.PrintStream ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 import org.netbeans.api.java.classpath.ClassPath; 29 import org.netbeans.api.java.project.JavaProjectConstants; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.api.project.SourceGroup; 33 import org.netbeans.api.project.Sources; 34 import org.netbeans.junit.NbTestCase; 35 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 36 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 37 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 41 45 public class WebPersistenceProviderTest extends NbTestCase { 46 47 49 private Project project; 50 private WebPersistenceProvider provider; 51 private FileObject persistenceLocation; 52 53 public WebPersistenceProviderTest(String testName) { 54 super(testName); 55 } 56 57 protected Level logLevel() { 58 return Level.INFO; 60 } 63 64 public PrintStream getLog() { 65 return System.err; 66 } 67 68 public void setUp() throws Exception { 69 Logger.getLogger(PersistenceScopesHelper.class.getName()).setLevel(Level.FINEST); 71 File f = new File (getDataDir().getAbsolutePath(), "projects/WebApplication1"); 73 project = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f)); 74 provider = (WebPersistenceProvider)project.getLookup().lookup(WebPersistenceProvider.class); 75 persistenceLocation = project.getProjectDirectory().getFileObject("src/conf"); 76 77 FileObject persistenceXml = persistenceLocation.getFileObject("persistence.xml"); 78 if (persistenceXml != null) { 79 persistenceXml.delete(); 80 } 81 } 82 83 public void testPersistenceLocation() throws Exception { 84 assertEquals(persistenceLocation, provider.getLocation()); 85 assertEquals(persistenceLocation, provider.createLocation()); 86 } 87 88 public void testPersistenceScopes() throws Exception { 89 class PCL implements PropertyChangeListener { 90 private int changeCount; 91 92 public void propertyChange(PropertyChangeEvent event) { 93 changeCount++; 94 } 95 } 96 97 Sources src = (Sources)project.getLookup().lookup(Sources.class); 98 SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 99 FileObject root = groups[0].getRootFolder(); 100 101 PersistenceScopes persistenceScopes = PersistenceScopes.getPersistenceScopes(project); 102 PCL listener = new PCL(); 103 persistenceScopes.addPropertyChangeListener(listener); 104 105 PersistenceScope persistenceScope = provider.findPersistenceScope(root); 107 assertNull(persistenceScope); 108 assertEquals(0, listener.changeCount); 109 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 110 111 FileObject persistenceXml = persistenceLocation.createData("persistence.xml"); persistenceScope = provider.findPersistenceScope(root); 114 assertNotNull(persistenceScope); 115 assertEquals(persistenceXml, persistenceScope.getPersistenceXml()); 116 assertEquals(1, listener.changeCount); 117 assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]); 118 119 ClassPath scopeCP = persistenceScope.getClassPath(); 121 assertNotNull(scopeCP); 122 persistenceScope = provider.findPersistenceScope(root); 123 assertSame("Should return the same classpath object", scopeCP, persistenceScope.getClassPath()); 124 125 persistenceXml.delete(); 127 assertNull("Should return a null persistence.xml", persistenceScope.getPersistenceXml()); 128 persistenceScope = provider.findPersistenceScope(root); 129 assertNull(persistenceScope); 130 assertEquals(2, listener.changeCount); 131 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 132 133 persistenceLocation.createData("persistence.xml"); persistenceScope = provider.findPersistenceScope(root); 136 assertTrue("Should always return a valid persistence.xml", persistenceScope.getPersistenceXml().isValid()); 137 } 138 139 public void testPersistenceClassPath() throws Exception { 140 ClassPath persistenceCP = provider.getClassPath(); 141 assertNotNull(persistenceCP); 142 assertSame("Should return the same classpath object", persistenceCP, provider.getClassPath()); 143 } 144 } 145 | Popular Tags |