1 19 20 package org.netbeans.modules.java.j2seproject; 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.Arrays ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import org.netbeans.api.java.classpath.ClassPath; 30 import org.netbeans.api.java.project.JavaProjectConstants; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.api.project.SourceGroup; 35 import org.netbeans.api.project.Sources; 36 import org.netbeans.api.project.TestUtil; 37 import org.netbeans.junit.NbTestCase; 38 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 39 import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes; 40 import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.modules.SpecificationVersion; 44 45 49 public class J2SEPersistenceProviderTest extends NbTestCase { 50 51 53 private Project project; 54 private J2SEPersistenceProvider provider; 55 private File persistenceLocation; 56 57 public J2SEPersistenceProviderTest(String testName) { 58 super(testName); 59 } 60 61 protected Level logLevel() { 62 return Level.INFO; 64 } 67 68 public PrintStream getLog() { 69 return System.err; 70 } 71 72 public void setUp() throws Exception { 73 Logger.getLogger(PersistenceScopesHelper.class.getName()).setLevel(Level.FINEST); 75 FileObject scratch = TestUtil.makeScratchDir(this); 77 FileObject projdir = scratch.createFolder("proj"); 78 J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.5")); 79 J2SEProjectGenerator.createProject(FileUtil.toFile(projdir), "proj", "foo.Main", "manifest.mf"); 80 J2SEProjectGenerator.setDefaultSourceLevel(null); 81 project = ProjectManager.getDefault().findProject(projdir); 82 provider = (J2SEPersistenceProvider)project.getLookup().lookup(J2SEPersistenceProvider.class); 83 persistenceLocation = new File (FileUtil.toFile(project.getProjectDirectory().getFileObject("src")), "META-INF"); 84 System.out.println(Arrays.asList(ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA))); 85 System.out.println(persistenceLocation); 86 } 87 88 public void testPersistenceLocation() throws Exception { 89 assertEquals(null, provider.getLocation()); 90 assertEquals(persistenceLocation, FileUtil.toFile(provider.createLocation())); 91 assertEquals(persistenceLocation, FileUtil.toFile(provider.getLocation())); 92 } 93 94 public void testPersistenceScopes() throws Exception { 95 class PCL implements PropertyChangeListener { 96 private int changeCount; 97 98 public void propertyChange(PropertyChangeEvent event) { 99 changeCount++; 100 } 101 } 102 103 Sources src = (Sources)project.getLookup().lookup(Sources.class); 104 SourceGroup[] groups = src.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 105 FileObject root = groups[0].getRootFolder(); 106 107 PersistenceScopes persistenceScopes = PersistenceScopes.getPersistenceScopes(project); 108 PCL listener = new PCL(); 109 persistenceScopes.addPropertyChangeListener(listener); 110 111 PersistenceScope persistenceScope = provider.findPersistenceScope(root); 113 assertNull(persistenceScope); 114 assertEquals(0, listener.changeCount); 115 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 116 117 FileObject persistenceLocationFO = provider.createLocation(); 119 FileObject persistenceXml = persistenceLocationFO.createData("persistence.xml"); persistenceScope = provider.findPersistenceScope(root); 121 assertNotNull(persistenceScope); 122 assertEquals(persistenceXml, persistenceScope.getPersistenceXml()); 123 assertEquals(1, listener.changeCount); 124 assertSame(persistenceScope, persistenceScopes.getPersistenceScopes()[0]); 125 126 ClassPath scopeCP = persistenceScope.getClassPath(); 128 assertNotNull(scopeCP); 129 persistenceScope = provider.findPersistenceScope(root); 130 assertSame("Should return the same classpath object", scopeCP, persistenceScope.getClassPath()); 131 132 persistenceXml.delete(); 134 assertNull("Should return a null persistence.xml", persistenceScope.getPersistenceXml()); 135 persistenceScope = provider.findPersistenceScope(root); 136 assertNull(persistenceScope); 137 assertEquals(2, listener.changeCount); 138 assertEquals(0, persistenceScopes.getPersistenceScopes().length); 139 140 persistenceLocationFO.createData("persistence.xml"); persistenceScope = provider.findPersistenceScope(root); 143 assertTrue("Should always return a valid persistence.xml", persistenceScope.getPersistenceXml().isValid()); 144 } 145 146 public void testPersistenceClassPath() throws Exception { 147 ClassPath persistenceCP = provider.getClassPath(); 148 assertNotNull(persistenceCP); 149 assertSame("Should return the same classpath object", persistenceCP, provider.getClassPath()); 150 } 151 } 152 | Popular Tags |