KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import org.netbeans.api.java.project.JavaProjectConstants;
26 import org.netbeans.api.java.queries.SourceForBinaryQuery;
27 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties;
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.ProjectUtils;
36 import org.netbeans.api.project.SourceGroup;
37 import org.netbeans.api.project.TestUtil;
38 import org.netbeans.spi.project.support.ant.AntProjectHelper;
39 import org.netbeans.spi.project.support.ant.EditableProperties;
40 import org.netbeans.spi.project.ui.ProjectOpenedHook;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.w3c.dom.Document JavaDoc;
44
45 /**
46  * Tests J2SESources
47  * Tests if SourceForBinaryQuery works fine on external build folder.
48  *
49  * @author Tomas Zezula
50  */

51 public class J2SESourcesTest extends NbTestCase {
52
53     public J2SESourcesTest(String JavaDoc testName) {
54         super(testName);
55     }
56
57     private FileObject scratch;
58     private FileObject projdir;
59     private FileObject sources;
60     private FileObject build;
61     private FileObject classes;
62     private ProjectManager pm;
63     private Project project;
64     private AntProjectHelper helper;
65
66     protected void setUp() throws Exception JavaDoc {
67         super.setUp();
68         TestUtil.setLookup(new Object JavaDoc[] {
69             new org.netbeans.modules.java.j2seproject.J2SEProjectType(),
70             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation()
71         });
72         scratch = TestUtil.makeScratchDir(this);
73         projdir = scratch.createFolder("proj");
74         J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
75
helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); //NOI18N
76
J2SEProjectGenerator.setDefaultSourceLevel(null);
77         sources = this.getFileObject(projdir, "src");
78         build = this.getFileObject (scratch, "build");
79         classes = this.getFileObject(build,"classes");
80         File JavaDoc f = FileUtil.normalizeFile (FileUtil.toFile(build));
81         String JavaDoc path = f.getAbsolutePath ();
82 //#47657: SourcesHelper.remarkExternalRoots () does not work on deleted folders
83
// To reproduce it uncomment following line
84
// build.delete();
85
EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
86         props.setProperty(J2SEProjectProperties.BUILD_DIR, path);
87         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
88         pm = ProjectManager.getDefault();
89         project = pm.findProject(projdir);
90         assertTrue("Invalid project type", project instanceof J2SEProject);
91     }
92
93     protected void tearDown() throws Exception JavaDoc {
94         scratch = null;
95         projdir = null;
96         sources = null;
97         build = null;
98         classes = null;
99         pm = null;
100         project = null;
101         helper = null;
102         TestUtil.setLookup(Lookup.EMPTY);
103         super.tearDown();
104     }
105
106     public void testSourceRoots () throws Exception JavaDoc {
107         FileObject[] roots = SourceForBinaryQuery.findSourceRoots(classes.getURL()).getRoots();
108         assertNotNull (roots);
109         assertEquals("There should be 1 src root",1,roots.length);
110         assertTrue ("The source root is not valid", sources.isValid());
111         assertEquals("Invalid src root", sources, roots[0]);
112         FileObject src2 = projdir.createFolder("src2");
113         addSourceRoot (helper, src2, "src2");
114         roots = SourceForBinaryQuery.findSourceRoots(classes.getURL()).getRoots();
115         assertNotNull (roots);
116         assertEquals("There should be 2 src roots", 2, roots.length);
117         assertTrue ("The source root is not valid", sources.isValid());
118         assertEquals("Invalid src root", sources, roots[0]);
119         assertTrue ("The source root 2 is not valid", src2.isValid());
120         assertEquals("Invalid src2 root", src2, roots[1]);
121     }
122
123     public void testIncludesExcludes() throws Exception JavaDoc {
124         SourceGroup g = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)[0];
125         assertEquals(sources, g.getRootFolder());
126         FileObject objectJava = FileUtil.createData(sources, "java/lang/Object.java");
127         FileObject jcJava = FileUtil.createData(sources, "javax/swing/JComponent.java");
128         FileObject doc = FileUtil.createData(sources, "javax/swing/doc-files/index.html");
129         assertTrue(g.contains(objectJava));
130         assertTrue(g.contains(jcJava));
131         assertTrue(g.contains(doc));
132         Method JavaDoc projectOpened = ProjectOpenedHook.class.getDeclaredMethod("projectOpened");
133         projectOpened.setAccessible(true);
134         projectOpened.invoke(project.getLookup().lookup(ProjectOpenedHook.class));
135         EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
136         assertEquals("includes/excludes were initialized to defaults", "**", ep.getProperty(J2SEProjectProperties.INCLUDES));
137         assertEquals("includes/excludes were initialized to defaults", "", ep.getProperty(J2SEProjectProperties.EXCLUDES));
138         ep.setProperty(J2SEProjectProperties.INCLUDES, "javax/swing/");
139         ep.setProperty(J2SEProjectProperties.EXCLUDES, "**/doc-files/");
140         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
141         pm.saveProject(project);
142         assertFalse(g.contains(objectJava));
143         assertTrue(g.contains(jcJava));
144         assertFalse(g.contains(doc));
145     }
146     
147     private static FileObject getFileObject (FileObject parent, String JavaDoc name) throws IOException JavaDoc {
148         FileObject result = parent.getFileObject(name);
149         if (result == null) {
150             result = parent.createFolder(name);
151         }
152         return result;
153     }
154     
155
156     private static void addSourceRoot (AntProjectHelper helper, FileObject sourceFolder, String JavaDoc propName) throws Exception JavaDoc {
157         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
158         NodeList JavaDoc nl = data.getElementsByTagNameNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
159         assert nl.getLength() == 1;
160         Element JavaDoc roots = (Element JavaDoc) nl.item(0);
161         Document JavaDoc doc = roots.getOwnerDocument();
162         Element JavaDoc root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root");
163         root.setAttribute("id", propName);
164         roots.appendChild (root);
165         helper.putPrimaryConfigurationData (data,true);
166         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
167         File JavaDoc f = FileUtil.normalizeFile(FileUtil.toFile(sourceFolder));
168         props.put (propName, f.getAbsolutePath());
169         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
170     }
171
172
173 }
174
Popular Tags