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