KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > CreateFromTemplateHandlerTest


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.openide.loaders;
22
23 import java.awt.Dialog JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JDialog 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.WizardDescriptor;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.Enumerations;
41
42 /**
43  *
44  * @author Jaroslav Tulach
45  */

46 public class CreateFromTemplateHandlerTest extends NbTestCase {
47     
48     public CreateFromTemplateHandlerTest(String JavaDoc testName) {
49         super(testName);
50     }
51     
52     protected boolean runInEQ() {
53         return true;
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         Hand.acceptObject = null;
58         Hand.fileObject = null;
59         Hand.origObject = null;
60         Hand.name = null;
61         Hand.parameters = null;
62         
63         MockServices.setServices(Hand.class, Attr.class, DD.class, Pool.class);
64     }
65
66     protected void tearDown() throws Exception JavaDoc {
67         super.tearDown();
68     }
69
70     public void testCreateFromTemplate() throws Exception JavaDoc {
71         FileObject root = FileUtil.createMemoryFileSystem().getRoot();
72         FileObject fo = FileUtil.createData(root, "simpleObject.txt");
73         
74         DataObject obj = DataObject.find(fo);
75         
76         DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
77         
78         Map JavaDoc<String JavaDoc,String JavaDoc> parameters = Collections.singletonMap("type", "empty");
79         DataObject n = obj.createFromTemplate(folder, "complex", parameters);
80         
81         assertEquals("Created in right place", folder, n.getFolder());
82         assertEquals("Created with right name", "complex.txt", n.getName());
83         
84         assertEquals("The right source", fo, Hand.origObject);
85         assertEquals("The right source in query", fo, Hand.acceptObject);
86         assertEquals("The right destiny folder", folder.getPrimaryFile(), Hand.fileObject);
87         assertEquals("The right name", "complex", Hand.name);
88         if (Hand.parameters.size() < 2) {
89             fail("As least two: " + Hand.parameters + " but was " + Hand.parameters.size());
90         }
91         assertEquals("empty", Hand.parameters.get("type"));
92         assertEquals("complex", Hand.parameters.get("name"));
93         try {
94             Hand.parameters.put("kuk", "buk");
95         } catch (UnsupportedOperationException JavaDoc ex) {
96             // ok
97
return;
98         }
99         fail("Modifications shall be unsupported");
100     }
101     
102     public void testTemplateWizardCopiesItsPropertiesToMap() throws Exception JavaDoc {
103         doTemplateWizardCopiesItsPropertiesToMap("simpleObject.txt");
104     }
105     
106     public void testTemplateWizardCopiesItsPropertiesToMapForOverridenEntry() throws Exception JavaDoc {
107         DataObject obj = doTemplateWizardCopiesItsPropertiesToMap("simpleObject.prima");
108         assertEquals("The right loader", SimpleLoader.class, obj.getLoader().getClass());
109     }
110     
111     private DataObject doTemplateWizardCopiesItsPropertiesToMap(String JavaDoc fileName) throws Exception JavaDoc {
112         FileObject root = FileUtil.createMemoryFileSystem().getRoot();
113         FileObject fo = FileUtil.createData(root, fileName);
114         
115         DataObject obj = DataObject.find(fo);
116         
117         DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target"));
118         
119         TemplateWizard t = new TemplateWizard();
120         t.putProperty("type", "empty");
121         Set JavaDoc<DataObject> created = t.instantiate(obj, folder);
122         assertNotNull(created);
123         assertEquals("One is created: " + created, 1, created.size());
124         
125         DataObject n = created.iterator().next();
126         
127         assertEquals("Created in right place", folder, n.getFolder());
128         assertEquals("Created with right name", fileName, n.getName());
129         
130         assertEquals("The right source", fo, Hand.origObject);
131         assertEquals("The right source in query", fo, Hand.acceptObject);
132         assertEquals("The right destiny folder", folder.getPrimaryFile(), Hand.fileObject);
133         assertEquals("The right name", "simpleObject", Hand.name);
134         assertTrue("At least two elements: " + Hand.parameters, 2 <= Hand.parameters.size());
135         assertEquals("empty", Hand.parameters.get("type"));
136         assertEquals("There was no name, just default", null, Hand.parameters.get("name"));
137         assertTrue("the argument is there", Hand.parameters.containsKey("name"));
138         try {
139             Hand.parameters.put("kuk", "buk");
140         } catch (UnsupportedOperationException JavaDoc ex) {
141             // ok
142
return obj;
143         }
144         fail("Modifications shall be unsupported");
145         throw new NullPointerException JavaDoc();
146     }
147
148     public static final class Hand extends CreateFromTemplateHandler {
149         public static FileObject fileObject, origObject, acceptObject;
150         public static String JavaDoc name;
151         public static Map JavaDoc<String JavaDoc, Object JavaDoc> parameters;
152     
153         protected boolean accept(FileObject fo) {
154             assertNull(acceptObject);
155             acceptObject = fo;
156             return true;
157         }
158
159         protected FileObject createFromTemplate(
160             FileObject orig, FileObject f, String JavaDoc n,
161             Map JavaDoc<String JavaDoc, Object JavaDoc> p
162         ) throws IOException JavaDoc {
163             assertNull("Not yet filled", fileObject);
164             
165             origObject = orig;
166             fileObject = f;
167             name = n;
168             parameters = p;
169
170             return FileUtil.copyFile(orig, fileObject, name);
171         }
172     }
173     
174     public static final class Attr extends CreateFromTemplateAttributesProvider {
175         
176     
177         protected Map JavaDoc<String JavaDoc, ? extends Object JavaDoc> attributesFor(
178             DataObject template,
179             DataFolder target,
180             String JavaDoc name
181         ) {
182             return Collections.singletonMap("name", name);
183         }
184     }
185     
186     public static final class DD extends DialogDisplayer {
187         public Object JavaDoc notify(NotifyDescriptor descriptor) {
188             throw new UnsupportedOperationException JavaDoc("Not supported yet.");
189         }
190
191         public Dialog JavaDoc createDialog(final DialogDescriptor descriptor) {
192             return new JDialog JavaDoc() {
193                 @Deprecated JavaDoc
194                 public void show() {
195                     for (Object JavaDoc object : descriptor.getOptions()) {
196                         if (object instanceof JButton JavaDoc) {
197                             JButton JavaDoc b = (JButton JavaDoc)object;
198                             if (b.getText().equals("Finish")) {
199                                 descriptor.setValue(WizardDescriptor.FINISH_OPTION);
200                                 b.doClick();
201                                 return;
202                             }
203                         }
204                     }
205                     fail("Cannot find Finish button: " + Arrays.asList(descriptor.getOptions()));
206                 }
207             };
208         }
209     }
210     
211     public static final class Pool extends DataLoaderPool {
212         protected Enumeration JavaDoc<DataLoader> loaders() {
213             return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class));
214         }
215     }
216     
217     public static final class SimpleLoader extends MultiFileLoader {
218         public SimpleLoader() {
219             super(SimpleObject.class.getName());
220         }
221         protected String JavaDoc displayName() {
222             return "SimpleLoader";
223         }
224         protected FileObject findPrimaryFile(FileObject fo) {
225             if (fo.hasExt("prima")) {
226                 return fo;
227             }
228             return null;
229         }
230         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
231             return new SimpleObject(this, primaryFile);
232         }
233         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
234             return new FE(obj, primaryFile);
235         }
236         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
237             return new FileEntry(obj, secondaryFile);
238         }
239     }
240     
241     private static final class FE extends FileEntry {
242         public FE(MultiDataObject mo, FileObject fo) {
243             super(mo, fo);
244         }
245
246         @Override JavaDoc
247         public FileObject createFromTemplate(FileObject f, String JavaDoc name) throws IOException JavaDoc {
248             fail("I do not want to be called");
249             return null;
250         }
251
252         
253         
254     }
255     
256     public static final class SimpleObject extends MultiDataObject {
257         public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
258             super(fo, l);
259         }
260         
261         public String JavaDoc getName() {
262             return getPrimaryFile().getNameExt();
263         }
264     }
265     
266 }
267
Popular Tags