KickJava   Java API By Example, From Geeks To Geeks.

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


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. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import org.netbeans.junit.MockServices;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.Repository;
30 import org.openide.util.Enumerations;
31 import org.openide.util.MapFormat;
32
33 /** Checks the ability to create data object from template.
34  * (only for investing bug #38421, could be removed if needed)
35  * @author Jiri Rechtacek
36  */

37 public class CreateFromTemplateTest extends NbTestCase {
38
39     public CreateFromTemplateTest (String JavaDoc name) {
40         super(name);
41     }
42     
43     protected void setUp() {
44         MockServices.setServices(Pool.class);
45     }
46
47     public void testCreateExecutorFromTemplate () throws Exception JavaDoc {
48         String JavaDoc folderName = "/Templates/Services/Executor";
49         FileObject data = org.openide.filesystems.FileUtil.createData (
50             Repository.getDefault ().getDefaultFileSystem ().getRoot (),
51             folderName + "/" + "X.xml"
52         );
53         data.setAttribute ("template", Boolean.TRUE);
54         FileObject fo = data.getParent ();
55         assertNotNull ("FileObject " + folderName + " found on DefaultFileSystem.", fo);
56         DataFolder f = DataFolder.findFolder (fo);
57         assertNotNull ("Folder " + folderName + " found on DefaultFileSystem.", f);
58         DataObject[] executors = f.getChildren ();
59         assertTrue ("Templates for Executor found.", executors.length > 0);
60         DataObject executor = executors[0];
61 // System.out.println("do Executors before:");
62
// for (int i = 0; i < executors.length; i++) {
63
// System.out.println(">>> " + i + " -- " + executors[i].getName ());
64
// }
65
// System.out.println("done.");
66
assertNotNull ("Executor found.", executor);
67         String JavaDoc newExecutorName = "NewExecutor" + Double.toString (Math.random ());
68         executor.createFromTemplate (f, newExecutorName);
69         executors = f.getChildren ();
70         boolean found = false;
71 // System.out.println("do Executors after:");
72
for (int i = 0; i < executors.length && !found; i++) {
73 // System.out.println(">>> " + i + " -- " + executors[i].getName ());
74
found = newExecutorName.equals (executors[i].getName ());
75         }
76 // System.out.println("done.");
77
assertTrue (newExecutorName + " was created on right place.", found);
78     }
79
80     public void testNoTemplateFlagUnset() throws Exception JavaDoc {
81         String JavaDoc folderName = "/Templates/";
82         FileObject data = org.openide.filesystems.FileUtil.createData (
83             Repository.getDefault ().getDefaultFileSystem ().getRoot (),
84             folderName + "/" + "X.prima"
85         );
86         data.setAttribute ("template", Boolean.TRUE);
87         FileObject fo = data.getParent ();
88         assertNotNull ("FileObject " + folderName + " found on DefaultFileSystem.", fo);
89         DataFolder f = DataFolder.findFolder (fo);
90         DataObject templ = DataObject.find(data);
91         
92         DataObject res = templ.createFromTemplate(f);
93         
94         assertFalse("Not marked as template", res.isTemplate());
95         assertEquals(SimpleLoader.class, res.getLoader().getClass());
96     }
97     
98     public static final class Pool extends DataLoaderPool {
99         protected Enumeration JavaDoc<DataLoader> loaders() {
100             return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class));
101         }
102     }
103     
104     public static final class SimpleLoader extends MultiFileLoader {
105         public SimpleLoader() {
106             super(SimpleObject.class.getName());
107         }
108         protected String JavaDoc displayName() {
109             return "SimpleLoader";
110         }
111         protected FileObject findPrimaryFile(FileObject fo) {
112             if (fo.hasExt("prima")) {
113                 return fo;
114             }
115             return null;
116         }
117         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
118             return new SimpleObject(this, primaryFile);
119         }
120         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
121             return new FE(obj, primaryFile);
122         }
123         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
124             return new FileEntry(obj, secondaryFile);
125         }
126     }
127     
128     private static final class FE extends FileEntry.Format {
129         public FE(MultiDataObject mo, FileObject fo) {
130             super(mo, fo);
131         }
132         
133         protected java.text.Format JavaDoc createFormat(FileObject target, String JavaDoc n, String JavaDoc e) {
134             return new MapFormat(Collections.emptyMap());
135         }
136     }
137     
138     public static final class SimpleObject extends MultiDataObject {
139         public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
140             super(fo, l);
141         }
142         
143         public String JavaDoc getName() {
144             return getPrimaryFile().getNameExt();
145         }
146     }
147     
148 }
149
150
151
Popular Tags