KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > templates > ProcessorTest


1 /*
2 * The contents of this file are subject to the terms of the
3 * Common Development and Distribution License, Version 1.0 only
4 * (the "License"). You may not use this file except in compliance
5 * with the License. A copy of the license is available
6 * at http://www.opensource.org/licenses/cddl1.php
7 *
8 * See the License for the specific language governing permissions
9 * and limitations under the License.
10 *
11 * The Original Code is the dvbcentral.sf.net project.
12 * The Initial Developer of the Original Code is Jaroslav Tulach.
13 * Portions created by Jaroslav Tulach are Copyright (C) 2006.
14 * All Rights Reserved.
15 */

16 package org.netbeans.api.templates;
17
18 import java.awt.Color JavaDoc;
19 import java.awt.Panel JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.StringWriter JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.script.ScriptContext;
29 import javax.script.ScriptEngine;
30 import javax.script.ScriptEngineManager;
31 import junit.framework.TestCase;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35
36 /**
37  *
38  * @author Jaroslav Tulach
39  */

40 public class ProcessorTest extends TestCase {
41     FileObject root;
42     
43     public ProcessorTest(String JavaDoc testName) {
44         super(testName);
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48         root = Repository.getDefault().getDefaultFileSystem().getRoot();
49         for (FileObject f : root.getChildren()) {
50             f.delete();
51         }
52     }
53
54     protected void tearDown() throws Exception JavaDoc {
55     }
56
57     public void testApply() throws Exception JavaDoc {
58         FileObject template = FileUtil.createData(root, "some.txt");
59         OutputStream JavaDoc os = template.getOutputStream();
60         String JavaDoc txt = "<html><h1>${title}</h1></html>";
61         os.write(txt.getBytes());
62         os.close();
63         template.setAttribute("title", "Nazdar");
64         
65         StringWriter JavaDoc w = new StringWriter JavaDoc();
66         
67         apply(template, w);
68         
69         String JavaDoc exp = "<html><h1>Nazdar</h1></html>";
70         assertEquals(exp, w.toString());
71     }
72
73     public void testCanHandleComplexData() throws Exception JavaDoc {
74         Panel JavaDoc p = new Panel JavaDoc();
75         p.setForeground(Color.BLUE);
76         
77         FileObject template = FileUtil.createData(root, "some.txt");
78         OutputStream JavaDoc os = template.getOutputStream();
79         String JavaDoc txt = "<html><h1>${panel.foreground.red} ${panel.foreground.green} ${panel.foreground.blue}</h1></html>";
80         os.write(txt.getBytes());
81         os.close();
82         template.setAttribute("panel", p);
83         
84         StringWriter JavaDoc w = new StringWriter JavaDoc();
85         
86         apply(template, w);
87         
88         String JavaDoc exp = "<html><h1>0 0 255</h1></html>";
89         assertEquals(exp, w.toString());
90     }
91     
92     public void testCanHandleImport() throws Exception JavaDoc {
93         Panel JavaDoc p = new Panel JavaDoc();
94         p.setForeground(Color.BLUE);
95
96         FileObject imp = FileUtil.createData(root, "import.txt");
97         {
98             OutputStream JavaDoc os = imp.getOutputStream();
99             String JavaDoc txt = "${panel.foreground.blue}";
100             os.write(txt.getBytes());
101             os.close();
102         }
103         
104         FileObject template = FileUtil.createData(root, "some.txt");
105         {
106             OutputStream JavaDoc os = template.getOutputStream();
107             String JavaDoc txt = "<html><h1><#include \"import.txt\"></h1></html>";
108             os.write(txt.getBytes());
109             os.close();
110             template.setAttribute("panel", p);
111         }
112         StringWriter JavaDoc w = new StringWriter JavaDoc();
113         
114         apply(template, w);
115         
116         String JavaDoc exp = "<html><h1>255</h1></html>";
117         assertEquals(exp, w.toString());
118     }
119     public void testImportCanInheritVariable() throws Exception JavaDoc {
120         Panel JavaDoc p = new Panel JavaDoc();
121         p.setForeground(Color.BLUE);
122
123         FileObject imp = FileUtil.createData(root, "import.txt");
124         {
125             OutputStream JavaDoc os = imp.getOutputStream();
126             String JavaDoc txt = "${prefix} First Line\n" +
127                          "${prefix} Second Line\n";
128             os.write(txt.getBytes());
129             os.close();
130         }
131         
132         FileObject template = FileUtil.createData(root, "some.txt");
133         {
134             OutputStream JavaDoc os = template.getOutputStream();
135             String JavaDoc txt = "<#assign prefix = \"#\">" +
136                          "<#include \"import.txt\">";
137             os.write(txt.getBytes());
138             os.close();
139             template.setAttribute("panel", p);
140         }
141         StringWriter JavaDoc w = new StringWriter JavaDoc();
142         
143         apply(template, w);
144         
145         String JavaDoc exp = "# First Line\n" +
146                      "# Second Line\n";
147         assertEquals(exp, w.toString());
148     }
149     public void testImportCanInheritVariableInSubFolder() throws Exception JavaDoc {
150         Panel JavaDoc p = new Panel JavaDoc();
151         p.setForeground(Color.BLUE);
152
153         FileObject imp = FileUtil.createData(root, "sub/import.txt");
154         {
155             OutputStream JavaDoc os = imp.getOutputStream();
156             String JavaDoc txt = "${prefix} First Line\n" +
157                          "${prefix} Second Line\n";
158             os.write(txt.getBytes());
159             os.close();
160         }
161         
162         FileObject template = FileUtil.createData(root, "sub/some.txt");
163         {
164             OutputStream JavaDoc os = template.getOutputStream();
165             String JavaDoc txt = "<#assign prefix=\"#\">" +
166                          "<#include \"import.txt\">";
167             os.write(txt.getBytes());
168             os.close();
169             template.setAttribute("panel", p);
170         }
171         StringWriter JavaDoc w = new StringWriter JavaDoc();
172         
173         apply(template, w);
174         
175         String JavaDoc exp = "# First Line\n" +
176                      "# Second Line\n";
177         assertEquals(exp, w.toString());
178     }
179     public void testAbilityToSendOwnTemplate() throws Exception JavaDoc {
180         Map JavaDoc<String JavaDoc,Object JavaDoc> myValues = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
181         myValues.put("prefix", "#");
182
183         FileObject template = FileUtil.createData(root, "some.txt");
184         {
185             OutputStream JavaDoc os = template.getOutputStream();
186             String JavaDoc txt = "${prefix} First Line\n" +
187                          "${prefix} Second Line\n";
188             os.write(txt.getBytes());
189             os.close();
190             template.setAttribute("prefix", " * ");
191         }
192         StringWriter JavaDoc w = new StringWriter JavaDoc();
193         
194         apply(template, w, myValues);
195         
196         String JavaDoc exp = "# First Line\n" +
197                      "# Second Line\n";
198         assertEquals(exp, w.toString());
199     }
200     
201     private static void apply(FileObject template, Writer JavaDoc w) throws Exception JavaDoc {
202         apply(template, w, Collections.<String JavaDoc,Object JavaDoc>emptyMap());
203     }
204     
205     private static void apply(FileObject template, Writer JavaDoc w, Map JavaDoc<String JavaDoc,? extends Object JavaDoc> values) throws Exception JavaDoc {
206         ScriptEngineManager mgr = new ScriptEngineManager();
207         ScriptEngine eng = mgr.getEngineByName("freemarker");
208         assertNotNull("We do have such engine", eng);
209         eng.getContext().setWriter(w);
210         eng.getContext().setAttribute(FileObject.class.getName(), template, ScriptContext.ENGINE_SCOPE);
211         eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE).putAll(values);
212         eng.eval(new InputStreamReader JavaDoc(template.getInputStream()));
213     }
214     
215 }
216
Popular Tags