KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.api.project.ProjectUtils;
24 import org.openide.modules.SpecificationVersion;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
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.api.project.TestUtil;
35 import org.netbeans.api.project.Sources;
36 import org.netbeans.api.queries.SharabilityQuery;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.EditableProperties;
39
40 public class J2SESharabilityQueryTest extends NbTestCase {
41
42     public J2SESharabilityQueryTest(String JavaDoc testName) {
43         super(testName);
44     }
45
46     private FileObject scratch;
47     private FileObject projdir;
48     private FileObject sources;
49     private FileObject tests;
50     private FileObject dist;
51     private FileObject build;
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 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);
67         sources = projdir.getFileObject("src");
68         tests = projdir.getFileObject("test");
69         dist = FileUtil.createFolder(projdir,"dist");
70         build = FileUtil.createFolder(projdir,"build");
71         pm = ProjectManager.getDefault();
72         Project p = pm.findProject(projdir);
73         assertTrue("Invalid project type",p instanceof J2SEProject);
74         pp = (J2SEProject) p;
75     }
76
77     protected void tearDown() throws Exception JavaDoc {
78         scratch = null;
79         projdir = null;
80         sources = null;
81         tests = null;
82         pm = null;
83         pp = null;
84         helper = null;
85         TestUtil.setLookup(Lookup.EMPTY);
86         super.tearDown();
87     }
88
89     public void testSharability () throws Exception JavaDoc {
90         File JavaDoc f = FileUtil.toFile (this.sources);
91         int res = SharabilityQuery.getSharability(f);
92         assertEquals("Sources must be sharable", SharabilityQuery.SHARABLE, res);
93         f = FileUtil.toFile (this.tests);
94         res = SharabilityQuery.getSharability(f);
95         assertEquals("Tests must be sharable", SharabilityQuery.SHARABLE, res);
96         f = FileUtil.toFile (this.build);
97         res = SharabilityQuery.getSharability(f);
98         assertEquals("Build can't be sharable", SharabilityQuery.NOT_SHARABLE, res);
99         f = FileUtil.toFile (this.dist);
100         res = SharabilityQuery.getSharability(f);
101         assertEquals("Dist can't be sharable", SharabilityQuery.NOT_SHARABLE, res);
102         FileObject newSourceRoot = addSourceRoot(helper, projdir, "src2.dir",new File JavaDoc(FileUtil.toFile(scratch),"sources2"));
103         ProjectUtils.getSources(pp).getSourceGroups(Sources.TYPE_GENERIC);
104         f = FileUtil.toFile (newSourceRoot);
105         res = SharabilityQuery.getSharability(f);
106         assertEquals("Sources2 must be sharable", SharabilityQuery.SHARABLE, res);
107         FileObject newSourceRoot2 = changeSourceRoot(helper, projdir, "src2.dir", new File JavaDoc(FileUtil.toFile(scratch),"sources3"));
108         f = FileUtil.toFile (newSourceRoot2);
109         res = SharabilityQuery.getSharability(f);
110         assertEquals("Sources3 must be sharable", SharabilityQuery.SHARABLE, res);
111         f = FileUtil.toFile (newSourceRoot);
112         res = SharabilityQuery.getSharability(f);
113         assertEquals("Sources2 must be unknown", SharabilityQuery.UNKNOWN, res);
114     }
115
116     public static FileObject addSourceRoot (AntProjectHelper helper, FileObject projdir,
117                                             String JavaDoc propName, File JavaDoc folder) throws Exception JavaDoc {
118         if (!folder.exists()) {
119             folder.mkdirs();
120         }
121         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
122         NodeList JavaDoc nl = data.getElementsByTagNameNS (J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
123         assert nl.getLength() == 1;
124         Element JavaDoc roots = (Element JavaDoc) nl.item(0);
125         Document JavaDoc doc = roots.getOwnerDocument();
126         Element JavaDoc root = doc.createElementNS(J2SEProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root");
127         root.setAttribute("id", propName);
128         roots.appendChild (root);
129         helper.putPrimaryConfigurationData (data,true);
130         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
131         props.put (propName,folder.getAbsolutePath());
132         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
133         return FileUtil.toFileObject(folder);
134     }
135
136     public static FileObject changeSourceRoot (AntProjectHelper helper, FileObject projdir,
137                                                String JavaDoc propName, File JavaDoc folder) throws Exception JavaDoc {
138         if (!folder.exists()) {
139             folder.mkdirs();
140         }
141         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
142         assert props.containsKey(propName);
143         props.put (propName,folder.getAbsolutePath());
144         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
145         return FileUtil.toFileObject(folder);
146     }
147
148 }
149
Popular Tags