Lines 1-507
Link Here
|
1 |
/* |
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. |
31 |
* |
32 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
33 |
*/ |
34 |
|
35 |
package org.netbeans.modules.templates; |
36 |
|
37 |
import java.awt.Dialog; |
38 |
import java.io.BufferedReader; |
39 |
import java.io.IOException; |
40 |
import java.io.InputStream; |
41 |
import java.io.InputStreamReader; |
42 |
import java.io.OutputStream; |
43 |
import java.io.OutputStreamWriter; |
44 |
import java.nio.CharBuffer; |
45 |
import java.nio.charset.Charset; |
46 |
import java.util.Collections; |
47 |
import java.util.Enumeration; |
48 |
import java.util.HashMap; |
49 |
import java.util.Iterator; |
50 |
import java.util.Map; |
51 |
import java.util.logging.Level; |
52 |
import java.util.logging.Logger; |
53 |
import org.netbeans.junit.MockServices; |
54 |
import org.netbeans.junit.NbTestCase; |
55 |
import org.netbeans.spi.queries.FileEncodingQueryImplementation; |
56 |
import org.openide.DialogDescriptor; |
57 |
import org.openide.DialogDisplayer; |
58 |
import org.openide.NotifyDescriptor; |
59 |
import org.openide.filesystems.FileObject; |
60 |
import org.openide.filesystems.FileStateInvalidException; |
61 |
import org.openide.filesystems.FileSystem; |
62 |
import org.openide.filesystems.FileUtil; |
63 |
import org.openide.filesystems.LocalFileSystem; |
64 |
import org.openide.loaders.DataFolder; |
65 |
import org.openide.loaders.DataLoader; |
66 |
import org.openide.loaders.DataLoaderPool; |
67 |
import org.openide.loaders.DataObject; |
68 |
import org.openide.loaders.DataObjectExistsException; |
69 |
import org.openide.loaders.FileEntry; |
70 |
import org.openide.loaders.MultiDataObject; |
71 |
import org.openide.loaders.MultiFileLoader; |
72 |
import org.openide.util.Enumerations; |
73 |
|
74 |
/** |
75 |
* |
76 |
* @author Jaroslav Tulach |
77 |
*/ |
78 |
public class SCFTHandlerTest extends NbTestCase { |
79 |
static { |
80 |
// confuse the system a bit, if your system runs with UTF-8 default locale... |
81 |
//System.setProperty("file.encoding", "cp1252"); |
82 |
} |
83 |
|
84 |
public SCFTHandlerTest(String testName) { |
85 |
super(testName); |
86 |
} |
87 |
|
88 |
@Override |
89 |
protected boolean runInEQ() { |
90 |
return true; |
91 |
} |
92 |
|
93 |
@Override |
94 |
protected Level logLevel() { |
95 |
return Level.FINE; |
96 |
} |
97 |
|
98 |
@Override |
99 |
protected void setUp() throws Exception { |
100 |
clearWorkDir(); |
101 |
MockServices.setServices(DD.class, Pool.class, FEQI.class); |
102 |
} |
103 |
|
104 |
@Override |
105 |
protected void tearDown() throws Exception { |
106 |
super.tearDown(); |
107 |
} |
108 |
|
109 |
public void testCreateFromTemplateUsingFreemarker() throws Exception { |
110 |
FileObject root = FileUtil.createMemoryFileSystem().getRoot(); |
111 |
FileObject fo = FileUtil.createData(root, "simpleObject.txt"); |
112 |
OutputStream os = fo.getOutputStream(); |
113 |
String txt = "print('<html><h1>'); print(title); print('</h1></html>');"; |
114 |
os.write(txt.getBytes()); |
115 |
os.close(); |
116 |
fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
117 |
|
118 |
|
119 |
DataObject obj = DataObject.find(fo); |
120 |
|
121 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); |
122 |
|
123 |
Map<String,String> parameters = Collections.singletonMap("title", "Nazdar"); |
124 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
125 |
|
126 |
assertEquals("Created in right place", folder, n.getFolder()); |
127 |
assertEquals("Created with right name", "complex.txt", n.getName()); |
128 |
|
129 |
String exp = "<html><h1>Nazdar</h1></html>"; |
130 |
assertEquals(exp, readFile(n.getPrimaryFile())); |
131 |
|
132 |
} |
133 |
|
134 |
public void testCreateWithNameAndExt() throws Exception { |
135 |
FileObject root = FileUtil.createMemoryFileSystem().getRoot(); |
136 |
FileObject fo = FileUtil.createData(root, "simpleObject.txt"); |
137 |
OutputStream os = fo.getOutputStream(); |
138 |
String txt = "print('<html><h1>'); print(nameAndExt); print('</h1></html>')"; |
139 |
os.write(txt.getBytes()); |
140 |
os.close(); |
141 |
fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
142 |
|
143 |
|
144 |
DataObject obj = DataObject.find(fo); |
145 |
|
146 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); |
147 |
|
148 |
Map<String,String> parameters = Collections.emptyMap(); |
149 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
150 |
|
151 |
assertEquals("Created in right place", folder, n.getFolder()); |
152 |
assertEquals("Created with right name", "complex.txt", n.getName()); |
153 |
|
154 |
String exp = "<html><h1>complex.txt</h1></html>"; |
155 |
assertEquals(exp, readFile(n.getPrimaryFile())); |
156 |
|
157 |
} |
158 |
|
159 |
public void testCreateWithNameAndExtForForm() throws Exception { |
160 |
LocalFileSystem lfs = new LocalFileSystem(); |
161 |
lfs.setRootDirectory(getWorkDir()); |
162 |
FileObject root = lfs.getRoot(); |
163 |
FileObject fo = FileUtil.createData(root, "j.java"); |
164 |
OutputStream os = fo.getOutputStream(); |
165 |
String txt = "print('<html><h1>'); print(nameAndExt); print('</h1></html>')"; |
166 |
os.write(txt.getBytes()); |
167 |
os.close(); |
168 |
fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
169 |
|
170 |
FileObject fo2 = FileUtil.createData(root, "j.form"); |
171 |
OutputStream os2 = fo2.getOutputStream(); |
172 |
String txt2 = "print('<html><h2>'); print(nameAndExt); print('</h2></html>')"; |
173 |
os2.write(txt2.getBytes()); |
174 |
os2.close(); |
175 |
fo2.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
176 |
|
177 |
DataObject obj = DataObject.find(fo); |
178 |
assertEquals("Both files", 2, obj.files().size()); |
179 |
|
180 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); |
181 |
|
182 |
Map<String,String> parameters = Collections.emptyMap(); |
183 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
184 |
|
185 |
assertEquals("Two files", 2, n.files().size()); |
186 |
|
187 |
|
188 |
FileObject newForm = FileUtil.findBrother(n.getPrimaryFile(), "form"); |
189 |
|
190 |
assertEquals("Primary file is java", "java", n.getPrimaryFile().getExt()); |
191 |
|
192 |
assertNotNull("Form copied", newForm); |
193 |
DataObject frm = DataObject.find(newForm); |
194 |
assertSame("Form belongs to java", n, frm); |
195 |
|
196 |
assertEquals("Created in right place", folder, n.getFolder()); |
197 |
assertEquals("Created with right name", "complex", n.getName()); |
198 |
|
199 |
String exp = "<html><h1>complex.java</h1></html>"; |
200 |
assertEquals("Primary file" + n.getPrimaryFile(), exp, readFile(n.getPrimaryFile())); |
201 |
|
202 |
String exp2 = "<html><h2>complex.form</h2></html>"; |
203 |
assertEquals(exp2, readFile(newForm)); |
204 |
} |
205 |
|
206 |
public void testBasePropertiesAlwaysPresent() throws Exception { |
207 |
FileObject root = FileUtil.createMemoryFileSystem().getRoot(); |
208 |
FileObject fo = FileUtil.createData(root, "simpleObject.txt"); |
209 |
OutputStream os = fo.getOutputStream(); |
210 |
String txt = "print('<html><h1>'); print(name); print('</h1>');" + |
211 |
"print('<h2>'); print(date); print('</h2>');" + |
212 |
"print('<h3>'); print(time); print('</h3>');" + |
213 |
"print('<h4>'); print(user); print('</h4>');" + |
214 |
"print('<h4>'); print(dateTime.getTime()); print('</h4>');" + |
215 |
"print('</html>');"; |
216 |
os.write(txt.getBytes()); |
217 |
os.close(); |
218 |
fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
219 |
|
220 |
|
221 |
DataObject obj = DataObject.find(fo); |
222 |
|
223 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); |
224 |
|
225 |
Map<String,String> parameters = Collections.singletonMap("title", "Nazdar"); |
226 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
227 |
|
228 |
assertEquals("Created in right place", folder, n.getFolder()); |
229 |
assertEquals("Created with right name", "complex.txt", n.getName()); |
230 |
|
231 |
String res = readFile(n.getPrimaryFile()); |
232 |
|
233 |
if (res.indexOf("date") >= 0) fail(res); |
234 |
if (res.indexOf("time") >= 0) fail(res); |
235 |
if (res.indexOf("user") >= 0) fail(res); |
236 |
if (res.indexOf("name") >= 0) fail(res); |
237 |
if (res.indexOf("dateTime") >= 0) fail(res); |
238 |
} |
239 |
|
240 |
private static String readFile(FileObject fo) throws IOException { |
241 |
byte[] arr = new byte[(int)fo.getSize()]; |
242 |
int len = fo.getInputStream().read(arr); |
243 |
assertEquals("Fully read", arr.length, len); |
244 |
return new String(arr); |
245 |
} |
246 |
|
247 |
private static String readChars(FileObject fo, Charset set) throws IOException { |
248 |
CharBuffer arr = CharBuffer.allocate((int)fo.getSize() * 2); |
249 |
BufferedReader r = new BufferedReader(new InputStreamReader(fo.getInputStream(), set)); |
250 |
while (r.read(arr) != -1) { |
251 |
// again |
252 |
} |
253 |
r.close(); |
254 |
|
255 |
arr.flip(); |
256 |
return arr.toString(); |
257 |
} |
258 |
|
259 |
public void testUTF8() throws Exception { |
260 |
FileObject root = FileUtil.getConfigRoot(); |
261 |
FileObject xmldir = FileUtil.createFolder(root, "xml"); |
262 |
FileObject xml = FileUtil.createData(xmldir, "class.txt"); |
263 |
OutputStream os = xml.getOutputStream(); |
264 |
FileUtil.copy(getClass().getResourceAsStream("utf8.xml"), os); |
265 |
xml.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
266 |
os.close(); |
267 |
|
268 |
DataObject obj = DataObject.find(xml); |
269 |
|
270 |
|
271 |
FileObject target = FileUtil.createFolder(FileUtil.createMemoryFileSystem().getRoot(), "dir"); |
272 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(target, "target")); |
273 |
|
274 |
|
275 |
|
276 |
Charset set = Charset.forName("iso-8859-2"); |
277 |
FEQI.fs = target.getFileSystem(); |
278 |
FEQI.result = set; |
279 |
|
280 |
|
281 |
Map<String,String> parameters = Collections.singletonMap("title", "Nazdar"); |
282 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
283 |
|
284 |
assertEquals("Created in right place", folder, n.getFolder()); |
285 |
assertEquals("Created with right name", "complex.txt", n.getName()); |
286 |
|
287 |
|
288 |
String read = readChars(n.getPrimaryFile(), set).replaceAll("println\\('", "").replaceAll("'\\);", ""); |
289 |
String exp = readChars(xml, Charset.forName("utf-8")).replaceAll("println\\('", "").replaceAll("'\\);", ""); |
290 |
assertEquals(exp, read); |
291 |
|
292 |
} |
293 |
|
294 |
public void testTemplateWizardCopiesItsPropertiesToMapForOverridenEntryOnMoreEntries() throws Exception { |
295 |
LocalFileSystem fs = new LocalFileSystem(); |
296 |
fs.setRootDirectory(getWorkDir()); |
297 |
|
298 |
FileObject root = fs.getRoot(); |
299 |
FileObject fo = FileUtil.createData(root, "simpleObject.java"); |
300 |
FileObject fo2 = FileUtil.createData(root, "simpleObject.form"); |
301 |
fo.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
302 |
fo2.setAttribute(ScriptingCreateFromTemplateHandler.SCRIPT_ENGINE_ATTR, "js"); |
303 |
|
304 |
Charset set = Charset.forName("iso-8859-2"); |
305 |
OutputStream os = fo2.getOutputStream(); |
306 |
OutputStreamWriter w = new OutputStreamWriter(os, set); |
307 |
String txt = "print('skvělej tým, co nikdy neusíná - ěščřžýáíéúů')"; |
308 |
w.write(txt); |
309 |
w.close(); |
310 |
|
311 |
|
312 |
DataObject obj = DataObject.find(fo); |
313 |
assertEquals(TwoPartObject.class, obj.getClass()); |
314 |
TwoPartObject tpo = (TwoPartObject)obj; |
315 |
tpo.encoding = set; |
316 |
|
317 |
FileObject root2 = FileUtil.createMemoryFileSystem().getRoot(); |
318 |
DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root2, "target")); |
319 |
|
320 |
Map<String,String> parameters = Collections.singletonMap("type", "empty"); |
321 |
|
322 |
FEQI.fs = root2.getFileSystem(); |
323 |
FEQI.result = Charset.forName("UTF-8"); |
324 |
|
325 |
DataObject n = obj.createFromTemplate(folder, "complex", parameters); |
326 |
Integer cnt = TwoPartLoader.queried.get(n.getPrimaryFile()); |
327 |
assertEquals("No query", null, cnt); |
328 |
|
329 |
assertEquals("Created in right place", folder, n.getFolder()); |
330 |
assertEquals("Created with right name", "complex", n.getName()); |
331 |
Iterator<FileObject> it = n.files().iterator(); |
332 |
it.next(); |
333 |
FileObject snd = it.next(); |
334 |
|
335 |
long length = snd.getSize(); |
336 |
if (length <= 0) { |
337 |
fail("Too small file: " + length + " for " + snd); |
338 |
} |
339 |
InputStream is = snd.getInputStream(); |
340 |
InputStreamReader r = new InputStreamReader(is, "UTF-8"); |
341 |
char[] cbuf = new char[1024]; |
342 |
int len = r.read(cbuf); |
343 |
if (len == -1) { |
344 |
fail("no input stream for " + snd); |
345 |
} |
346 |
String read = new String(cbuf, 0, len); |
347 |
txt = txt.replaceAll("print\\('", "").replaceAll("'\\)", ""); |
348 |
|
349 |
assertEquals(txt, read); |
350 |
} |
351 |
|
352 |
public static final class DD extends DialogDisplayer { |
353 |
public Object notify(NotifyDescriptor descriptor) { |
354 |
throw new UnsupportedOperationException("Not supported yet."); |
355 |
} |
356 |
|
357 |
public Dialog createDialog(final DialogDescriptor descriptor) { |
358 |
throw new UnsupportedOperationException("Not supported yet."); |
359 |
/* |
360 |
return new JDialog() { |
361 |
@Deprecated |
362 |
public void show() { |
363 |
for (Object object : descriptor.getOptions()) { |
364 |
if (object instanceof JButton) { |
365 |
JButton b = (JButton)object; |
366 |
if (b.getText().equals("Finish")) { |
367 |
descriptor.setValue(WizardDescriptor.FINISH_OPTION); |
368 |
b.doClick(); |
369 |
return; |
370 |
} |
371 |
} |
372 |
} |
373 |
fail("Cannot find Finish button: " + Arrays.asList(descriptor.getOptions())); |
374 |
} |
375 |
}; |
376 |
*/ |
377 |
} |
378 |
} |
379 |
|
380 |
public static final class FEQI extends FileEncodingQueryImplementation { |
381 |
public static FileSystem fs; |
382 |
public static Charset result; |
383 |
|
384 |
public Charset getEncoding(FileObject f) { |
385 |
try { |
386 |
if (f.getFileSystem() == fs) { |
387 |
return result; |
388 |
} |
389 |
return null; |
390 |
} catch (FileStateInvalidException ex) { |
391 |
return null; |
392 |
} |
393 |
} |
394 |
} |
395 |
|
396 |
public static final class Pool extends DataLoaderPool { |
397 |
protected Enumeration<DataLoader> loaders() { |
398 |
return Enumerations.<DataLoader>array(new DataLoader[] { |
399 |
TwoPartLoader.getLoader(TwoPartLoader.class), |
400 |
SimpleLoader.getLoader(SimpleLoader.class), |
401 |
}); |
402 |
} |
403 |
} |
404 |
|
405 |
public static final class SimpleLoader extends MultiFileLoader { |
406 |
public SimpleLoader() { |
407 |
super(SimpleObject.class.getName()); |
408 |
} |
409 |
protected String displayName() { |
410 |
return "SimpleLoader"; |
411 |
} |
412 |
protected FileObject findPrimaryFile(FileObject fo) { |
413 |
if (fo.hasExt("prima")) { |
414 |
return fo; |
415 |
} |
416 |
return null; |
417 |
} |
418 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
419 |
return new SimpleObject(this, primaryFile); |
420 |
} |
421 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
422 |
return new FE(obj, primaryFile); |
423 |
} |
424 |
protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { |
425 |
return new FileEntry(obj, secondaryFile); |
426 |
} |
427 |
} |
428 |
|
429 |
private static final class FE extends FileEntry { |
430 |
public FE(MultiDataObject mo, FileObject fo) { |
431 |
super(mo, fo); |
432 |
} |
433 |
|
434 |
@Override |
435 |
public FileObject createFromTemplate(FileObject f, String name) throws IOException { |
436 |
fail("I do not want to be called"); |
437 |
return null; |
438 |
} |
439 |
|
440 |
|
441 |
|
442 |
} |
443 |
|
444 |
public static final class SimpleObject extends MultiDataObject { |
445 |
public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { |
446 |
super(fo, l); |
447 |
} |
448 |
|
449 |
@Override |
450 |
public String getName() { |
451 |
return getPrimaryFile().getNameExt(); |
452 |
} |
453 |
} |
454 |
|
455 |
static final Logger LOG = Logger.getLogger("tst.TwoPartLoader"); |
456 |
public static final class TwoPartLoader extends MultiFileLoader { |
457 |
static Map<FileObject,Integer> queried = new HashMap<FileObject,Integer>(); |
458 |
|
459 |
public TwoPartLoader() { |
460 |
super(TwoPartObject.class.getName ()); |
461 |
} |
462 |
protected String displayName() { |
463 |
return "TwoPart"; |
464 |
} |
465 |
protected FileObject findPrimaryFile(FileObject fo) { |
466 |
Integer i = queried.get(fo); |
467 |
queried.put(fo, i == null ? 1 : i + 1); |
468 |
FileObject ret; |
469 |
|
470 |
if (fo.hasExt("java") || fo.hasExt("form")) { |
471 |
ret = org.openide.filesystems.FileUtil.findBrother(fo, "java"); |
472 |
} else { |
473 |
ret = null; |
474 |
} |
475 |
|
476 |
LOG.fine("findPrimaryFile for " + fo + " yeilded " + ret); |
477 |
return ret; |
478 |
} |
479 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
480 |
LOG.info("New data object for " + primaryFile); |
481 |
return new TwoPartObject(this, primaryFile); |
482 |
} |
483 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
484 |
LOG.fine("new primary entry " + primaryFile); |
485 |
return new FE(obj, primaryFile); |
486 |
} |
487 |
protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { |
488 |
LOG.fine("new snd entry: " + secondaryFile); |
489 |
return new FE(obj, secondaryFile); |
490 |
} |
491 |
} |
492 |
public static final class TwoPartObject extends MultiDataObject { |
493 |
public TwoPartObject(TwoPartLoader l, FileObject folder) throws DataObjectExistsException { |
494 |
super(folder, l); |
495 |
getCookieSet().assign(FileEncodingQueryImplementation.class, eq); |
496 |
} |
497 |
private Charset encoding; |
498 |
private FileEncodingQueryImplementation eq = new FileEncodingQueryImplementation() { |
499 |
|
500 |
public Charset getEncoding(FileObject file) { |
501 |
return encoding; |
502 |
} |
503 |
|
504 |
}; |
505 |
} |
506 |
|
507 |
} |