KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > applet > AppletSupportTest


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.applet;
21
22 import java.net.URL JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Collections JavaDoc;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.util.Lookup;
31 import org.openide.modules.SpecificationVersion;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.api.project.TestUtil;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.api.java.platform.JavaPlatform;
37 import org.netbeans.api.java.platform.Specification;
38 import org.netbeans.api.java.classpath.ClassPath;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.spi.project.support.ant.EditableProperties;
41 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator;
42 import org.netbeans.modules.java.j2seproject.J2SEProjectUtil;
43 import org.netbeans.modules.java.platform.JavaPlatformProvider;
44
45 public class AppletSupportTest extends NbTestCase {
46
47     private FileObject scratch;
48     private FileObject projdir;
49     private AntProjectHelper helper;
50     private FileObject source;
51     private FileObject buildFolder;
52     private FileObject classesFolder;
53
54     public AppletSupportTest (String JavaDoc name) {
55         super (name);
56     }
57
58      protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60         JavaPlatform platform1 = new TestPlatform("TP1","tp1",new Specification("j2se", new SpecificationVersion("1.4")));
61         JavaPlatform platform2 = new TestPlatform("TP2","tp2",new Specification("j2se", new SpecificationVersion("1.5")));
62         JavaPlatform platform3 = new TestPlatform("TP2","tp3",new Specification("j2se", new SpecificationVersion("1.5.1")));
63         TestUtil.setLookup(new Object JavaDoc[] {
64             new PlatformProviderImpl(new JavaPlatform[] {
65                 platform1,
66                 platform2,
67                 platform3
68             }),
69         });
70         scratch = TestUtil.makeScratchDir(this);
71         FileObject folderWithSpaces = scratch.createFolder("Folder With Spaces");
72         projdir = folderWithSpaces.createFolder("proj");
73         J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
74
helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); //NOI18N
75
J2SEProjectGenerator.setDefaultSourceLevel(null);
76         Project p = ProjectManager.getDefault().findProject(projdir);
77
78         FileObject src = projdir.getFileObject("src");
79         FileObject pkg = src.createFolder("pkg");
80         source = pkg.createData("Applet","java");
81         EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
82         String JavaDoc buildFolderName = (String JavaDoc) J2SEProjectUtil.getEvaluatedProperty (p, ep.getProperty("build.dir"));
83         buildFolder = FileUtil.createFolder(projdir,buildFolderName);
84         String JavaDoc classesFolderName = (String JavaDoc) J2SEProjectUtil.getEvaluatedProperty(p, ep.getProperty("build.classes.dir"));
85         classesFolder = FileUtil.createFolder(projdir,classesFolderName);
86     }
87
88     protected void tearDown() throws Exception JavaDoc {
89         scratch = null;
90         projdir = null;
91         helper = null;
92         super.tearDown();
93     }
94
95     public void testgenerateHtmlFileURL () throws Exception JavaDoc {
96         //Tests the JDK issue #6193279
97
URL JavaDoc url = AppletSupport.generateHtmlFileURL(source,buildFolder,classesFolder,"tp1");
98         String JavaDoc[] parts = url.toExternalForm().split("/");
99         assertTrue (parts.length>4);
100         assertEquals (parts[parts.length-1],"Applet.html");
101         assertEquals (parts[parts.length-2],"build");
102         assertEquals (parts[parts.length-3],"proj");
103         assertEquals (parts[parts.length-4],"Folder%20With%20Spaces");
104         url = AppletSupport.generateHtmlFileURL(source,buildFolder,classesFolder,"tp2");
105         parts = url.toExternalForm().split("/");
106         assertTrue (parts.length>4);
107         assertEquals (parts[parts.length-1],"Applet.html");
108         assertEquals (parts[parts.length-2],"build");
109         assertEquals (parts[parts.length-3],"proj");
110         assertEquals (parts[parts.length-4],"Folder With Spaces");
111         url = AppletSupport.generateHtmlFileURL(source,buildFolder,classesFolder,null);
112         parts = url.toExternalForm().split("/");
113         assertTrue (parts.length>4);
114         assertEquals (parts[parts.length-1],"Applet.html");
115         assertEquals (parts[parts.length-2],"build");
116         assertEquals (parts[parts.length-3],"proj");
117         assertEquals (parts[parts.length-4],"Folder%20With%20Spaces");
118         url = AppletSupport.generateHtmlFileURL(source,buildFolder,classesFolder,"tp3");
119         parts = url.toExternalForm().split("/");
120         assertTrue (parts.length>4);
121         assertEquals (parts[parts.length-1],"Applet.html");
122         assertEquals (parts[parts.length-2],"build");
123         assertEquals (parts[parts.length-3],"proj");
124         assertEquals (parts[parts.length-4],"Folder With Spaces");
125     }
126
127     private static class PlatformProviderImpl implements JavaPlatformProvider {
128         private JavaPlatform[] platforms;
129
130         public PlatformProviderImpl (JavaPlatform[] platforms) {
131             this.platforms = platforms;
132         }
133
134         public JavaPlatform[] getInstalledPlatforms() {
135             return platforms;
136         }
137
138         public JavaPlatform getDefaultPlatform() {
139             return this.platforms[0];
140         }
141
142         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
143         }
144
145         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
146         }
147     }
148
149     private static class TestPlatform extends JavaPlatform {
150
151         private String JavaDoc displayName;
152         private Map JavaDoc props;
153         private Specification spec;
154
155         public TestPlatform (String JavaDoc displayName, String JavaDoc antName, Specification spec) {
156             this.displayName = displayName;
157             this.props = Collections.singletonMap("platform.ant.name",antName);
158             this.spec = spec;
159         }
160
161         public String JavaDoc getDisplayName() {
162             return this.displayName;
163         }
164
165         public Specification getSpecification() {
166             return spec;
167         }
168
169         public Map JavaDoc getProperties() {
170             return this.props;
171         }
172
173         public ClassPath getBootstrapLibraries() {
174             return null;
175         }
176
177         public ClassPath getStandardLibraries() {
178             return null;
179         }
180
181         public String JavaDoc getVendor() {
182             return null;
183         }
184
185         public Collection JavaDoc getInstallFolders() {
186             return null;
187         }
188
189         public FileObject findTool(String JavaDoc toolName) {
190             return null;
191         }
192
193         public ClassPath getSourceFolders() {
194             return null;
195         }
196
197         public List JavaDoc getJavadocFolders() {
198             return null;
199         }
200     }
201 }
202
Popular Tags