KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientPersistenceProviderTest


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.j2ee.clientproject;
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.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
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 /**
42  *
43  * @author Andrei Badea
44  */

45 public class AppClientPersistenceProviderTest extends NbTestCase {
46
47     // TODO also test the contents of the classpaths
48

49     private Project project;
50     private AppClientPersistenceProvider provider;
51     private FileObject persistenceLocation;
52
53     public AppClientPersistenceProviderTest(String JavaDoc testName) {
54         super(testName);
55     }
56
57     protected Level JavaDoc logLevel() {
58         // enabling logging
59
return Level.INFO;
60         // we are only interested in a single logger, so we set its level in setUp(),
61
// as returning Level.FINEST here would log from all loggers
62
}
63
64     public PrintStream JavaDoc getLog() {
65         return System.err;
66     }
67
68     public void setUp() throws Exception JavaDoc {
69         // in an attempt to find the cause of issue 90762
70
Logger.getLogger(PersistenceScopesHelper.class.getName()).setLevel(Level.FINEST);
71         // setup the project
72
File JavaDoc f = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/ApplicationClient1");
73         project = ProjectManager.getDefault().findProject(FileUtil.toFileObject(f));
74         provider = (AppClientPersistenceProvider)project.getLookup().lookup(AppClientPersistenceProvider.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 JavaDoc {
84         assertEquals(persistenceLocation, provider.getLocation());
85         assertEquals(persistenceLocation, provider.createLocation());
86     }
87
88     public void testPersistenceScopes() throws Exception JavaDoc {
89         class PCL implements PropertyChangeListener JavaDoc {
90             private int changeCount;
91
92             public void propertyChange(PropertyChangeEvent JavaDoc 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         // no persistence scope
106
PersistenceScope persistenceScope = provider.findPersistenceScope(root);
107         assertNull(persistenceScope);
108         assertEquals(0, listener.changeCount);
109         assertEquals(0, persistenceScopes.getPersistenceScopes().length);
110
111         // adding persistence scope
112
FileObject persistenceXml = persistenceLocation.createData("persistence.xml"); // NOI18N
113
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         // testing the persistence scope classpath
120
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         // removing persistence.xml
126
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         // re-adding persistence scope
134
persistenceLocation.createData("persistence.xml"); // NOI18N
135
persistenceScope = provider.findPersistenceScope(root);
136         assertTrue("Should always return a valid persistence.xml", persistenceScope.getPersistenceXml().isValid());
137     }
138
139     public void testPersistenceClassPath() throws Exception JavaDoc {
140         ClassPath persistenceCP = provider.getClassPath();
141         assertNotNull(persistenceCP);
142         assertSame("Should return the same classpath object", persistenceCP, provider.getClassPath());
143     }
144 }
145
Popular Tags