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 2002 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.modules.settings.convertors; |
43 |
|
44 |
import java.io.*; |
45 |
|
46 |
import org.netbeans.api.settings.ConvertAsJavaBean; |
47 |
import org.netbeans.junit.NbTestCase; |
48 |
|
49 |
|
50 |
|
51 |
import org.openide.cookies.InstanceCookie; |
52 |
import org.openide.filesystems.FileObject; |
53 |
import org.openide.filesystems.FileUtil; |
54 |
import org.openide.loaders.DataFolder; |
55 |
import org.openide.loaders.DataObject; |
56 |
import org.openide.loaders.InstanceDataObject; |
57 |
import org.openide.modules.ModuleInfo; |
58 |
import org.openide.util.Lookup; |
59 |
import org.openide.util.test.AnnotationProcessorTestUtils; |
60 |
|
61 |
/** Checks usage of annotation to assign XML properties convertor. |
62 |
* |
63 |
* @author Jaroslav Tulach |
64 |
*/ |
65 |
public final class ConvertAsBeanTest extends NbTestCase { |
66 |
/** Creates a new instance of XMLPropertiesConvertorTest */ |
67 |
public ConvertAsBeanTest(String name) { |
68 |
super(name); |
69 |
} |
70 |
|
71 |
@Override |
72 |
protected void setUp() throws Exception { |
73 |
clearWorkDir(); |
74 |
Lookup.getDefault().lookup(ModuleInfo.class); |
75 |
} |
76 |
|
77 |
public void testReadWrite() throws Exception { |
78 |
AnnoFoo foo = new AnnoFoo(); |
79 |
foo.setName("xxx"); |
80 |
|
81 |
DataFolder test = DataFolder.findFolder(FileUtil.getConfigRoot()); |
82 |
DataObject obj = InstanceDataObject.create(test, null, foo, null); |
83 |
final FileObject pf = obj.getPrimaryFile(); |
84 |
final String content = pf.asText(); |
85 |
if (content.indexOf("<string>xxx</string>") == -1) { |
86 |
fail(content); |
87 |
} |
88 |
obj.setValid(false); |
89 |
DataObject newObj = DataObject.find(pf); |
90 |
if (newObj == obj) { |
91 |
fail("Strange, objects shall differ"); |
92 |
} |
93 |
InstanceCookie ic = newObj.getLookup().lookup(InstanceCookie.class); |
94 |
assertNotNull("Instance cookie found", ic); |
95 |
|
96 |
Object read = ic.instanceCreate(); |
97 |
assertNotNull("Instance created", read); |
98 |
assertEquals("Correct class", AnnoFoo.class, read.getClass()); |
99 |
AnnoFoo readFoo = (AnnoFoo)read; |
100 |
assertEquals("property changed", "xxx", readFoo.getName()); |
101 |
} |
102 |
|
103 |
public void testReadWriteOnSubclass() throws Exception { |
104 |
HooFoo foo = new HooFoo(); |
105 |
foo.setName("xxx"); |
106 |
|
107 |
DataFolder test = DataFolder.findFolder(FileUtil.getConfigRoot()); |
108 |
DataObject obj = InstanceDataObject.create(test, null, foo, null); |
109 |
final FileObject pf = obj.getPrimaryFile(); |
110 |
final String content = pf.asText(); |
111 |
if (content.indexOf("<string>xxx</string>") == -1) { |
112 |
fail(content); |
113 |
} |
114 |
obj.setValid(false); |
115 |
DataObject newObj = DataObject.find(pf); |
116 |
if (newObj == obj) { |
117 |
fail("Strange, objects shall differ"); |
118 |
} |
119 |
InstanceCookie ic = newObj.getLookup().lookup(InstanceCookie.class); |
120 |
assertNotNull("Instance cookie found", ic); |
121 |
|
122 |
Object read = ic.instanceCreate(); |
123 |
assertNotNull("Instance created", read); |
124 |
assertEquals("Correct class", HooFoo.class, read.getClass()); |
125 |
HooFoo readFoo = (HooFoo)read; |
126 |
assertEquals("property changed", "xxx", readFoo.getName()); |
127 |
} |
128 |
|
129 |
@ConvertAsJavaBean( |
130 |
) |
131 |
public static class AnnoFoo extends Object { |
132 |
private String name; |
133 |
|
134 |
public String getName() { |
135 |
return name; |
136 |
} |
137 |
|
138 |
public void setName(String name) { |
139 |
this.name = name; |
140 |
} |
141 |
} // end of AnnoFoo |
142 |
|
143 |
public static class HooFoo extends AnnoFoo { |
144 |
private int count; |
145 |
|
146 |
public int getCount() { |
147 |
return count; |
148 |
} |
149 |
|
150 |
public void setCount(int count) { |
151 |
this.count = count; |
152 |
} |
153 |
} // end of HooFoo |
154 |
|
155 |
@ConvertAsJavaBean( |
156 |
subclasses=false |
157 |
) |
158 |
public static class JuuFoo extends Object { |
159 |
private String name; |
160 |
|
161 |
public String getName() { |
162 |
return name; |
163 |
} |
164 |
|
165 |
public void setName(String name) { |
166 |
this.name = name; |
167 |
} |
168 |
} // end of JuuFoo |
169 |
|
170 |
public static class NotFoo extends JuuFoo { |
171 |
private int count; |
172 |
|
173 |
public int getCount() { |
174 |
return count; |
175 |
} |
176 |
|
177 |
public void setCount(int count) { |
178 |
this.count = count; |
179 |
} |
180 |
} // end of NotFoo |
181 |
public void testReadWriteNotForSubclasses() throws Exception { |
182 |
NotFoo foo = new NotFoo(); |
183 |
foo.setName("xxx"); |
184 |
|
185 |
DataFolder test = DataFolder.findFolder(FileUtil.getConfigRoot()); |
186 |
try { |
187 |
DataObject obj = InstanceDataObject.create(test, null, foo, null); |
188 |
final FileObject pf = obj.getPrimaryFile(); |
189 |
final String content = pf.asText(); |
190 |
fail(content); |
191 |
} catch (NotSerializableException ex) { |
192 |
// OK |
193 |
} |
194 |
} |
195 |
|
196 |
public void testVerifyHaveDefaultConstructor() throws Exception { |
197 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.y.Kuk", |
198 |
"import org.netbeans.api.settings.ConvertAsJavaBean;\n" + |
199 |
"@ConvertAsJavaBean()\n" + |
200 |
"public class Kuk {\n" + |
201 |
" public Kuk(int i) {}\n" + |
202 |
"}\n" |
203 |
); |
204 |
ByteArrayOutputStream err = new ByteArrayOutputStream(); |
205 |
boolean res = AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, err); |
206 |
assertFalse("Should fail", res); |
207 |
if (err.toString().indexOf("x.y.Kuk must have a no-argument constructor") == -1) { |
208 |
fail("Wrong error message:\n" + err.toString()); |
209 |
} |
210 |
} |
211 |
public void testInterfacesCannotBeAnnotated() throws Exception { |
212 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.y.Kuk", |
213 |
"import org.netbeans.api.settings.ConvertAsJavaBean;\n" + |
214 |
"@ConvertAsJavaBean()\n" + |
215 |
"public interface Kuk {\n" + |
216 |
" public int buk(int i);\n" + |
217 |
"}\n" |
218 |
); |
219 |
ByteArrayOutputStream err = new ByteArrayOutputStream(); |
220 |
boolean res = AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, err); |
221 |
assertFalse("Should fail", res); |
222 |
if (err.toString().indexOf("is not loadable") == -1) { |
223 |
fail("Wrong error message:\n" + err.toString()); |
224 |
} |
225 |
} |
226 |
} |