KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEPersistenceProviderTest


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.java.j2seproject;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
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 /**
46  *
47  * @author Andrei Badea
48  */

49 public class J2SEPersistenceProviderTest extends NbTestCase {
50
51     // TODO also test the contents of the classpaths
52

53     private Project project;
54     private J2SEPersistenceProvider provider;
55     private File JavaDoc persistenceLocation;
56
57     public J2SEPersistenceProviderTest(String JavaDoc testName) {
58         super(testName);
59     }
60
61     protected Level JavaDoc logLevel() {
62         // enabling logging
63
return Level.INFO;
64         // we are only interested in a single logger, so we set its level in setUp(),
65
// as returning Level.FINEST here would log from all loggers
66
}
67
68     public PrintStream JavaDoc getLog() {
69         return System.err;
70     }
71
72     public void setUp() throws Exception JavaDoc {
73         // in an attempt to find the cause of issue 90762
74
Logger.getLogger(PersistenceScopesHelper.class.getName()).setLevel(Level.FINEST);
75         // setup the project
76
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 JavaDoc(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 JavaDoc {
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 JavaDoc {
95         class PCL implements PropertyChangeListener JavaDoc {
96             private int changeCount;
97
98             public void propertyChange(PropertyChangeEvent JavaDoc 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         // no persistence scope
112
PersistenceScope persistenceScope = provider.findPersistenceScope(root);
113         assertNull(persistenceScope);
114         assertEquals(0, listener.changeCount);
115         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
116
117         // adding persistence scope
118
FileObject persistenceLocationFO = provider.createLocation();
119         FileObject persistenceXml = persistenceLocationFO.createData("persistence.xml"); // NOI18N
120
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         // testing the persistence scope classpath
127
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         // removing persistence.xml
133
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         // re-adding persistence scope
141
persistenceLocationFO.createData("persistence.xml"); // NOI18N
142
persistenceScope = provider.findPersistenceScope(root);
143         assertTrue("Should always return a valid persistence.xml", persistenceScope.getPersistenceXml().isValid());
144     }
145
146     public void testPersistenceClassPath() throws Exception JavaDoc {
147         ClassPath persistenceCP = provider.getClassPath();
148         assertNotNull(persistenceCP);
149         assertSame("Should return the same classpath object", persistenceCP, provider.getClassPath());
150     }
151 }
152
Popular Tags