KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.JDialog JavaDoc;
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 /**
48  *
49  * @author Jaroslav Tulach
50  */

51 public class SCFTHandlerTest extends NbTestCase {
52     
53     public SCFTHandlerTest(String JavaDoc testName) {
54         super(testName);
55     }
56     
57     protected boolean runInEQ() {
58         return true;
59     }
60     
61     protected void setUp() throws Exception JavaDoc {
62         MockServices.setServices(DD.class, Pool.class);
63     }
64
65     protected void tearDown() throws Exception JavaDoc {
66         super.tearDown();
67     }
68
69     public void testCreateFromTemplateUsingFreemarker() throws Exception JavaDoc {
70         FileObject root = FileUtil.createMemoryFileSystem().getRoot();
71         FileObject fo = FileUtil.createData(root, "simpleObject.txt");
72         OutputStream JavaDoc os = fo.getOutputStream();
73         String JavaDoc 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 JavaDoc<String JavaDoc,String JavaDoc> 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 JavaDoc exp = "<html><h1>Nazdar</h1></html>";
90         assertEquals(exp, readFile(n.getPrimaryFile()));
91         
92     }
93     
94     
95     public void testBasePropertiesAlwaysPresent() throws Exception JavaDoc {
96         FileObject root = FileUtil.createMemoryFileSystem().getRoot();
97         FileObject fo = FileUtil.createData(root, "simpleObject.txt");
98         OutputStream JavaDoc os = fo.getOutputStream();
99         String JavaDoc 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 JavaDoc<String JavaDoc,String JavaDoc> 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 JavaDoc 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 JavaDoc readFile(FileObject fo) throws IOException JavaDoc {
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 JavaDoc(arr);
132     }
133     
134     public static final class DD extends DialogDisplayer {
135         public Object JavaDoc notify(NotifyDescriptor descriptor) {
136             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
137         }
138
139         public Dialog JavaDoc createDialog(final DialogDescriptor descriptor) {
140             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
141             /*
142             return new JDialog() {
143                 @Deprecated
144                 public void show() {
145                     for (Object object : descriptor.getOptions()) {
146                         if (object instanceof JButton) {
147                             JButton b = (JButton)object;
148                             if (b.getText().equals("Finish")) {
149                                 descriptor.setValue(WizardDescriptor.FINISH_OPTION);
150                                 b.doClick();
151                                 return;
152                             }
153                         }
154                     }
155                     fail("Cannot find Finish button: " + Arrays.asList(descriptor.getOptions()));
156                 }
157             };
158              */

159         }
160     }
161     
162     public static final class Pool extends DataLoaderPool {
163         protected Enumeration JavaDoc<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 JavaDoc 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 JavaDoc {
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 JavaDoc
198         public FileObject createFromTemplate(FileObject f, String JavaDoc name) throws IOException JavaDoc {
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 JavaDoc getName() {
213             return getPrimaryFile().getNameExt();
214         }
215     }
216     
217 }
218
Popular Tags