KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebSharabilityQueryTest


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.web.project;
21
22 import java.io.File JavaDoc;
23 import org.netbeans.api.project.ProjectUtils;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.util.Lookup;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.api.queries.SharabilityQuery;
35 import org.netbeans.modules.web.project.test.TestBase;
36 import org.netbeans.modules.web.project.test.TestUtil;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.EditableProperties;
39
40 public class WebSharabilityQueryTest extends NbTestCase {
41
42     public WebSharabilityQueryTest(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 docRoot;
51     private FileObject dist;
52     private FileObject build;
53     private WebProject pp;
54     private AntProjectHelper helper;
55
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         
59         TestBase.setLookup(new Object JavaDoc[] {
60             new WebProjectType(),
61             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation()
62         });
63         
64         File JavaDoc f = new File JavaDoc(getDataDir().getAbsolutePath(), "projects/WebApplication1");
65         projdir = FileUtil.toFileObject(f);
66
67         scratch = TestUtil.makeScratchDir(this);
68         sources = projdir.getFileObject("src/java");
69         tests = projdir.getFileObject("test");
70         docRoot = projdir.getFileObject("web");
71         dist = FileUtil.createFolder(projdir,"dist");
72         build = FileUtil.createFolder(projdir,"build");
73         Project p = ProjectManager.getDefault().findProject(projdir);
74         assertTrue("Invalid project type",p instanceof WebProject);
75         pp = (WebProject) p;
76         helper = pp.getAntProjectHelper();
77     }
78
79     protected void tearDown() throws Exception JavaDoc {
80         scratch = null;
81         projdir = null;
82         sources = null;
83         tests = null;
84         docRoot = null;
85         pp = null;
86         helper = null;
87         TestUtil.setLookup(Lookup.EMPTY);
88         super.tearDown();
89     }
90
91     public void testSharability () throws Exception JavaDoc {
92         File JavaDoc f = FileUtil.toFile (this.sources);
93         int res = SharabilityQuery.getSharability(f);
94         assertEquals("Sources must be sharable", SharabilityQuery.SHARABLE, res);
95         f = FileUtil.toFile (this.tests);
96         res = SharabilityQuery.getSharability(f);
97         assertEquals("Tests must be sharable", SharabilityQuery.SHARABLE, res);
98         f = FileUtil.toFile (this.docRoot);
99         res = SharabilityQuery.getSharability(f);
100         assertEquals("Web Pages must be sharable", SharabilityQuery.SHARABLE, res);
101         f = FileUtil.toFile (this.build);
102         res = SharabilityQuery.getSharability(f);
103         assertEquals("Build can't be sharable", SharabilityQuery.NOT_SHARABLE, res);
104         f = FileUtil.toFile (this.dist);
105         res = SharabilityQuery.getSharability(f);
106         assertEquals("Dist can't be sharable", SharabilityQuery.NOT_SHARABLE, res);
107         FileObject newSourceRoot = addSourceRoot(helper, projdir, "src2.dir",new File JavaDoc(FileUtil.toFile(scratch),"sources2"));
108         ProjectUtils.getSources(pp).getSourceGroups(Sources.TYPE_GENERIC);
109         f = FileUtil.toFile (newSourceRoot);
110         res = SharabilityQuery.getSharability(f);
111         assertEquals("Sources2 must be sharable", SharabilityQuery.SHARABLE, res);
112         FileObject newSourceRoot2 = changeSourceRoot(helper, projdir, "src2.dir", new File JavaDoc(FileUtil.toFile(scratch),"sources3"));
113         f = FileUtil.toFile (newSourceRoot2);
114         res = SharabilityQuery.getSharability(f);
115         assertEquals("Sources3 must be sharable", SharabilityQuery.SHARABLE, res);
116         f = FileUtil.toFile (newSourceRoot);
117         res = SharabilityQuery.getSharability(f);
118         assertEquals("Sources2 must be unknown", SharabilityQuery.UNKNOWN, res);
119     }
120
121     public static FileObject addSourceRoot (AntProjectHelper helper, FileObject projdir,
122                                             String JavaDoc propName, File JavaDoc folder) throws Exception JavaDoc {
123         if (!folder.exists()) {
124             folder.mkdirs();
125         }
126         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
127         NodeList JavaDoc nl = data.getElementsByTagNameNS (WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots");
128         assert nl.getLength() == 1;
129         Element JavaDoc roots = (Element JavaDoc) nl.item(0);
130         Document JavaDoc doc = roots.getOwnerDocument();
131         Element JavaDoc root = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"root");
132         root.setAttributeNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE,"id",propName);
133         roots.appendChild (root);
134         helper.putPrimaryConfigurationData (data,true);
135         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
136         props.put (propName,folder.getAbsolutePath());
137         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
138         return FileUtil.toFileObject(folder);
139     }
140
141     public static FileObject changeSourceRoot (AntProjectHelper helper, FileObject projdir,
142                                                String JavaDoc propName, File JavaDoc folder) throws Exception JavaDoc {
143         if (!folder.exists()) {
144             folder.mkdirs();
145         }
146         EditableProperties props = helper.getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
147         assert props.containsKey(propName);
148         props.put (propName,folder.getAbsolutePath());
149         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,props);
150         return FileUtil.toFileObject(folder);
151     }
152
153 }
154
Popular Tags