001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc.
028: *
029: * Portions Copyrighted 2007 Sun Microsystems, Inc.
030: */
031: package org.netbeans.api.templates;
032:
033: import freemarker.ext.beans.BeansWrapper;
034: import java.awt.Color;
035: import java.awt.Panel;
036: import java.io.InputStreamReader;
037: import java.io.OutputStream;
038: import java.io.StringWriter;
039: import java.io.Writer;
040: import java.nio.charset.Charset;
041: import java.util.Collections;
042: import java.util.HashMap;
043: import java.util.Map;
044: import javax.script.ScriptContext;
045: import javax.script.ScriptEngine;
046: import javax.script.ScriptEngineManager;
047: import junit.framework.Test;
048: import junit.framework.TestCase;
049: import org.netbeans.junit.MockServices;
050: import org.netbeans.junit.NbTestSuite;
051: import org.netbeans.spi.queries.FileEncodingQueryImplementation;
052: import org.openide.filesystems.FileObject;
053: import org.openide.filesystems.FileUtil;
054: import org.openide.filesystems.Repository;
055:
056: /**
057: *
058: * @author Jaroslav Tulach
059: */
060: public class ProcessorTest extends TestCase {
061: FileObject root;
062:
063: public ProcessorTest(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() throws Exception {
068: return new NbTestSuite(ProcessorTest.class);
069: //return new ProcessorTest("testCanImportSubpkgOfParentPkg");
070: }
071:
072: protected void setUp() throws Exception {
073: root = Repository.getDefault().getDefaultFileSystem().getRoot();
074: for (FileObject f : root.getChildren()) {
075: f.delete();
076: }
077: }
078:
079: protected void tearDown() throws Exception {
080: }
081:
082: public void testApply() throws Exception {
083: FileObject template = FileUtil.createData(root, "some.txt");
084: OutputStream os = template.getOutputStream();
085: String txt = "<html><h1>${title}</h1></html>";
086: os.write(txt.getBytes());
087: os.close();
088: template.setAttribute("title", "Nazdar");
089:
090: StringWriter w = new StringWriter();
091:
092: apply(template, w);
093:
094: String exp = "<html><h1>Nazdar</h1></html>";
095: assertEquals(exp, w.toString());
096: }
097:
098: public void testCanHandleComplexData() throws Exception {
099: Panel p = new Panel();
100: p.setForeground(Color.BLUE);
101:
102: FileObject template = FileUtil.createData(root, "some.txt");
103: OutputStream os = template.getOutputStream();
104: String txt = "<html><h1>${panel.foreground.red} ${panel.foreground.green} ${panel.foreground.blue}</h1></html>";
105: os.write(txt.getBytes());
106: os.close();
107: template.setAttribute("panel", p);
108:
109: StringWriter w = new StringWriter();
110:
111: apply(template, w);
112:
113: String exp = "<html><h1>0 0 255</h1></html>";
114: assertEquals(exp, w.toString());
115: }
116:
117: public void testCanImportSubpkgOfParentPkg() throws Exception {
118: FileObject imp = FileUtil.createData(root,
119: "Templates/Licenses/gpl.txt");
120: {
121: OutputStream os = imp.getOutputStream();
122: String txt = "GPL";
123: os.write(txt.getBytes());
124: os.close();
125: }
126:
127: FileObject template = FileUtil.createData(root,
128: "Templates/Others/some.txt");
129: {
130: OutputStream os = template.getOutputStream();
131: String txt = "<html><h1><#include \"*/Licenses/gpl.txt\"></h1></html>";
132: os.write(txt.getBytes());
133: os.close();
134: }
135: StringWriter w = new StringWriter();
136:
137: apply(template, w);
138:
139: String exp = "<html><h1>GPL</h1></html>";
140: assertEquals(exp, w.toString());
141: }
142:
143: public void testCanImportRelative() throws Exception {
144: FileObject imp = FileUtil.createData(root,
145: "Templates/Licenses/gpl.txt");
146: {
147: OutputStream os = imp.getOutputStream();
148: String txt = "GPL";
149: os.write(txt.getBytes());
150: os.close();
151: }
152:
153: FileObject template = FileUtil.createData(root,
154: "Templates/Others/some.txt");
155: {
156: OutputStream os = template.getOutputStream();
157: String txt = "<html><h1><#include \"../Licenses/gpl.txt\"></h1></html>";
158: os.write(txt.getBytes());
159: os.close();
160: }
161: StringWriter w = new StringWriter();
162:
163: apply(template, w);
164:
165: String exp = "<html><h1>GPL</h1></html>";
166: assertEquals(exp, w.toString());
167: }
168:
169: public void testImportHonorsFEQ() throws Exception {
170: doImportHonorsFEQ("cp1250");
171: }
172:
173: public void testImportHonorsFEQIso() throws Exception {
174: doImportHonorsFEQ("iso-8859-2");
175: }
176:
177: private void doImportHonorsFEQ(String enc) throws Exception {
178: MockServices.setServices(FEQI.class);
179:
180: //String kun = "ŽluÅ¥ouÄ?ký kůň";
181: String kun = "\u017Dlu\u0165ou\u010Dky k\u016F\u0148";
182:
183: FileObject imp = FileUtil.createData(root,
184: "Templates/Licenses/gpl.txt");
185: {
186: OutputStream os = imp.getOutputStream();
187: os.write(kun.getBytes(enc));
188: os.close();
189: imp.setAttribute("enc", enc);
190: }
191:
192: FileObject template = FileUtil.createData(root,
193: "Templates/Others/some.txt");
194: {
195: OutputStream os = template.getOutputStream();
196: String txt = "<html><h1><#include \"../Licenses/gpl.txt\"></h1></html>";
197: os.write(txt.getBytes("utf-8"));
198: os.close();
199: }
200: StringWriter w = new StringWriter();
201:
202: apply(template, w);
203:
204: String exp = "<html><h1>" + kun + "</h1></html>";
205: assertEquals(exp, w.toString());
206: }
207:
208: public void testMissingVariablesAreJustLogged() throws Exception {
209: FileObject template = FileUtil.createData(root,
210: "Templates/Others/some.txt");
211: {
212: OutputStream os = template.getOutputStream();
213: String txt = "<html><h1>${title}</h1></html>";
214: os.write(txt.getBytes());
215: os.close();
216: }
217: StringWriter w = new StringWriter();
218:
219: apply(template, w);
220:
221: if (!w.toString().matches("<html><h1>.*</h1></html>")) {
222: fail("should survive the missing variable:\n"
223: + w.toString());
224: }
225: if (w.toString().indexOf("title") == -1) {
226: fail("There should be a note about title variable:\n" + w);
227: }
228: }
229:
230: public void testMissingImportsAreJustLogged() throws Exception {
231: FileObject template = FileUtil.createData(root,
232: "Templates/Others/some.txt");
233: {
234: OutputStream os = template.getOutputStream();
235: String txt = "<html><h1><#include \"*/Licenses/gpl.txt\"></h1></html>";
236: os.write(txt.getBytes());
237: os.close();
238: }
239: StringWriter w = new StringWriter();
240:
241: apply(template, w);
242:
243: if (!w.toString().matches("<html><h1>.*</h1></html>")) {
244: fail("should survive the missing variable:\n"
245: + w.toString());
246: }
247: if (w.toString().indexOf("gpl.txt") == -1) {
248: fail("There should be a note about gpl include:\n" + w);
249: }
250: }
251:
252: public void testCanHandleImport() throws Exception {
253: Panel p = new Panel();
254: p.setForeground(Color.BLUE);
255:
256: FileObject imp = FileUtil.createData(root, "import.txt");
257: {
258: OutputStream os = imp.getOutputStream();
259: String txt = "${panel.foreground.blue}";
260: os.write(txt.getBytes());
261: os.close();
262: }
263:
264: FileObject template = FileUtil.createData(root, "some.txt");
265: {
266: OutputStream os = template.getOutputStream();
267: String txt = "<html><h1><#include \"import.txt\"></h1></html>";
268: os.write(txt.getBytes());
269: os.close();
270: template.setAttribute("panel", p);
271: }
272: StringWriter w = new StringWriter();
273:
274: apply(template, w);
275:
276: String exp = "<html><h1>255</h1></html>";
277: assertEquals(exp, w.toString());
278: }
279:
280: public void testImportCanInheritVariable() throws Exception {
281: Panel p = new Panel();
282: p.setForeground(Color.BLUE);
283:
284: FileObject imp = FileUtil.createData(root, "import.txt");
285: {
286: OutputStream os = imp.getOutputStream();
287: String txt = "${prefix} First Line\n"
288: + "${prefix} Second Line\n";
289: os.write(txt.getBytes());
290: os.close();
291: }
292:
293: FileObject template = FileUtil.createData(root, "some.txt");
294: {
295: OutputStream os = template.getOutputStream();
296: String txt = "<#assign prefix = \"#\">"
297: + "<#include \"import.txt\">";
298: os.write(txt.getBytes());
299: os.close();
300: template.setAttribute("panel", p);
301: }
302: StringWriter w = new StringWriter();
303:
304: apply(template, w);
305:
306: String exp = "# First Line\n" + "# Second Line\n";
307: assertEquals(exp, w.toString());
308: }
309:
310: public void testImportCanInheritVariableInSubFolder()
311: throws Exception {
312: Panel p = new Panel();
313: p.setForeground(Color.BLUE);
314:
315: FileObject imp = FileUtil.createData(root, "sub/import.txt");
316: {
317: OutputStream os = imp.getOutputStream();
318: String txt = "${prefix} First Line\n"
319: + "${prefix} Second Line\n";
320: os.write(txt.getBytes());
321: os.close();
322: }
323:
324: FileObject template = FileUtil.createData(root, "sub/some.txt");
325: {
326: OutputStream os = template.getOutputStream();
327: String txt = "<#assign prefix=\"#\">"
328: + "<#include \"import.txt\">";
329: os.write(txt.getBytes());
330: os.close();
331: template.setAttribute("panel", p);
332: }
333: StringWriter w = new StringWriter();
334:
335: apply(template, w);
336:
337: String exp = "# First Line\n" + "# Second Line\n";
338: assertEquals(exp, w.toString());
339: }
340:
341: public void testAbilityToSendOwnTemplate() throws Exception {
342: Map<String, Object> myValues = new HashMap<String, Object>();
343: myValues.put("prefix", "#");
344:
345: FileObject template = FileUtil.createData(root, "some.txt");
346: {
347: OutputStream os = template.getOutputStream();
348: String txt = "${prefix} First Line\n"
349: + "${prefix} Second Line\n";
350: os.write(txt.getBytes());
351: os.close();
352: template.setAttribute("prefix", " * ");
353: }
354: StringWriter w = new StringWriter();
355:
356: apply(template, w, myValues);
357:
358: String exp = "# First Line\n" + "# Second Line\n";
359: assertEquals(exp, w.toString());
360: }
361:
362: public void testShowItIsPossibleToPassInBeansWrappedObject()
363: throws Exception {
364: FileObject root = FileUtil.createMemoryFileSystem().getRoot();
365: FileObject fo = FileUtil.createData(root, "simpleObject.txt");
366: OutputStream os = fo.getOutputStream();
367: String txt = "<#if (classInfo.getMethods().size() > 0) >The size is greater than 0.</#if>";
368: os.write(txt.getBytes());
369: os.close();
370:
371: StringWriter w = new StringWriter();
372:
373: Map<String, Object> parameters = Collections
374: .<String, Object> singletonMap("classInfo",
375: BeansWrapper.getDefaultInstance().wrap(
376: new ClassInfo()));
377: apply(fo, w, parameters);
378: assertEquals("The size is greater than 0.", w.toString());
379: }
380:
381: public void testShowHowToGetSizeOfASequenceWithoutWrapper()
382: throws Exception {
383: FileObject root = FileUtil.createMemoryFileSystem().getRoot();
384: FileObject fo = FileUtil.createData(root, "simpleObject.txt");
385: OutputStream os = fo.getOutputStream();
386: String txt = "<#if (classInfo.getMethods()?size > 0) >The size is greater than 0.</#if>";
387: os.write(txt.getBytes());
388: os.close();
389:
390: StringWriter w = new StringWriter();
391:
392: Map<String, Object> parameters = Collections
393: .<String, Object> singletonMap("classInfo",
394: new ClassInfo());
395: apply(fo, w, parameters);
396: assertEquals("The size is greater than 0.", w.toString());
397: }
398:
399: public void testMissingClassInfoSimple() throws Exception {
400: FileObject root = FileUtil.createMemoryFileSystem().getRoot();
401: FileObject fo = FileUtil.createData(root, "simpleObject.txt");
402: OutputStream os = fo.getOutputStream();
403: // String txt = "<#if (classInfo.getMethods().size() > 0) >The size is greater than 0.</#if>";
404: String txt = "<#if (classInfo.getMethodsCount() > 0) >The size is greater than 0.</#if>";
405: os.write(txt.getBytes());
406: os.close();
407:
408: StringWriter w = new StringWriter();
409:
410: Map<String, ClassInfo> parameters = Collections.singletonMap(
411: "classInfo", new ClassInfo());
412: apply(fo, w, parameters);
413: assertEquals("The size is greater than 0.", w.toString());
414: }
415:
416: public void testChangeOfTemplate() throws Exception {
417: FileObject template = FileUtil.createData(root, "some.txt");
418: OutputStream os = template.getOutputStream();
419: String txt = "<html><h1>${title}</h1></html>";
420: os.write(txt.getBytes());
421: os.close();
422: template.setAttribute("title", "Nazdar");
423:
424: StringWriter w = new StringWriter();
425:
426: apply(template, w);
427:
428: String exp = "<html><h1>Nazdar</h1></html>";
429: assertEquals(exp, w.toString());
430:
431: os = template.getOutputStream();
432: txt = "<html><h2>${title}</h2></html>";
433: os.write(txt.getBytes());
434: os.close();
435:
436: w = new StringWriter();
437: apply(template, w);
438:
439: exp = "<html><h2>Nazdar</h2></html>";
440: assertEquals("Second run", exp, w.toString());
441: }
442:
443: public void testChangeOfParams() throws Exception {
444: FileObject template = FileUtil.createData(root, "some.txt");
445: OutputStream os = template.getOutputStream();
446: String txt = "<${html}><h1>${title}</h1></${html}>";
447: os.write(txt.getBytes());
448: os.close();
449: template.setAttribute("title", "Nazdar");
450:
451: StringWriter w = new StringWriter();
452:
453: Map<String, Object> param = new HashMap<String, Object>();
454: param.put("html", "html");
455: apply(template, w, param);
456:
457: String exp = "<html><h1>Nazdar</h1></html>";
458: assertEquals(exp, w.toString());
459:
460: param.put("html", "xml");
461: w = new StringWriter();
462: apply(template, w, param);
463:
464: exp = "<xml><h1>Nazdar</h1></xml>";
465: assertEquals("Second run", exp, w.toString());
466: }
467:
468: static void apply(FileObject template, Writer w) throws Exception {
469: apply(template, w, Collections.<String, Object> emptyMap());
470: }
471:
472: static void apply(FileObject template, Writer w,
473: Map<String, ? extends Object> values) throws Exception {
474: ScriptEngineManager mgr = new ScriptEngineManager();
475: ScriptEngine eng = mgr.getEngineByName("freemarker");
476: assertNotNull("We do have such engine", eng);
477: eng.getContext().setWriter(w);
478: eng.getContext().setAttribute(FileObject.class.getName(),
479: template, ScriptContext.ENGINE_SCOPE);
480: eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE)
481: .putAll(values);
482: eng.eval(new InputStreamReader(template.getInputStream()));
483: }
484:
485: public static final class FEQI extends
486: FileEncodingQueryImplementation {
487: @Override
488: public Charset getEncoding(FileObject file) {
489: Object obj = file.getAttribute("enc");
490: if (obj instanceof String) {
491: return Charset.forName((String) obj);
492: }
493: return null;
494: }
495:
496: }
497: }
|