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