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.io.StringWriter ; 27 import java.io.Writer ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.Map ; 31 import javax.swing.text.Document ; 32 import org.netbeans.junit.MockServices; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.DialogDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.loaders.DataFolder; 40 import org.openide.loaders.DataLoader; 41 import org.openide.loaders.DataLoaderPool; 42 import org.openide.loaders.DataObject; 43 import org.openide.loaders.DataObjectExistsException; 44 import org.openide.loaders.FileEntry; 45 import org.openide.loaders.MultiDataObject; 46 import org.openide.loaders.MultiFileLoader; 47 import org.openide.text.IndentEngine; 48 import org.openide.util.Enumerations; 49 50 54 public class IndentEngineIntTest extends NbTestCase { 55 56 public IndentEngineIntTest(String testName) { 57 super(testName); 58 } 59 60 protected boolean runInEQ() { 61 return true; 62 } 63 64 65 @SuppressWarnings ("deprecation") 66 protected void setUp() throws Exception { 67 MockServices.setServices(DD.class, Pool.class, IEImpl.class); 68 FileUtil.setMIMEType("txt", "text/jarda"); 69 } 70 71 protected void tearDown() throws Exception { 72 super.tearDown(); 73 } 74 75 public void testCreateFromTemplateUsingFreemarker() throws Exception { 76 FileObject root = FileUtil.createMemoryFileSystem().getRoot(); 77 FileObject fo = FileUtil.createData(root, "simpleObject.txt"); 78 OutputStream os = fo.getOutputStream(); 79 String txt = "<html><h1>${title}</h1></html>"; 80 os.write(txt.getBytes()); 81 os.close(); 82 fo.setAttribute("javax.script.ScriptEngine", "freemarker"); 83 84 85 DataObject obj = DataObject.find(fo); 86 87 DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); 88 89 Map <String ,String > parameters = Collections.singletonMap("title", "Nazdar"); 90 DataObject n = obj.createFromTemplate(folder, "complex", parameters); 91 92 assertEquals("Created in right place", folder, n.getFolder()); 93 assertEquals("Created with right name", "complex.txt", n.getName()); 94 95 String exp = ">lmth/<>1h/<radzaN>1h<>lmth<"; 96 assertEquals(exp, readFile(n.getPrimaryFile())); 97 98 } 99 100 private static String readFile(FileObject fo) throws IOException { 101 byte[] arr = new byte[(int)fo.getSize()]; 102 int len = fo.getInputStream().read(arr); 103 assertEquals("Fully read", arr.length, len); 104 return new String (arr); 105 } 106 107 public static final class DD extends DialogDisplayer { 108 public Object notify(NotifyDescriptor descriptor) { 109 throw new UnsupportedOperationException ("Not supported yet."); 110 } 111 112 public Dialog createDialog(final DialogDescriptor descriptor) { 113 throw new UnsupportedOperationException ("Not supported yet."); 114 } 115 } 116 117 public static final class Pool extends DataLoaderPool { 118 protected Enumeration <DataLoader> loaders() { 119 return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class)); 120 } 121 } 122 123 public static final class SimpleLoader extends MultiFileLoader { 124 public SimpleLoader() { 125 super(SimpleObject.class.getName()); 126 } 127 protected String displayName() { 128 return "SimpleLoader"; 129 } 130 protected FileObject findPrimaryFile(FileObject fo) { 131 if (fo.hasExt("prima")) { 132 return fo; 133 } 134 return null; 135 } 136 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 137 return new SimpleObject(this, primaryFile); 138 } 139 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 140 return new FE(obj, primaryFile); 141 } 142 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 143 return new FileEntry(obj, secondaryFile); 144 } 145 } 146 147 private static final class FE extends FileEntry { 148 public FE(MultiDataObject mo, FileObject fo) { 149 super(mo, fo); 150 } 151 152 @Override 153 public FileObject createFromTemplate(FileObject f, String name) throws IOException { 154 fail("I do not want to be called"); 155 return null; 156 } 157 158 159 160 } 161 162 public static final class SimpleObject extends MultiDataObject { 163 public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { 164 super(fo, l); 165 } 166 167 public String getName() { 168 return getPrimaryFile().getNameExt(); 169 } 170 } 171 172 public static final class IEImpl extends IndentEngine { 173 174 175 public int indentLine(Document doc, int offset) { 176 throw new UnsupportedOperationException ("Not supported yet."); 177 } 178 179 public int indentNewLine(Document doc, int offset) { 180 throw new UnsupportedOperationException ("Not supported yet."); 181 } 182 183 @Override 184 protected boolean acceptMimeType(String mime) { 185 return "text/jarda".equals(mime); } 187 188 public Writer createWriter(Document doc, int offset, final Writer writer) { 189 class Rotate extends StringWriter { 190 @Override 191 public void close() throws IOException { 192 super.close(); 193 194 String s = toString(); 195 StringBuilder sb = new StringBuilder (s.length()); 196 for (int i = s.length() - 1; i >= 0; i--) { 197 sb.append(s.charAt(i)); 198 } 199 200 writer.write(sb.toString()); 201 writer.close(); 202 } 203 } 204 205 assertNotNull("There is some document", doc); 206 assertEquals("Its length is 0", 0, doc.getLength()); 207 assertEquals("Offset is 0", 0, offset); 208 209 return new Rotate(); 210 } 211 } 212 } 213 | Popular Tags |