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.java.j2seproject.applet;
043:
044: import java.net.URL;
045: import java.beans.PropertyChangeListener;
046: import java.util.Map;
047: import java.util.Collection;
048: import java.util.List;
049: import java.util.Collections;
050: import org.openide.filesystems.FileObject;
051: import org.openide.filesystems.FileUtil;
052: import org.openide.util.Lookup;
053: import org.openide.modules.SpecificationVersion;
054: import org.netbeans.junit.NbTestCase;
055: import org.netbeans.api.project.TestUtil;
056: import org.netbeans.api.project.Project;
057: import org.netbeans.api.project.ProjectManager;
058: import org.netbeans.api.java.platform.JavaPlatform;
059: import org.netbeans.api.java.platform.Specification;
060: import org.netbeans.api.java.classpath.ClassPath;
061: import org.netbeans.spi.project.support.ant.AntProjectHelper;
062: import org.netbeans.spi.project.support.ant.EditableProperties;
063: import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
064: import org.netbeans.modules.java.j2seproject.J2SEProjectUtil;
065: import org.netbeans.modules.java.platform.JavaPlatformProvider;
066: import org.netbeans.spi.java.classpath.support.ClassPathSupport;
067:
068: public class AppletSupportTest extends NbTestCase {
069:
070: private FileObject scratch;
071: private FileObject projdir;
072: private AntProjectHelper helper;
073: private FileObject source;
074: private FileObject buildFolder;
075: private FileObject classesFolder;
076:
077: public AppletSupportTest(String name) {
078: super (name);
079: }
080:
081: protected void setUp() throws Exception {
082: super .setUp();
083: JavaPlatform platform1 = new TestPlatform("TP1", "tp1",
084: new Specification("j2se", new SpecificationVersion(
085: "1.4")));
086: JavaPlatform platform2 = new TestPlatform("TP2", "tp2",
087: new Specification("j2se", new SpecificationVersion(
088: "1.5")));
089: JavaPlatform platform3 = new TestPlatform("TP2", "tp3",
090: new Specification("j2se", new SpecificationVersion(
091: "1.5.1")));
092: TestUtil
093: .setLookup(new Object[] { new PlatformProviderImpl(
094: new JavaPlatform[] { platform1, platform2,
095: platform3 }), });
096: scratch = TestUtil.makeScratchDir(this );
097: FileObject folderWithSpaces = scratch
098: .createFolder("Folder With Spaces");
099: projdir = folderWithSpaces.createFolder("proj");
100: J2SEProjectGenerator
101: .setDefaultSourceLevel(new SpecificationVersion("1.4")); //NOI18N
102: helper = J2SEProjectGenerator.createProject(FileUtil
103: .toFile(projdir), "proj", null, null, null); //NOI18N
104: J2SEProjectGenerator.setDefaultSourceLevel(null);
105: Project p = ProjectManager.getDefault().findProject(projdir);
106:
107: FileObject src = projdir.getFileObject("src");
108: FileObject pkg = src.createFolder("pkg");
109: source = pkg.createData("Applet", "java");
110: EditableProperties ep = helper
111: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
112: String buildFolderName = (String) J2SEProjectUtil
113: .getEvaluatedProperty(p, ep.getProperty("build.dir"));
114: buildFolder = FileUtil.createFolder(projdir, buildFolderName);
115: String classesFolderName = (String) J2SEProjectUtil
116: .getEvaluatedProperty(p, ep
117: .getProperty("build.classes.dir"));
118: classesFolder = FileUtil.createFolder(projdir,
119: classesFolderName);
120: }
121:
122: protected void tearDown() throws Exception {
123: scratch = null;
124: projdir = null;
125: helper = null;
126: super .tearDown();
127: }
128:
129: public void testgenerateHtmlFileURL() throws Exception {
130: //Tests the JDK issue #6193279
131: URL url = AppletSupport.generateHtmlFileURL(source,
132: buildFolder, classesFolder, "tp1");
133: String[] parts = url.toExternalForm().split("/");
134: assertTrue(parts.length > 4);
135: assertEquals(parts[parts.length - 1], "Applet.html");
136: assertEquals(parts[parts.length - 2], "build");
137: assertEquals(parts[parts.length - 3], "proj");
138: assertEquals(parts[parts.length - 4], "Folder%20With%20Spaces");
139: url = AppletSupport.generateHtmlFileURL(source, buildFolder,
140: classesFolder, "tp2");
141: parts = url.toExternalForm().split("/");
142: assertTrue(parts.length > 4);
143: assertEquals(parts[parts.length - 1], "Applet.html");
144: assertEquals(parts[parts.length - 2], "build");
145: assertEquals(parts[parts.length - 3], "proj");
146: assertEquals(parts[parts.length - 4], "Folder With Spaces");
147: url = AppletSupport.generateHtmlFileURL(source, buildFolder,
148: classesFolder, null);
149: parts = url.toExternalForm().split("/");
150: assertTrue(parts.length > 4);
151: assertEquals(parts[parts.length - 1], "Applet.html");
152: assertEquals(parts[parts.length - 2], "build");
153: assertEquals(parts[parts.length - 3], "proj");
154: assertEquals(parts[parts.length - 4], "Folder%20With%20Spaces");
155: url = AppletSupport.generateHtmlFileURL(source, buildFolder,
156: classesFolder, "tp3");
157: parts = url.toExternalForm().split("/");
158: assertTrue(parts.length > 4);
159: assertEquals(parts[parts.length - 1], "Applet.html");
160: assertEquals(parts[parts.length - 2], "build");
161: assertEquals(parts[parts.length - 3], "proj");
162: assertEquals(parts[parts.length - 4], "Folder With Spaces");
163: }
164:
165: private static class PlatformProviderImpl implements
166: JavaPlatformProvider {
167: private JavaPlatform[] platforms;
168:
169: public PlatformProviderImpl(JavaPlatform[] platforms) {
170: this .platforms = platforms;
171: }
172:
173: public JavaPlatform[] getInstalledPlatforms() {
174: return platforms;
175: }
176:
177: public JavaPlatform getDefaultPlatform() {
178: return this .platforms[0];
179: }
180:
181: public void addPropertyChangeListener(
182: PropertyChangeListener listener) {
183: }
184:
185: public void removePropertyChangeListener(
186: PropertyChangeListener listener) {
187: }
188: }
189:
190: private static class TestPlatform extends JavaPlatform {
191:
192: private String displayName;
193: private Map props;
194: private Specification spec;
195:
196: public TestPlatform(String displayName, String antName,
197: Specification spec) {
198: this .displayName = displayName;
199: this .props = Collections.singletonMap("platform.ant.name",
200: antName);
201: this .spec = spec;
202: }
203:
204: public String getDisplayName() {
205: return this .displayName;
206: }
207:
208: public Specification getSpecification() {
209: return spec;
210: }
211:
212: public Map getProperties() {
213: return this .props;
214: }
215:
216: public ClassPath getBootstrapLibraries() {
217: return ClassPathSupport.createClassPath(new URL[0]);
218: }
219:
220: public ClassPath getStandardLibraries() {
221: return ClassPathSupport.createClassPath(new URL[0]);
222: }
223:
224: public String getVendor() {
225: return null;
226: }
227:
228: public Collection getInstallFolders() {
229: return null;
230: }
231:
232: public FileObject findTool(String toolName) {
233: return null;
234: }
235:
236: public ClassPath getSourceFolders() {
237: return null;
238: }
239:
240: public List getJavadocFolders() {
241: return null;
242: }
243: }
244: }
|