1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.net.URL ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeEvent ; 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Collections ; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.modules.SpecificationVersion; 31 import org.openide.util.Lookup; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.api.project.Project; 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.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 import org.w3c.dom.Document ; 41 42 public class SourceRootsTest extends NbTestCase { 43 44 public SourceRootsTest (String testName) { 45 super(testName); 46 } 47 48 private FileObject scratch; 49 private FileObject projdir; 50 private FileObject sources; 51 private FileObject tests; 52 private ProjectManager pm; 53 private J2SEProject pp; 54 private AntProjectHelper helper; 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 TestUtil.setLookup(new Object [] { 59 new org.netbeans.modules.java.j2seproject.J2SEProjectType(), 60 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation() 61 }); 62 scratch = TestUtil.makeScratchDir(this); 63 projdir = scratch.createFolder("proj"); 64 J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); J2SEProjectGenerator.setDefaultSourceLevel(null); sources = projdir.getFileObject("src"); 68 tests = projdir.getFileObject("test"); 69 pm = ProjectManager.getDefault(); 70 Project p = pm.findProject(projdir); 71 assertTrue("Invalid project type",p instanceof J2SEProject); 72 pp = (J2SEProject) p; 73 } 74 75 protected void tearDown() throws Exception { 76 scratch = null; 77 projdir = null; 78 sources = null; 79 tests = null; 80 pm = null; 81 pp = null; 82 helper = null; 83 TestUtil.setLookup(Lookup.EMPTY); 84 super.tearDown(); 85 } 86 87 public void testSourceRoots () throws Exception { 88 SourceRoots sources = pp.getSourceRoots(); 89 String [] srcProps = sources.getRootProperties(); 90 assertNotNull ("Source properties can not be null",srcProps); 91 assertEquals ("Source properties length must be 1",1,srcProps.length); 92 assertEquals("Source property should be src.dir","src.dir",srcProps[0]); 93 FileObject[] srcFos = sources.getRoots(); 94 assertNotNull ("Roots can not be null",srcFos); 95 assertEquals ("Roots length must be 1",1,srcFos.length); 96 assertEquals("Root should be "+this.sources.getPath(),this.sources,srcFos[0]); 97 URL [] srcURLs = sources.getRootURLs(); 98 assertNotNull ("Root URLs can not be null",srcURLs); 99 assertEquals ("Root URLs length must be 1",1,srcURLs.length); 100 assertEquals("Root URLs should be "+this.sources.getURL(),this.sources.getURL(),srcURLs[0]); 101 SourceRoots tests = pp.getTestSourceRoots(); 102 srcProps = tests.getRootProperties(); 103 assertNotNull ("Source properties can not be null",srcProps); 104 assertEquals ("Source properties length must be 1",1,srcProps.length); 105 assertEquals("Source property should be test.src.dir","test.src.dir",srcProps[0]); 106 srcFos = tests.getRoots(); 107 assertNotNull ("Roots can not be null",srcFos); 108 assertEquals ("Roots length must be 1",1,srcFos.length); 109 assertEquals("Root should be "+this.tests.getPath(),this.tests,srcFos[0]); 110 srcURLs = tests.getRootURLs(); 111 assertNotNull ("Root URLs can not be null",srcURLs); 112 assertEquals ("Root URLs length must be 1",1,srcURLs.length); 113 assertEquals("Root URLs should be "+this.tests.getURL(),this.tests.getURL(),srcURLs[0]); 114 TestListener tl = new TestListener(); 116 sources.addPropertyChangeListener (tl); 117 FileObject newRoot = addSourceRoot (helper, projdir, "src.other.dir","other"); 118 srcProps = sources.getRootProperties(); 119 assertNotNull ("Source properties can not be null",srcProps); 120 assertEquals ("Source properties length must be 2",2,srcProps.length); 121 assertEquals("The first source property should be src.dir","src.dir",srcProps[0]); 122 assertEquals("The second source property should be src.other.dir","src.other.dir",srcProps[1]); 123 srcFos = sources.getRoots(); 124 assertNotNull ("Roots can not be null",srcFos); 125 assertEquals ("Roots length must be 2",2,srcFos.length); 126 assertEquals("The first root should be "+this.sources.getPath(),this.sources,srcFos[0]); 127 assertEquals("The second root should be "+newRoot.getPath(),newRoot,srcFos[1]); 128 srcURLs = sources.getRootURLs(); 129 assertNotNull ("Root URLs can not be null",srcURLs); 130 assertEquals ("Root URLs length must be 2",2,srcURLs.length); 131 assertEquals("The first root URLs should be "+this.sources.getURL(),this.sources.getURL(),srcURLs[0]); 132 assertEquals("The second root URLs should be "+newRoot.getURL(),newRoot.getURL(),srcURLs[1]); 133 Set events = tl.getEvents(); 134 assertTrue ("PROP_ROOT_PROPERTIES has to be fired",events.contains(SourceRoots.PROP_ROOT_PROPERTIES)); 135 assertTrue ("PROP_ROOTS has to be fired",events.contains(SourceRoots.PROP_ROOTS)); 136 tl.reset(); 137 newRoot = changeSourceRoot (helper, projdir, "src.other.dir","other2"); 138 srcProps = sources.getRootProperties(); 139 assertNotNull ("Source properties can not be null",srcProps); 140 assertEquals ("Source properties length must be 2",2,srcProps.length); 141 assertEquals("The first source property should be src.dir","src.dir",srcProps[0]); 142 assertEquals("The second source property should be src.other.dir","src.other.dir",srcProps[1]); 143 srcFos = sources.getRoots(); 144 assertNotNull ("Roots can not be null",srcFos); 145 assertEquals ("Roots length must be 2",2,srcFos.length); 146 assertEquals("The first root should be "+this.sources.getPath(),this.sources,srcFos[0]); 147 assertEquals("The second root should be "+newRoot.getPath(),newRoot,srcFos[1]); 148 srcURLs = sources.getRootURLs(); 149 assertNotNull ("Root URLs can not be null",srcURLs); 150 assertEquals ("Root URLs length must be 2",2,srcURLs.length); 151 assertEquals("The first root URLs should be "+this.sources.getURL(),this.sources.getURL(),srcURLs[0]); 152 assertEquals("The second root URLs should be "+newRoot.getURL(),newRoot.getURL(),srcURLs[1]); 153 events = tl.getEvents(); 154 assertEquals(Collections.singleton(SourceRoots.PROP_ROOTS), events); 155 sources.removePropertyChangeListener(tl); 156 } 157 158 public static FileObject addSourceRoot (AntProjectHelper helper, FileObject projdir, 159 String propName, String folderName) throws Exception { 160 Element data = helper.getPrimaryConfigurationData(true); 161 NodeList nl = data.getElementsByTagNameNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); 162 assert nl.getLength() == 1; 163 Element roots = (Element ) nl.item(0); 164 Document doc = roots.getOwnerDocument(); 165 Element root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root"); 166 root.setAttribute("id", propName); 167 roots.appendChild (root); 168 helper.putPrimaryConfigurationData (data,true); 169 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 170 props.put (propName,folderName); 171 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props); 172 FileObject fo = projdir.getFileObject(folderName); 173 if (fo==null) { 174 fo = projdir.createFolder(folderName); 175 } 176 return fo; 177 } 178 179 public static FileObject changeSourceRoot (AntProjectHelper helper, FileObject projdir, 180 String propName, String folderName) throws Exception { 181 EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH); 182 assert props.containsKey(propName); 183 props.put (propName,folderName); 184 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props); 185 FileObject fo = projdir.getFileObject(folderName); 186 if (fo==null) { 187 fo = projdir.createFolder(folderName); 188 } 189 return fo; 190 } 191 192 private static final class TestListener implements PropertyChangeListener { 193 Set events = new HashSet (); 194 195 public void propertyChange(PropertyChangeEvent evt) { 196 String propName = evt.getPropertyName(); 197 if (propName != null) { 198 this.events.add (propName); 199 } 200 } 201 202 public void reset () { 203 this.events.clear(); 204 } 205 206 public Set getEvents () { 207 return Collections.unmodifiableSet(this.events); 208 } 209 } 210 211 } 212 | Popular Tags |