1 19 20 package org.netbeans.modules.java.j2seproject.classpath; 21 22 import java.beans.PropertyChangeListener ; 23 import java.beans.PropertyChangeSupport ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.Map ; 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 50 public class BootClassPathImplementationTest extends NbTestCase { 51 52 public BootClassPathImplementationTest(String 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 { 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 []{defaultPlatformBootRoot.getURL()}); 72 ClassPath expBCP = ClassPathSupport.createClassPath(new URL []{explicitPlatformBootRoot.getURL()}); 73 tp = new TestPlatformProvider (defBCP, expBCP); 74 TestUtil.setLookup(new Object [] { 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 { 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 platformName) throws IOException { 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 { 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 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 support; 135 private boolean hideExplicitPlatform; 136 137 public TestPlatformProvider (ClassPath defaultPlatformBootClassPath, ClassPath explicitPlatformBootClassPath) { 138 this.support = new PropertyChangeSupport (this); 139 this.defaultPlatform = new TestPlatform ("DefaultPlatform", defaultPlatformBootClassPath); 140 this.explicitPlatform = new TestPlatform ("ExplicitPlatform", explicitPlatformBootClassPath); 141 } 142 143 public void removePropertyChangeListener(PropertyChangeListener listener) { 144 this.support.removePropertyChangeListener (listener); 145 } 146 147 public void addPropertyChangeListener(PropertyChangeListener 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 systemName; 178 private Map properties; 179 private ClassPath bootClassPath; 180 181 public TestPlatform (String 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 toolName) { 188 return null; 189 } 190 191 public String 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 getProperties() { 208 return this.properties; 209 } 210 211 public List getJavadocFolders() { 212 return null; 213 } 214 215 public Collection getInstallFolders() { 216 return null; 217 } 218 219 public String getDisplayName() { 220 return this.systemName; 221 } 222 223 public ClassPath getBootstrapLibraries() { 224 return this.bootClassPath; 225 } 226 227 } 228 229 } 230 | Popular Tags |