1 19 20 package org.netbeans.modules.java.j2seproject.queries; 21 22 import java.io.IOException ; 23 import java.util.Collections ; 24 import java.util.Properties ; 25 import org.netbeans.api.java.classpath.ClassPath; 26 import org.netbeans.api.java.platform.JavaPlatform; 27 import org.netbeans.api.java.platform.Specification; 28 import org.netbeans.api.java.queries.SourceLevelQuery; 29 import org.netbeans.api.project.Project; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.junit.NbTestCase; 32 import org.netbeans.modules.java.platform.JavaPlatformProvider; 33 import org.netbeans.spi.project.support.ant.PropertyUtils; 34 import org.openide.filesystems.FileObject; 35 import org.netbeans.api.project.TestUtil; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.netbeans.spi.project.support.ant.EditableProperties; 38 import org.netbeans.spi.project.support.ant.ProjectGenerator; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.modules.SpecificationVersion; 41 42 47 public class SourceLevelQueryImplTest extends NbTestCase { 48 49 public SourceLevelQueryImplTest(String testName) { 50 super(testName); 51 } 52 53 private FileObject scratch; 54 private FileObject projdir; 55 private FileObject sources; 56 private FileObject tests; 57 private ProjectManager pm; 58 private Project pp; 59 60 protected void setUp() throws Exception { 61 super.setUp(); 62 TestUtil.setLookup(new Object [] { 63 new org.netbeans.modules.java.j2seproject.J2SEProjectType(), 64 new org.netbeans.modules.java.project.ProjectSourceLevelQueryImpl(), 65 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(), 66 new TestPlatformProvider () 67 }); 68 Properties p = System.getProperties(); 69 if (p.getProperty ("netbeans.user") == null) { 70 p.put("netbeans.user", FileUtil.toFile(TestUtil.makeScratchDir(this)).getAbsolutePath()); 71 } 72 } 73 74 protected void tearDown() throws Exception { 75 scratch = null; 76 projdir = null; 77 pm = null; 78 super.tearDown(); 79 } 80 81 82 private void prepareProject (String platformName) throws IOException { 83 scratch = TestUtil.makeScratchDir(this); 84 projdir = scratch.createFolder("proj"); 85 AntProjectHelper helper = ProjectGenerator.createProject(projdir, "org.netbeans.modules.java.j2seproject"); 86 pm = ProjectManager.getDefault(); 87 pp = pm.findProject(projdir); 88 EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 89 props.setProperty("javac.source", "${def}"); 90 props.setProperty ("platform.active",platformName); 91 props.setProperty("def", "1.2"); 92 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 93 props = PropertyUtils.getGlobalProperties(); 94 props.put("default.javac.source", "4.3"); 95 PropertyUtils.putGlobalProperties(props); 96 sources = projdir.createFolder("src"); 97 tests = projdir.createFolder("test"); 98 } 99 100 public void testGetSourceLevelWithValidPlatform() throws Exception { 101 this.prepareProject("TestPlatform"); 102 FileObject file = scratch.createData("some.java"); 103 String sl = SourceLevelQuery.getSourceLevel(file); 104 assertEquals("Non-project Java file does not have any source level", null, sl); 105 file = sources.createData("a.java"); 106 sl = SourceLevelQuery.getSourceLevel(file); 107 assertEquals("Project's Java file must have project's source", "1.2", sl); 108 } 109 110 public void testGetSourceLevelWithBrokenPlatform() throws Exception { 111 this.prepareProject("BrokenPlatform"); 112 FileObject file = scratch.createData("some.java"); 113 String sl = SourceLevelQuery.getSourceLevel(file); 114 assertEquals("Non-project Java file does not have any source level", null, sl); 115 file = sources.createData("a.java"); 116 sl = SourceLevelQuery.getSourceLevel(file); 117 assertEquals("Project's Java file must have project's source", "4.3", sl); 118 } 119 120 121 122 private static class TestPlatformProvider implements JavaPlatformProvider { 123 124 private JavaPlatform platform; 125 126 public void removePropertyChangeListener(java.beans.PropertyChangeListener listener) { 127 } 128 129 public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) { 130 } 131 132 public JavaPlatform[] getInstalledPlatforms() { 133 return new JavaPlatform[] { 134 getDefaultPlatform() 135 }; 136 } 137 138 public JavaPlatform getDefaultPlatform() { 139 if (this.platform == null) { 140 this.platform = new TestPlatform (); 141 } 142 return this.platform; 143 } 144 } 145 146 private static class TestPlatform extends JavaPlatform { 147 148 public FileObject findTool(String toolName) { 149 return null; 150 } 151 152 public String getVendor() { 153 return "me"; 154 } 155 156 public ClassPath getStandardLibraries() { 157 return null; 158 } 159 160 public Specification getSpecification() { 161 return new Specification ("j2se", new SpecificationVersion ("1.5")); 162 } 163 164 public ClassPath getSourceFolders() { 165 return null; 166 } 167 168 public java.util.Map getProperties() { 169 return Collections.singletonMap("platform.ant.name","TestPlatform"); 170 } 171 172 public java.util.List getJavadocFolders() { 173 return null; 174 } 175 176 public java.util.Collection getInstallFolders() { 177 return null; 178 } 179 180 public String getDisplayName() { 181 return "TestPlatform"; 182 } 183 184 public ClassPath getBootstrapLibraries() { 185 return null; 186 } 187 188 } 189 190 } 191 | Popular Tags |