KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > SourceRootsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seproject;
21
22 import java.net.URL JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Collections JavaDoc;
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 JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40 import org.w3c.dom.Document JavaDoc;
41
42 public class SourceRootsTest extends NbTestCase {
43
44     public SourceRootsTest (String JavaDoc 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 JavaDoc {
57         super.setUp();
58         TestUtil.setLookup(new Object JavaDoc[] {
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")); //NOI18N
65
helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); //NOI18N
66
J2SEProjectGenerator.setDefaultSourceLevel(null); //NOI18N
67
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 JavaDoc {
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 JavaDoc {
88         SourceRoots sources = pp.getSourceRoots();
89         String JavaDoc[] 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 JavaDoc[] 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         //Now add new source root
115
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 JavaDoc 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 JavaDoc propName, String JavaDoc folderName) throws Exception JavaDoc {
160         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
161         NodeList JavaDoc nl = data.getElementsByTagNameNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
162         assert nl.getLength() == 1;
163         Element JavaDoc roots = (Element JavaDoc) nl.item(0);
164         Document JavaDoc doc = roots.getOwnerDocument();
165         Element JavaDoc 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 JavaDoc propName, String JavaDoc folderName) throws Exception JavaDoc {
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 JavaDoc {
193         Set JavaDoc events = new HashSet JavaDoc ();
194
195         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
196             String JavaDoc 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 JavaDoc getEvents () {
207             return Collections.unmodifiableSet(this.events);
208         }
209     }
210
211 }
212
Popular Tags