KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > classpath > BootClassPathImplementationTest


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.classpath;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import org.netbeans.api.java.classpath.ClassPath;
31 import org.netbeans.api.java.platform.JavaPlatform;
32 import org.netbeans.api.java.platform.Specification;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ProjectManager;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.java.platform.JavaPlatformProvider;
37 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
38 import org.openide.filesystems.FileObject;
39 import org.netbeans.api.project.TestUtil;
40 import org.netbeans.spi.project.support.ant.AntProjectHelper;
41 import org.netbeans.spi.project.support.ant.EditableProperties;
42 import org.netbeans.spi.project.support.ant.ProjectGenerator;
43 import org.openide.modules.SpecificationVersion;
44 import org.openide.util.Lookup;
45
46 /**
47  * Tests for {@link BootClassPathImplementation}.
48  * @author Tomas Zezula
49  */

50 public class BootClassPathImplementationTest extends NbTestCase {
51     
52     public BootClassPathImplementationTest(String JavaDoc testName) {
53         super(testName);
54     }
55     
56     private FileObject scratch;
57     private FileObject projdir;
58     private FileObject sources;
59     private FileObject tests;
60     private FileObject defaultPlatformBootRoot;
61     private FileObject explicitPlatformBootRoot;
62     private ProjectManager pm;
63     private Project pp;
64     private TestPlatformProvider tp;
65     
66     protected void setUp() throws Exception JavaDoc {
67         super.setUp();
68         scratch = TestUtil.makeScratchDir(this);
69         this.defaultPlatformBootRoot = scratch.createFolder("DefaultPlatformBootRoot");
70         this.explicitPlatformBootRoot = scratch.createFolder("ExplicitPlatformBootRoot");
71         ClassPath defBCP = ClassPathSupport.createClassPath(new URL JavaDoc[]{defaultPlatformBootRoot.getURL()});
72         ClassPath expBCP = ClassPathSupport.createClassPath(new URL JavaDoc[]{explicitPlatformBootRoot.getURL()});
73         tp = new TestPlatformProvider (defBCP, expBCP);
74         TestUtil.setLookup(new Object JavaDoc[] {
75             new org.netbeans.modules.java.j2seproject.J2SEProjectType(),
76             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
77             tp
78         });
79     }
80
81     protected void tearDown() throws Exception JavaDoc {
82         scratch = null;
83         projdir = null;
84         pm = null;
85         TestUtil.setLookup(Lookup.EMPTY);
86         super.tearDown();
87     }
88     
89     
90     private void prepareProject (String JavaDoc platformName) throws IOException JavaDoc {
91         projdir = scratch.createFolder("proj");
92         AntProjectHelper helper = ProjectGenerator.createProject(projdir, "org.netbeans.modules.java.j2seproject");
93         pm = ProjectManager.getDefault();
94         pp = pm.findProject(projdir);
95         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
96         props.setProperty ("platform.active",platformName);
97         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
98         sources = projdir.createFolder("src");
99         tests = projdir.createFolder("test");
100     }
101     
102     public void testBootClassPathImplementation () throws Exception JavaDoc {
103         this.prepareProject("ExplicitPlatform");
104         FileObject file = sources.createData("a.java");
105         ClassPath bootCP = ClassPath.getClassPath(file, ClassPath.BOOT);
106         assertNotNull("Boot ClassPath exists",bootCP);
107         FileObject[] roots = bootCP.getRoots();
108         assertEquals("Boot classpath size",1, roots.length);
109         assertEquals("Boot classpath",explicitPlatformBootRoot, roots[0]);
110         
111         tp.setExplicitPlatformVisible(false);
112         bootCP = ClassPath.getClassPath(file, ClassPath.BOOT);
113         assertNotNull("Boot ClassPath exists",bootCP);
114         roots = bootCP.getRoots();
115         //Change of the behavior of the BootClassPathImplementation,
116
// it should not return the default platform in case of broken project's platform
117
// see issue #57641:rt.jar and src.zip are scanned when it's not neccessary
118
assertEquals("Boot classpath size",0, roots.length);
119                 
120         tp.setExplicitPlatformVisible(true);
121         bootCP = ClassPath.getClassPath(file, ClassPath.BOOT);
122         assertNotNull("Boot ClassPath exists",bootCP);
123         roots = bootCP.getRoots();
124         assertEquals("Boot classpath size",1, roots.length);
125         assertEquals("Boot classpath",explicitPlatformBootRoot, roots[0]);
126     }
127     
128     
129     
130     private static class TestPlatformProvider implements JavaPlatformProvider {
131         
132         private JavaPlatform defaultPlatform;
133         private JavaPlatform explicitPlatform;
134         private PropertyChangeSupport JavaDoc support;
135         private boolean hideExplicitPlatform;
136         
137         public TestPlatformProvider (ClassPath defaultPlatformBootClassPath, ClassPath explicitPlatformBootClassPath) {
138             this.support = new PropertyChangeSupport JavaDoc (this);
139             this.defaultPlatform = new TestPlatform ("DefaultPlatform", defaultPlatformBootClassPath);
140             this.explicitPlatform = new TestPlatform ("ExplicitPlatform", explicitPlatformBootClassPath);
141         }
142         
143         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
144             this.support.removePropertyChangeListener (listener);
145         }
146
147         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
148             this.support.addPropertyChangeListener(listener);
149         }
150
151         public JavaPlatform[] getInstalledPlatforms() {
152             if (this.hideExplicitPlatform) {
153                 return new JavaPlatform[] {
154                     this.defaultPlatform,
155                 };
156             }
157             else {
158                 return new JavaPlatform[] {
159                     this.defaultPlatform,
160                     this.explicitPlatform,
161                 };
162             }
163         }
164        
165         public JavaPlatform getDefaultPlatform () {
166             return this.defaultPlatform;
167         }
168         
169         public void setExplicitPlatformVisible (boolean value) {
170             this.hideExplicitPlatform = !value;
171             this.support.firePropertyChange(PROP_INSTALLED_PLATFORMS,null,null);
172         }
173     }
174     
175     private static class TestPlatform extends JavaPlatform {
176         
177         private String JavaDoc systemName;
178         private Map JavaDoc properties;
179         private ClassPath bootClassPath;
180         
181         public TestPlatform (String JavaDoc systemName, ClassPath bootCP) {
182             this.systemName = systemName;
183             this.bootClassPath = bootCP;
184             this.properties = Collections.singletonMap("platform.ant.name",this.systemName);
185         }
186         
187         public FileObject findTool(String JavaDoc toolName) {
188             return null;
189         }
190
191         public String JavaDoc getVendor() {
192             return "me";
193         }
194
195         public ClassPath getStandardLibraries() {
196             return null;
197         }
198
199         public Specification getSpecification() {
200             return new Specification ("j2se", new SpecificationVersion ("1.5"));
201         }
202
203         public ClassPath getSourceFolders() {
204             return null;
205         }
206
207         public Map JavaDoc getProperties() {
208             return this.properties;
209         }
210
211         public List JavaDoc getJavadocFolders() {
212             return null;
213         }
214
215         public Collection JavaDoc getInstallFolders() {
216             return null;
217         }
218
219         public String JavaDoc getDisplayName() {
220             return this.systemName;
221         }
222
223         public ClassPath getBootstrapLibraries() {
224             return this.bootClassPath;
225         }
226         
227     }
228     
229 }
230
Popular Tags