1 16 package org.netbeans.api.templates; 17 18 import java.awt.Color ; 19 import java.awt.Panel ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.OutputStream ; 23 import java.io.StringWriter ; 24 import java.io.Writer ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Map ; 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 40 public class ProcessorTest extends TestCase { 41 FileObject root; 42 43 public ProcessorTest(String testName) { 44 super(testName); 45 } 46 47 protected void setUp() throws Exception { 48 root = Repository.getDefault().getDefaultFileSystem().getRoot(); 49 for (FileObject f : root.getChildren()) { 50 f.delete(); 51 } 52 } 53 54 protected void tearDown() throws Exception { 55 } 56 57 public void testApply() throws Exception { 58 FileObject template = FileUtil.createData(root, "some.txt"); 59 OutputStream os = template.getOutputStream(); 60 String txt = "<html><h1>${title}</h1></html>"; 61 os.write(txt.getBytes()); 62 os.close(); 63 template.setAttribute("title", "Nazdar"); 64 65 StringWriter w = new StringWriter (); 66 67 apply(template, w); 68 69 String exp = "<html><h1>Nazdar</h1></html>"; 70 assertEquals(exp, w.toString()); 71 } 72 73 public void testCanHandleComplexData() throws Exception { 74 Panel p = new Panel (); 75 p.setForeground(Color.BLUE); 76 77 FileObject template = FileUtil.createData(root, "some.txt"); 78 OutputStream os = template.getOutputStream(); 79 String 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 w = new StringWriter (); 85 86 apply(template, w); 87 88 String exp = "<html><h1>0 0 255</h1></html>"; 89 assertEquals(exp, w.toString()); 90 } 91 92 public void testCanHandleImport() throws Exception { 93 Panel p = new Panel (); 94 p.setForeground(Color.BLUE); 95 96 FileObject imp = FileUtil.createData(root, "import.txt"); 97 { 98 OutputStream os = imp.getOutputStream(); 99 String 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 os = template.getOutputStream(); 107 String txt = "<html><h1><#include \"import.txt\"></h1></html>"; 108 os.write(txt.getBytes()); 109 os.close(); 110 template.setAttribute("panel", p); 111 } 112 StringWriter w = new StringWriter (); 113 114 apply(template, w); 115 116 String exp = "<html><h1>255</h1></html>"; 117 assertEquals(exp, w.toString()); 118 } 119 public void testImportCanInheritVariable() throws Exception { 120 Panel p = new Panel (); 121 p.setForeground(Color.BLUE); 122 123 FileObject imp = FileUtil.createData(root, "import.txt"); 124 { 125 OutputStream os = imp.getOutputStream(); 126 String 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 os = template.getOutputStream(); 135 String txt = "<#assign prefix = \"#\">" + 136 "<#include \"import.txt\">"; 137 os.write(txt.getBytes()); 138 os.close(); 139 template.setAttribute("panel", p); 140 } 141 StringWriter w = new StringWriter (); 142 143 apply(template, w); 144 145 String exp = "# First Line\n" + 146 "# Second Line\n"; 147 assertEquals(exp, w.toString()); 148 } 149 public void testImportCanInheritVariableInSubFolder() throws Exception { 150 Panel p = new Panel (); 151 p.setForeground(Color.BLUE); 152 153 FileObject imp = FileUtil.createData(root, "sub/import.txt"); 154 { 155 OutputStream os = imp.getOutputStream(); 156 String 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 os = template.getOutputStream(); 165 String txt = "<#assign prefix=\"#\">" + 166 "<#include \"import.txt\">"; 167 os.write(txt.getBytes()); 168 os.close(); 169 template.setAttribute("panel", p); 170 } 171 StringWriter w = new StringWriter (); 172 173 apply(template, w); 174 175 String exp = "# First Line\n" + 176 "# Second Line\n"; 177 assertEquals(exp, w.toString()); 178 } 179 public void testAbilityToSendOwnTemplate() throws Exception { 180 Map <String ,Object > myValues = new HashMap <String , Object >(); 181 myValues.put("prefix", "#"); 182 183 FileObject template = FileUtil.createData(root, "some.txt"); 184 { 185 OutputStream os = template.getOutputStream(); 186 String 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 w = new StringWriter (); 193 194 apply(template, w, myValues); 195 196 String exp = "# First Line\n" + 197 "# Second Line\n"; 198 assertEquals(exp, w.toString()); 199 } 200 201 private static void apply(FileObject template, Writer w) throws Exception { 202 apply(template, w, Collections.<String ,Object >emptyMap()); 203 } 204 205 private static void apply(FileObject template, Writer w, Map <String ,? extends Object > values) throws Exception { 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 (template.getInputStream())); 213 } 214 215 } 216 | Popular Tags |