001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.project;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.io.PrintStream;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import org.netbeans.api.java.classpath.ClassPath;
051: import org.netbeans.api.java.project.JavaProjectConstants;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.api.project.SourceGroup;
055: import org.netbeans.api.project.Sources;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.modules.j2ee.persistence.api.EntityClassScope;
058: import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
059: import org.netbeans.modules.j2ee.persistence.api.PersistenceScopes;
060: import org.netbeans.modules.j2ee.persistence.spi.support.PersistenceScopesHelper;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063:
064: /**
065: *
066: * @author Andrei Badea
067: */
068: public class WebPersistenceProviderTest extends NbTestCase {
069:
070: // TODO also test the contents of the classpaths
071:
072: private Project project;
073: private FileObject root;
074: private WebPersistenceProvider provider;
075: private FileObject persistenceLocation;
076:
077: public WebPersistenceProviderTest(String testName) {
078: super (testName);
079: }
080:
081: protected Level logLevel() {
082: // enabling logging
083: return Level.INFO;
084: // we are only interested in a single logger, so we set its level in setUp(),
085: // as returning Level.FINEST here would log from all loggers
086: }
087:
088: public PrintStream getLog() {
089: return System.err;
090: }
091:
092: public void setUp() throws Exception {
093: // in an attempt to find the cause of issue 90762
094: Logger.getLogger(PersistenceScopesHelper.class.getName())
095: .setLevel(Level.FINEST);
096: // setup the project
097: File f = new File(getDataDir().getAbsolutePath(),
098: "projects/WebApplication1");
099: project = ProjectManager.getDefault().findProject(
100: FileUtil.toFileObject(f));
101: Sources src = (Sources) project.getLookup().lookup(
102: Sources.class);
103: SourceGroup[] groups = src
104: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
105: root = groups[0].getRootFolder();
106: provider = (WebPersistenceProvider) project.getLookup().lookup(
107: WebPersistenceProvider.class);
108: persistenceLocation = project.getProjectDirectory()
109: .getFileObject("src/conf");
110:
111: FileObject persistenceXml = persistenceLocation
112: .getFileObject("persistence.xml");
113: if (persistenceXml != null) {
114: persistenceXml.delete();
115: }
116: }
117:
118: public void testPersistenceLocation() throws Exception {
119: assertEquals(persistenceLocation, provider.getLocation());
120: assertEquals(persistenceLocation, provider.createLocation());
121: }
122:
123: public void testPersistenceScopes() throws Exception {
124: class PCL implements PropertyChangeListener {
125: private int changeCount;
126:
127: public void propertyChange(PropertyChangeEvent event) {
128: changeCount++;
129: }
130: }
131:
132: PersistenceScopes persistenceScopes = PersistenceScopes
133: .getPersistenceScopes(project);
134: PCL listener = new PCL();
135: persistenceScopes.addPropertyChangeListener(listener);
136:
137: // no persistence scope
138: PersistenceScope persistenceScope = provider
139: .findPersistenceScope(root);
140: assertNull(persistenceScope);
141: assertEquals(0, listener.changeCount);
142: assertEquals(0, persistenceScopes.getPersistenceScopes().length);
143:
144: // adding persistence scope
145: FileObject persistenceXml = persistenceLocation
146: .createData("persistence.xml"); // NOI18N
147: persistenceScope = provider.findPersistenceScope(root);
148: assertNotNull(persistenceScope);
149: assertEquals(persistenceXml, persistenceScope
150: .getPersistenceXml());
151: assertEquals(1, listener.changeCount);
152: assertSame(persistenceScope, persistenceScopes
153: .getPersistenceScopes()[0]);
154: assertNotNull(persistenceScope.getEntityMappingsModel("unit"));
155:
156: // testing the persistence scope classpath
157: ClassPath scopeCP = persistenceScope.getClassPath();
158: assertNotNull(scopeCP);
159: persistenceScope = provider.findPersistenceScope(root);
160: assertSame("Should return the same classpath object", scopeCP,
161: persistenceScope.getClassPath());
162:
163: // removing persistence.xml
164: persistenceXml.delete();
165: assertNull("Should return a null persistence.xml",
166: persistenceScope.getPersistenceXml());
167: assertNull(persistenceScope.getEntityMappingsModel("unit"));
168: persistenceScope = provider.findPersistenceScope(root);
169: assertNull(persistenceScope);
170: assertEquals(2, listener.changeCount);
171: assertEquals(0, persistenceScopes.getPersistenceScopes().length);
172:
173: // re-adding persistence scope
174: persistenceLocation.createData("persistence.xml"); // NOI18N
175: persistenceScope = provider.findPersistenceScope(root);
176: assertTrue("Should always return a valid persistence.xml",
177: persistenceScope.getPersistenceXml().isValid());
178: }
179:
180: public void testEntityClassScope() throws Exception {
181: EntityClassScope entityClassScope = provider
182: .findEntityClassScope(root);
183: assertNotNull(entityClassScope);
184: assertNotNull(entityClassScope.getEntityMappingsModel(false));
185: assertNotNull(entityClassScope.getEntityMappingsModel(true));
186: }
187: }
|