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-2007 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.j2ee.earproject.model;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.List;
048: import javax.swing.DefaultListModel;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.modules.j2ee.common.project.classpath.ClassPathSupport;
053: import org.netbeans.modules.j2ee.dd.api.application.Application;
054: import org.netbeans.modules.j2ee.dd.api.application.ApplicationMetadata;
055: import org.netbeans.modules.j2ee.dd.api.application.Module;
056: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
057: import org.netbeans.modules.j2ee.earproject.EarProject;
058: import org.netbeans.modules.j2ee.earproject.classpath.ClassPathSupportCallbackImpl;
059: import org.netbeans.modules.j2ee.earproject.test.TestUtil;
060: import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
061: import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectPropertiesTest;
062: import org.netbeans.modules.j2ee.earproject.ui.wizards.NewEarProjectWizardIteratorTest;
063: import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
064: import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction;
065: import org.netbeans.spi.project.support.ant.EditableProperties;
066: import org.openide.filesystems.FileObject;
067: import org.openide.filesystems.FileUtil;
068:
069: /**
070: * Test case for {@link ApplicationImpl}.
071: * @author Tomas Mysik
072: */
073: public class ApplicationImplTest extends NbTestCase {
074:
075: private static final String CAR_NAME = "testEA-app-client";
076: private static final String CAR_REFERENCE_EXPECTED_KEY = "reference.testEA-app-client.j2ee-module-car";
077: private static final String CAR_REFERENCE_EXPECTED_VALUE = "${project.testEA-app-client}/dist/testEA-app-client.jar";
078: private static final String EJB_NAME = "testEA-ejb";
079: private static final String EJB_REFERENCE_EXPECTED_KEY = "reference.testEA-ejb.dist-ear";
080: private static final String EJB_REFERENCE_EXPECTED_VALUE = "${project.testEA-ejb}/dist/testEA-ejb.jar";
081: private static final String WEB_NAME = "testEA-web";
082: private static final String WEB_REFERENCE_EXPECTED_KEY = "reference.testEA-web.dist-ear";
083: private static final String WEB_REFERENCE_EXPECTED_VALUE = "${project.testEA-web}/dist/testEA-web.war";
084: private String serverID;
085: private EarProject earProject;
086:
087: public ApplicationImplTest(String testName) {
088: super (testName);
089: }
090:
091: @Override
092: protected void setUp() throws Exception {
093: super .setUp();
094:
095: clearWorkDir();
096: TestUtil.initLookup(this ,
097: "org/netbeans/modules/web/core/resources/layer.xml");
098: serverID = TestUtil.registerSunAppServer(this );
099:
100: // create project
101: File earDirF = new File(getWorkDir(), "testEA");
102: String name = "Test EnterpriseApplication";
103: String j2eeLevel = J2eeModule.JAVA_EE_5;
104: NewEarProjectWizardIteratorTest.generateEARProject(earDirF,
105: name, j2eeLevel, serverID, WEB_NAME, EJB_NAME,
106: CAR_NAME, null, null, null);
107: FileObject prjDirFO = FileUtil.toFileObject(earDirF);
108:
109: // verify war reference
110: EditableProperties ep = TestUtil
111: .loadProjectProperties(prjDirFO);
112: String webReferenceValue = ep
113: .getProperty(WEB_REFERENCE_EXPECTED_KEY);
114: assertEquals("war reference should be set properly",
115: WEB_REFERENCE_EXPECTED_VALUE, webReferenceValue);
116:
117: // verify ejb reference
118: String ejbReferenceValue = ep
119: .getProperty(EJB_REFERENCE_EXPECTED_KEY);
120: assertEquals("ejb reference should be set properly",
121: EJB_REFERENCE_EXPECTED_VALUE, ejbReferenceValue);
122:
123: // verify car reference
124: String carReferenceValue = ep
125: .getProperty(CAR_REFERENCE_EXPECTED_KEY);
126: assertEquals("car reference should be set properly",
127: CAR_REFERENCE_EXPECTED_VALUE, carReferenceValue);
128:
129: // get ear project from lookup
130: Project p = ProjectManager.getDefault().findProject(prjDirFO);
131: earProject = p.getLookup().lookup(EarProject.class);
132: assertNotNull("project should be created", earProject);
133: }
134:
135: /**
136: * <ul>
137: * Test for:
138: * <li>deployment descriptor <i>application.xml</i> should not be generated</li>
139: * <li>Application should not be <code>null</code></li>
140: * </ul>
141: * @throws Exception if any error occurs.
142: */
143: public void testGetApplication() throws Exception {
144: FileObject prjDirFO = earProject.getProjectDirectory();
145: assertNull("application.xml should not exist", prjDirFO
146: .getFileObject("src/conf/application.xml"));
147:
148: // test model
149: getModel().runReadAction(
150: new MetadataModelAction<ApplicationMetadata, Void>() {
151: public Void run(ApplicationMetadata metadata) {
152: Application application = metadata.getRoot();
153: assertNotNull("application should not be null",
154: application);
155: return null;
156: }
157: });
158: }
159:
160: /**
161: * <ul>
162: * Test for:
163: * <li>Application modules should be empty outside <code>runReadAction</code></li>
164: * </ul>
165: * <p>
166: * <b>This should not be ever done!</b>
167: * @throws Exception if any error occurs.
168: */
169: public void testApplicationOutsideOfRunReadAction()
170: throws Exception {
171:
172: // test model
173: Application application = getModel()
174: .runReadAction(
175: new MetadataModelAction<ApplicationMetadata, Application>() {
176: public Application run(
177: ApplicationMetadata metadata) {
178: Application application = metadata
179: .getRoot();
180:
181: // test application
182: int size = EarProjectProperties
183: .getJarContentAdditional(
184: earProject).size();
185: assertSame(
186: "application should contains exactly "
187: + size + " modules",
188: size, application.sizeModule());
189: return application;
190: }
191: });
192:
193: // test model
194: try {
195: application.sizeModule();
196: fail("should not get here");
197:
198: } catch (IllegalStateException expected) {
199: }
200: }
201:
202: /**
203: * <ul>
204: * Test for:
205: * <li>Application should always be appropriate for EAR project</li>
206: * </ul>
207: * <ol>
208: * This test should be as follows (but cannot be right now because of missing functionality of metadata model):
209: * <li>get model</li>
210: * <li>change EJB module</li>
211: * <li>verify model</li>
212: * </ol>
213: * @throws Exception if any error occurs.
214: */
215: public void testChangesInEAR() throws Exception {
216:
217: ClassPathSupport.Item ejb = getEjb(EarProjectProperties
218: .getJarContentAdditional(earProject));
219: assertEquals("ejb path should be ok", EJB_NAME + ".jar",
220: EarProjectProperties.getCompletePathInArchive(
221: earProject, ejb));
222:
223: // change ejb
224: final String otherPath = "otherPath";
225: List<ClassPathSupport.Item> modules = new ArrayList<ClassPathSupport.Item>();
226: modules.addAll(EarProjectProperties
227: .getJarContentAdditional(earProject));
228: ejb = getEjb(modules);
229: ejb.setAdditionalProperty(
230: ClassPathSupportCallbackImpl.PATH_IN_DEPLOYMENT,
231: otherPath);
232: EarProjectPropertiesTest
233: .putProperty(
234: earProject,
235: EarProjectProperties.JAR_CONTENT_ADDITIONAL,
236: modules,
237: EarProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES);
238:
239: // test model
240: getModel().runReadAction(
241: new MetadataModelAction<ApplicationMetadata, Void>() {
242: public Void run(ApplicationMetadata metadata) {
243: Application application = metadata.getRoot();
244:
245: // verify ejb
246: Module ejbModule = getEjbModule(application
247: .getModule());
248: assertEquals("ejb path should be ok", otherPath
249: + "/" + EJB_NAME + ".jar", ejbModule
250: .getEjb());
251: return null;
252: }
253: });
254: }
255:
256: private MetadataModel<ApplicationMetadata> getModel()
257: throws IOException, InterruptedException {
258: return earProject.getAppModule().getMetadataModel();
259: }
260:
261: private ClassPathSupport.Item getEjb(
262: List<ClassPathSupport.Item> modules) {
263: for (ClassPathSupport.Item vcpi : modules) {
264: if (vcpi.getReference().indexOf(EJB_REFERENCE_EXPECTED_KEY) != -1
265: /*&& EJB_REFERENCE_EXPECTED_VALUE.endsWith(vcpi.getEvaluated())*/) {
266: return vcpi;
267: }
268: }
269: return null;
270: }
271:
272: private Module getEjbModule(Module[] modules) {
273: for (Module m : modules) {
274: if (m.getEjb() != null) {
275: return m;
276: }
277: }
278: return null;
279: }
280: }
|