1 20 21 package org.netbeans.modules.templates; 22 23 import java.awt.Dialog ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.Map ; 29 import javax.swing.JDialog ; 30 import org.netbeans.junit.MockServices; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.DialogDescriptor; 33 import org.openide.DialogDisplayer; 34 import org.openide.NotifyDescriptor; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.loaders.DataFolder; 38 import org.openide.loaders.DataLoader; 39 import org.openide.loaders.DataLoaderPool; 40 import org.openide.loaders.DataObject; 41 import org.openide.loaders.DataObjectExistsException; 42 import org.openide.loaders.FileEntry; 43 import org.openide.loaders.MultiDataObject; 44 import org.openide.loaders.MultiFileLoader; 45 import org.openide.util.Enumerations; 46 47 51 public class SCFTHandlerTest extends NbTestCase { 52 53 public SCFTHandlerTest(String testName) { 54 super(testName); 55 } 56 57 protected boolean runInEQ() { 58 return true; 59 } 60 61 protected void setUp() throws Exception { 62 MockServices.setServices(DD.class, Pool.class); 63 } 64 65 protected void tearDown() throws Exception { 66 super.tearDown(); 67 } 68 69 public void testCreateFromTemplateUsingFreemarker() throws Exception { 70 FileObject root = FileUtil.createMemoryFileSystem().getRoot(); 71 FileObject fo = FileUtil.createData(root, "simpleObject.txt"); 72 OutputStream os = fo.getOutputStream(); 73 String txt = "<html><h1>${title}</h1></html>"; 74 os.write(txt.getBytes()); 75 os.close(); 76 fo.setAttribute("javax.script.ScriptEngine", "freemarker"); 77 78 79 DataObject obj = DataObject.find(fo); 80 81 DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); 82 83 Map <String ,String > parameters = Collections.singletonMap("title", "Nazdar"); 84 DataObject n = obj.createFromTemplate(folder, "complex", parameters); 85 86 assertEquals("Created in right place", folder, n.getFolder()); 87 assertEquals("Created with right name", "complex.txt", n.getName()); 88 89 String exp = "<html><h1>Nazdar</h1></html>"; 90 assertEquals(exp, readFile(n.getPrimaryFile())); 91 92 } 93 94 95 public void testBasePropertiesAlwaysPresent() throws Exception { 96 FileObject root = FileUtil.createMemoryFileSystem().getRoot(); 97 FileObject fo = FileUtil.createData(root, "simpleObject.txt"); 98 OutputStream os = fo.getOutputStream(); 99 String txt = "<html><h1>${name}</h1>" + 100 "<h2>${date}</h2>" + 101 "<h3>${time}</h3>" + 102 "<h4>${user}</h4>" + 103 "</html>"; 104 os.write(txt.getBytes()); 105 os.close(); 106 fo.setAttribute("javax.script.ScriptEngine", "freemarker"); 107 108 109 DataObject obj = DataObject.find(fo); 110 111 DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); 112 113 Map <String ,String > parameters = Collections.singletonMap("title", "Nazdar"); 114 DataObject n = obj.createFromTemplate(folder, "complex", parameters); 115 116 assertEquals("Created in right place", folder, n.getFolder()); 117 assertEquals("Created with right name", "complex.txt", n.getName()); 118 119 String res = readFile(n.getPrimaryFile()); 120 121 if (res.indexOf("date") >= 0) fail(res); 122 if (res.indexOf("time") >= 0) fail(res); 123 if (res.indexOf("user") >= 0) fail(res); 124 if (res.indexOf("name") >= 0) fail(res); 125 } 126 127 private static String readFile(FileObject fo) throws IOException { 128 byte[] arr = new byte[(int)fo.getSize()]; 129 int len = fo.getInputStream().read(arr); 130 assertEquals("Fully read", arr.length, len); 131 return new String (arr); 132 } 133 134 public static final class DD extends DialogDisplayer { 135 public Object notify(NotifyDescriptor descriptor) { 136 throw new UnsupportedOperationException ("Not supported yet."); 137 } 138 139 public Dialog createDialog(final DialogDescriptor descriptor) { 140 throw new UnsupportedOperationException ("Not supported yet."); 141 159 } 160 } 161 162 public static final class Pool extends DataLoaderPool { 163 protected Enumeration <DataLoader> loaders() { 164 return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class)); 165 } 166 } 167 168 public static final class SimpleLoader extends MultiFileLoader { 169 public SimpleLoader() { 170 super(SimpleObject.class.getName()); 171 } 172 protected String displayName() { 173 return "SimpleLoader"; 174 } 175 protected FileObject findPrimaryFile(FileObject fo) { 176 if (fo.hasExt("prima")) { 177 return fo; 178 } 179 return null; 180 } 181 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 182 return new SimpleObject(this, primaryFile); 183 } 184 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 185 return new FE(obj, primaryFile); 186 } 187 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 188 return new FileEntry(obj, secondaryFile); 189 } 190 } 191 192 private static final class FE extends FileEntry { 193 public FE(MultiDataObject mo, FileObject fo) { 194 super(mo, fo); 195 } 196 197 @Override 198 public FileObject createFromTemplate(FileObject f, String name) throws IOException { 199 fail("I do not want to be called"); 200 return null; 201 } 202 203 204 205 } 206 207 public static final class SimpleObject extends MultiDataObject { 208 public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { 209 super(fo, l); 210 } 211 212 public String getName() { 213 return getPrimaryFile().getNameExt(); 214 } 215 } 216 217 } 218 | Popular Tags |