Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
|
42 |
package org.netbeans.spi.project.support.ant; |
43 |
|
44 |
import java.io.ByteArrayOutputStream; |
45 |
import java.io.IOException; |
46 |
import java.util.Collection; |
47 |
import java.util.Iterator; |
48 |
import org.netbeans.api.project.Project; |
49 |
import org.netbeans.api.project.ProjectManager; |
50 |
import org.netbeans.api.project.TestUtil; |
51 |
import org.netbeans.junit.NbTestCase; |
52 |
import org.openide.filesystems.FileObject; |
53 |
import org.openide.util.Lookup; |
54 |
import org.openide.util.lookup.Lookups; |
55 |
import org.openide.util.test.AnnotationProcessorTestUtils; |
56 |
import org.openide.util.test.MockLookup; |
57 |
|
58 |
/** |
59 |
* That that a project registered by annotation is really found. |
60 |
* @author Jaroslav Tulach |
61 |
*/ |
62 |
public class AntBasedProjectRegistrationTest extends NbTestCase { |
63 |
|
64 |
public AntBasedProjectRegistrationTest(String name) { |
65 |
super(name); |
66 |
} |
67 |
|
68 |
private FileObject scratch; |
69 |
private FileObject projdir; |
70 |
private ProjectManager pm; |
71 |
|
72 |
protected @Override void setUp() throws Exception { |
73 |
super.setUp(); |
74 |
MockLookup.init(); |
75 |
assertEquals("No factory has been used yet", 0, AnnotatedProject.factoryCalls); |
76 |
Collection<? extends AntBasedProjectType> all = Lookups.forPath("Services/AntBasedProjectTypes").lookupAll(AntBasedProjectType.class); |
77 |
assertEquals("Two found", 2, all.size()); |
78 |
Iterator<? extends AntBasedProjectType> it = all.iterator(); |
79 |
if ("testFactory".equals(getName())) { |
80 |
it.next(); |
81 |
} |
82 |
AntBasedProjectType t = it.next(); |
83 |
MockLookup.setInstances(t); |
84 |
scratch = TestUtil.makeScratchDir(this); |
85 |
projdir = scratch.createFolder("proj"); |
86 |
ProjectGenerator.createProject(projdir, t.getType()); |
87 |
pm = ProjectManager.getDefault(); |
88 |
} |
89 |
|
90 |
protected @Override void tearDown() throws Exception { |
91 |
scratch = null; |
92 |
projdir = null; |
93 |
pm = null; |
94 |
super.tearDown(); |
95 |
} |
96 |
|
97 |
public void testFindProject() throws Exception { |
98 |
Project p = pm.findProject(projdir); |
99 |
assertNotNull("Annotation project found", p.getLookup().lookup(AnnotatedProject.class)); |
100 |
} |
101 |
|
102 |
public void testFactory() throws Exception { |
103 |
Project p = pm.findProject(projdir); |
104 |
assertNotNull("Annotation project found", p.getLookup().lookup(AnnotatedProject.class)); |
105 |
assertEquals(1, AnnotatedProject.factoryCalls); |
106 |
AnnotatedProject.factoryCalls = 0; |
107 |
} |
108 |
|
109 |
public void testTypeNeedsToBeProject() throws Exception { |
110 |
String prj = |
111 |
"import org.netbeans.spi.project.support.ant.AntBasedProjectRegistration;" + |
112 |
"import org.netbeans.spi.project.support.ant.AntProjectHelper;" + |
113 |
"@AntBasedProjectRegistration(" + |
114 |
" iconResource=\"noicon\"," + |
115 |
" type=\"test\"," + |
116 |
" privateNamespace=\"urn:test:private\"," + |
117 |
" sharedNamespace=\"urn:test:shared\"" + |
118 |
")" + |
119 |
"public final class Prj extends Object {" + |
120 |
" public Prj(AntProjectHelper h) {" + |
121 |
" }" + |
122 |
"}" + |
123 |
""; |
124 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Prj", prj); |
125 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
126 |
boolean res = AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, out); |
127 |
assertFalse("Compilation failed", res); |
128 |
if (!out.toString().contains("extend Project")) { |
129 |
fail(out.toString()); |
130 |
} |
131 |
} |
132 |
|
133 |
public void testReturnTypeNeedsToBeProject() throws Exception { |
134 |
String prj = |
135 |
"import org.netbeans.spi.project.support.ant.AntBasedProjectRegistration;" + |
136 |
"import org.netbeans.spi.project.support.ant.AntProjectHelper;" + |
137 |
"" + |
138 |
"public final class Prj extends Object {" + |
139 |
" @AntBasedProjectRegistration(" + |
140 |
" iconResource=\"noicon\"," + |
141 |
" type=\"test\"," + |
142 |
" privateNamespace=\"urn:test:private\"," + |
143 |
" sharedNamespace=\"urn:test:shared\"" + |
144 |
" )" + |
145 |
" public static String factory(AntProjectHelper h) {" + |
146 |
" return null;" + |
147 |
" }" + |
148 |
"}" + |
149 |
""; |
150 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Prj", prj); |
151 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
152 |
boolean res = AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, out); |
153 |
assertFalse("Compilation failed", res); |
154 |
if (!out.toString().contains("return Project")) { |
155 |
fail(out.toString()); |
156 |
} |
157 |
} |
158 |
|
159 |
@AntBasedProjectRegistration( |
160 |
iconResource="noicon", |
161 |
type="test", |
162 |
privateNamespace="urn:test:private", |
163 |
sharedNamespace="urn:test:shared" |
164 |
) |
165 |
public static final class AnnotatedProject extends AntBasedTestUtil.TestAntBasedProject { |
166 |
static int factoryCalls; |
167 |
|
168 |
public AnnotatedProject(AntProjectHelper h) throws IOException { |
169 |
super(h, null); |
170 |
} |
171 |
|
172 |
@Override |
173 |
public Lookup getLookup() { |
174 |
return Lookups.singleton(this); |
175 |
} |
176 |
|
177 |
@AntBasedProjectRegistration( |
178 |
iconResource = "noicon", |
179 |
type = "test.factory", |
180 |
privateNamespace = "urn:test:private", |
181 |
sharedNamespace = "urn:test:shared" |
182 |
) |
183 |
public static Project factoryMethod(AntProjectHelper h) throws IOException { |
184 |
factoryCalls++; |
185 |
return new AnnotatedProject(h); |
186 |
} |
187 |
} |
188 |
|
189 |
} |