KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > templates > IndentEngineIntTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc.
17  *
18  * Portions Copyrighted 2007 Sun Microsystems, Inc.
19  */

20
21 package org.netbeans.modules.templates;
22
23 import java.awt.Dialog JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.swing.text.Document JavaDoc;
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 /**
51  *
52  * @author Jaroslav Tulach
53  */

54 public class IndentEngineIntTest extends NbTestCase {
55     
56     public IndentEngineIntTest(String JavaDoc testName) {
57         super(testName);
58     }
59     
60     protected boolean runInEQ() {
61         return true;
62     }
63     
64
65     @SuppressWarnings JavaDoc("deprecation")
66     protected void setUp() throws Exception JavaDoc {
67         MockServices.setServices(DD.class, Pool.class, IEImpl.class);
68         FileUtil.setMIMEType("txt", "text/jarda");
69     }
70
71     protected void tearDown() throws Exception JavaDoc {
72         super.tearDown();
73     }
74
75     public void testCreateFromTemplateUsingFreemarker() throws Exception JavaDoc {
76         FileObject root = FileUtil.createMemoryFileSystem().getRoot();
77         FileObject fo = FileUtil.createData(root, "simpleObject.txt");
78         OutputStream JavaDoc os = fo.getOutputStream();
79         String JavaDoc 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 JavaDoc<String JavaDoc,String JavaDoc> 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 JavaDoc exp = ">lmth/<>1h/<radzaN>1h<>lmth<";
96         assertEquals(exp, readFile(n.getPrimaryFile()));
97         
98     }
99     
100     private static String JavaDoc readFile(FileObject fo) throws IOException JavaDoc {
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 JavaDoc(arr);
105     }
106     
107     public static final class DD extends DialogDisplayer {
108         public Object JavaDoc notify(NotifyDescriptor descriptor) {
109             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
110         }
111
112         public Dialog JavaDoc createDialog(final DialogDescriptor descriptor) {
113             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
114         }
115     }
116     
117     public static final class Pool extends DataLoaderPool {
118         protected Enumeration JavaDoc<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 JavaDoc 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 JavaDoc {
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 JavaDoc
153         public FileObject createFromTemplate(FileObject f, String JavaDoc name) throws IOException JavaDoc {
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 JavaDoc getName() {
168             return getPrimaryFile().getNameExt();
169         }
170     }
171
172     public static final class IEImpl extends IndentEngine {
173         
174         
175         public int indentLine(Document JavaDoc doc, int offset) {
176             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
177         }
178
179         public int indentNewLine(Document JavaDoc doc, int offset) {
180             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
181         }
182
183         @Override JavaDoc
184         protected boolean acceptMimeType(String JavaDoc mime) {
185             return "text/jarda".equals(mime); // NOI18N
186
}
187
188         public Writer JavaDoc createWriter(Document JavaDoc doc, int offset, final Writer JavaDoc writer) {
189             class Rotate extends StringWriter JavaDoc {
190                 @Override JavaDoc
191                 public void close() throws IOException JavaDoc {
192                     super.close();
193                     
194                     String JavaDoc s = toString();
195                     StringBuilder JavaDoc sb = new StringBuilder JavaDoc(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