KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > java > project > classpath > support > ProjectClassPathImplementationTest


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.spi.java.project.classpath.support;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.api.java.classpath.ClassPath;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.spi.java.classpath.ClassPathFactory;
30 import org.netbeans.spi.java.classpath.ClassPathImplementation;
31 import org.netbeans.spi.project.support.ant.AntBasedTestUtil;
32 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
33 import org.openide.filesystems.FileObject;
34 import org.netbeans.api.project.TestUtil;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.spi.project.support.ant.EditableProperties;
37 import org.netbeans.spi.project.support.ant.ProjectGenerator;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.util.Lookup;
40
41 /**
42  * Tests for {@link ProjectClassPathImplementation}.
43  * @author Tomas Zezula
44  */

45 public class ProjectClassPathImplementationTest extends NbTestCase {
46     
47     private static final String JavaDoc PROP_NAME_1 = "classpath1"; //NOI18N
48
private static final String JavaDoc PROP_NAME_2 = "classpath2"; //NOI18N
49

50     public ProjectClassPathImplementationTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     private FileObject scratch;
55     private FileObject projdir;
56     private FileObject[] cpRoots1;
57     private FileObject[] cpRoots2;
58     private AntProjectHelper helper;
59     private PropertyEvaluator evaluator;
60     
61     protected void setUp() throws Exception JavaDoc {
62         super.setUp();
63         TestUtil.setLookup(new Object JavaDoc[] {
64             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
65             AntBasedTestUtil.testAntBasedProjectType(),
66         });
67     }
68
69     protected void tearDown() throws Exception JavaDoc {
70         scratch = null;
71         projdir = null;
72         cpRoots1 = null;
73         cpRoots2 = null;
74         helper = null;
75         evaluator = null;
76         TestUtil.setLookup(Lookup.EMPTY);
77         super.tearDown();
78     }
79     
80     
81     private void prepareProject () throws IOException JavaDoc {
82         scratch = TestUtil.makeScratchDir(this);
83         projdir = scratch.createFolder("proj"); //NOI18N
84
cpRoots1 = new FileObject[2];
85         cpRoots1[0] = scratch.createFolder("cpRoot1"); //NOI18N
86
cpRoots1[1] = scratch.createFolder("cpRoot2"); //NOI18N
87
cpRoots2 = new FileObject[2];
88         cpRoots2[0] = scratch.createFolder("cpRoot3"); //NOI18N
89
cpRoots2[1] = scratch.createFolder("cpRoot4"); //NOI18N
90
helper = ProjectGenerator.createProject(projdir, "test"); //NOI18N
91
evaluator = helper.getStandardPropertyEvaluator();
92         setClassPath(new String JavaDoc[] {PROP_NAME_1, PROP_NAME_2}, new FileObject[][] {cpRoots1, cpRoots2});
93     }
94     
95     public void testBootClassPathImplementation () throws Exception JavaDoc {
96         prepareProject();
97         ClassPathImplementation cpImpl = ProjectClassPathSupport.createPropertyBasedClassPathImplementation(
98                 FileUtil.toFile(helper.getProjectDirectory()), evaluator, new String JavaDoc[] {PROP_NAME_1, PROP_NAME_2});
99         ClassPath cp = ClassPathFactory.createClassPath(cpImpl);
100         FileObject[] fo = cp.getRoots();
101         List JavaDoc<FileObject> expected = new ArrayList JavaDoc<FileObject>();
102         expected.addAll(Arrays.asList(cpRoots1));
103         expected.addAll(Arrays.asList(cpRoots2));
104         assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); //NOI18N
105
cpRoots1 = new FileObject[] {cpRoots1[0]};
106         setClassPath(new String JavaDoc[] {PROP_NAME_1}, new FileObject[][]{cpRoots1});
107         fo = cp.getRoots();
108         expected = new ArrayList JavaDoc<FileObject>();
109         expected.addAll(Arrays.asList(cpRoots1));
110         expected.addAll(Arrays.asList(cpRoots2));
111         assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); //NOI18N
112
cpRoots2 = new FileObject[] {cpRoots2[0]};
113         setClassPath(new String JavaDoc[] {PROP_NAME_2}, new FileObject[][]{cpRoots2});
114         fo = cp.getRoots();
115         expected = new ArrayList JavaDoc<FileObject>();
116         expected.addAll(Arrays.asList(cpRoots1));
117         expected.addAll(Arrays.asList(cpRoots2));
118         assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); //NOI18N
119
}
120     
121     // XXX should test that changes are actually fired when appropriate
122

123     private void setClassPath (String JavaDoc[] propNames, FileObject[][] cpRoots) {
124         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
125         for (int i=0; i< propNames.length; i++) {
126             props.setProperty (propNames[i],toPath(cpRoots[i]));
127         }
128         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
129     }
130     
131     
132     private static String JavaDoc toPath (FileObject[] cpRoots) {
133         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
134         for (int i=0; i<cpRoots.length; i++) {
135             if (i>0) {
136                 result.append(':'); //NOI18N
137
}
138             File JavaDoc f = FileUtil.toFile (cpRoots[i]);
139             result.append (f.getAbsolutePath());
140         }
141         return result.toString();
142     }
143     
144 }
145
Popular Tags