KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import org.netbeans.junit.Log;
27 import org.netbeans.junit.NbTestCase;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.nodes.CookieSet;
31 import org.openide.util.Enumerations;
32 import org.openide.util.NbBundle;
33 import org.openide.util.io.NbMarshalledObject;
34
35
36 /**
37  * Java vs. Form in wrong order. Based on KitFox's report in #73037.
38  *
39  * @author Jaroslav Tulach
40  */

41 public class FightWithWrongOrderOfLoadersTest extends LoggingTestCaseHid
42 implements DataLoader.RecognizedFiles {
43
44     private CharSequence JavaDoc log;
45     private FileObject f0;
46     private FileObject f1;
47     private FileObject f2;
48     
49     public FightWithWrongOrderOfLoadersTest(String JavaDoc testName) {
50         super(testName);
51     }
52
53     protected void setUp() throws Exception JavaDoc {
54         log = Log.enable("org.openide.loaders", Level.SEVERE);
55
56         registerIntoLookup(new Pool());
57
58         FileObject fo = FileUtil.createFolder(FileUtil.createMemoryFileSystem().getRoot(), "test");
59
60         f0 = FileUtil.createData(fo, "j1.java");
61         f1 = FileUtil.createData(fo, "f.formKit");
62         f2 = FileUtil.createData(fo, "f.java");
63     }
64
65     protected Level JavaDoc logLevel() {
66         return Level.INFO;
67     }
68
69     public void testRecognizeToDefault() throws Exception JavaDoc {
70         DataObject obj0 = DataObject.find(f0);
71         DataObject obj1 = DataObject.find(f1);
72         DataObject obj2 = DataObject.find(f2);
73
74         assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj0.getLoader());
75         assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj2.getLoader());
76
77
78         if (obj1 == obj2) {
79             fail("They should be different: " + obj1);
80         }
81
82         assertEquals("It is default loader", obj1.getLoader(), DataLoaderPool.getDefaultFileLoader());
83     }
84
85     public void testRecognizeJavaFirstFormKitLater() throws Exception JavaDoc {
86         DataObject obj0 = DataObject.find(f0);
87         DataObject obj2 = DataObject.find(f2);
88         DataObject obj1 = DataObject.find(f1);
89
90         assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj0.getLoader());
91         assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj2.getLoader());
92
93         if (obj1 == obj2) {
94             fail("They should be different: " + obj1);
95         }
96
97         assertEquals("It is default loader", obj1.getLoader(), DataLoaderPool.getDefaultFileLoader());
98     }
99
100
101
102     public void markRecognized(FileObject fo) {
103     }
104
105
106     private static final class Pool extends DataLoaderPool {
107         private static DataLoader[] ARR = {
108             JavaDataLoader.getLoader(JavaDataLoader.class),
109             FormKitDataLoader.getLoader(FormKitDataLoader.class),
110         };
111
112
113         protected Enumeration JavaDoc loaders() {
114             return Enumerations.array(ARR);
115         }
116     }
117
118     public static class JavaDataLoader extends MultiFileLoader {
119
120         public static final String JavaDoc JAVA_EXTENSION = "java";
121
122         public JavaDataLoader() {
123             super(JavaDO.class.getName());
124         }
125
126         protected JavaDataLoader(String JavaDoc s) {
127             super(s);
128         }
129
130         protected FileObject findPrimaryFile(FileObject fo) {
131             if (fo.hasExt(JAVA_EXTENSION)) {
132                 return fo;
133             } else {
134                 return null;
135             }
136         }
137
138         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
139             return new JavaDO(primaryFile, this);
140         }
141
142         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
143             return new FileEntry(obj, primaryFile);
144         }
145
146         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
147             throw new UnsupportedOperationException JavaDoc();
148         }
149
150         static class JavaDO extends MultiDataObject {
151             public JavaDO(FileObject fo, JavaDataLoader l) throws DataObjectExistsException {
152                 super(fo, l);
153             }
154         }
155     }
156
157
158     public static class FormKitDataLoader extends JavaDataLoader {
159         private static Logger JavaDoc LOG = Logger.getLogger(FormKitDataLoader.class.getName());
160
161         public static final String JavaDoc REQUIRED_MIME = "text/x-java";
162         public static final String JavaDoc FORM_EXTENSION = "formKit"; // NOI18N
163

164         private static final long serialVersionUID = 1L;
165
166         public FormKitDataLoader()
167         {
168             super(FormKitDataObject.class.getName());
169         }
170
171         protected String JavaDoc defaultDisplayName()
172         {
173             return NbBundle.getMessage(FormKitDataLoader.class, "LBL_FormKit_loader_name");
174         }
175
176         protected String JavaDoc actionsContext()
177         {
178             return "Loaders/" + REQUIRED_MIME + "/Actions";
179         }
180
181         protected FileObject findPrimaryFile(FileObject fo)
182         {
183             LOG.info("FormKitDataLoader.findPrimaryFile(): " + fo.getNameExt());
184
185             String JavaDoc ext = fo.getExt();
186             if (ext.equals(FORM_EXTENSION))
187             {
188                 return FileUtil.findBrother(fo, JAVA_EXTENSION);
189             }
190             if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FORM_EXTENSION) != null)
191             {
192                 return fo;
193             }
194             return null;
195         }
196
197         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, java.io.IOException JavaDoc
198         {
199             LOG.info("FormKitDataLoader.createMultiObject(): " + primaryFile.getNameExt());
200
201             return new FormKitDataObject(FileUtil.findBrother(primaryFile, FORM_EXTENSION),
202                     primaryFile,
203                     this);
204         }
205
206         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject multiDataObject, FileObject fileObject)
207         {
208             if (fileObject.getExt().equals(FORM_EXTENSION))
209             {
210                 FileEntry formEntry = new FileEntry(multiDataObject, fileObject);
211                 return formEntry;
212             }
213             return super.createSecondaryEntry(multiDataObject, fileObject);
214         }
215
216         public final class FormKitDataObject extends JavaDO {
217             FileEntry formEntry;
218
219             public FormKitDataObject(FileObject ffo, FileObject jfo, FormKitDataLoader loader) throws DataObjectExistsException, IOException JavaDoc
220             {
221                 super(jfo, loader);
222                 formEntry = (FileEntry)registerEntry(ffo);
223
224                 CookieSet cookies = getCookieSet();
225                 //cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));
226
}
227
228
229         }
230     }
231
232 }
233
Popular Tags