Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.openide.loaders; |
46 |
|
47 |
|
48 |
import org.openide.filesystems.*; |
49 |
import org.netbeans.junit.*; |
50 |
import java.io.IOException; |
51 |
import java.util.*; |
52 |
import java.util.logging.Level; |
53 |
import org.openide.*; |
54 |
import org.openide.cookies.CloseCookie; |
55 |
import org.openide.cookies.EditCookie; |
56 |
import org.openide.cookies.EditorCookie; |
57 |
import org.openide.cookies.LineCookie; |
58 |
import org.openide.cookies.OpenCookie; |
59 |
import org.openide.cookies.PrintCookie; |
60 |
import org.openide.nodes.Node; |
61 |
import org.openide.util.Enumerations; |
62 |
|
63 |
public class MultiDataObjectVersion1Test extends NbTestCase { |
64 |
FileSystem fs; |
65 |
DataObject one; |
66 |
DataFolder from; |
67 |
DataFolder to; |
68 |
ErrorManager err; |
69 |
|
70 |
|
71 |
public MultiDataObjectVersion1Test (String name) { |
72 |
super (name); |
73 |
} |
74 |
|
75 |
@Override |
76 |
protected Level logLevel() { |
77 |
return Level.INFO; |
78 |
} |
79 |
|
80 |
@Override |
81 |
protected int timeOut() { |
82 |
return 45000; |
83 |
} |
84 |
|
85 |
@Override |
86 |
public void setUp() throws Exception { |
87 |
clearWorkDir(); |
88 |
|
89 |
super.setUp(); |
90 |
|
91 |
MockServices.setServices(Pool.class); |
92 |
|
93 |
err = ErrorManager.getDefault().getInstance("TEST-" + getName()); |
94 |
|
95 |
LocalFileSystem lfs = new LocalFileSystem(); |
96 |
lfs.setRootDirectory(getWorkDir()); |
97 |
fs = lfs; |
98 |
FileUtil.createData(fs.getRoot(), "from/x.prima"); |
99 |
FileUtil.createData(fs.getRoot(), "from/x.seconda"); |
100 |
FileUtil.createFolder(fs.getRoot(), "to/"); |
101 |
|
102 |
one = DataObject.find(fs.findResource("from/x.prima")); |
103 |
assertEquals(SimpleObject.class, one.getClass()); |
104 |
|
105 |
from = one.getFolder(); |
106 |
to = DataFolder.findFolder(fs.findResource("to/")); |
107 |
|
108 |
assertEquals("Nothing there", 0, to.getPrimaryFile().getChildren().length); |
109 |
} |
110 |
|
111 |
|
112 |
public void testWhatIsInTheLookup() throws Exception { |
113 |
assertTrue(this.one instanceof SimpleObject); |
114 |
SimpleObject s = (SimpleObject)this.one; |
115 |
|
116 |
assertEquals("DO in lookup", s, s.getLookup().lookup(DataObject.class)); |
117 |
assertEquals("Fo in lookup", s.getPrimaryFile(), s.getLookup().lookup(FileObject.class)); |
118 |
assertEquals("no node created yet", 0, s.cnt); |
119 |
Node lkpNd = s.getLookup().lookup(Node.class); |
120 |
assertEquals("now node created", 1, s.cnt); |
121 |
assertEquals("Node in lookup", s.getNodeDelegate(), lkpNd); |
122 |
} |
123 |
|
124 |
public void testEditorInLookup() throws Exception { |
125 |
assertTrue(this.one instanceof SimpleObject); |
126 |
SimpleObject s = (SimpleObject)this.one; |
127 |
|
128 |
LineCookie line = s.getLookup().lookup(LineCookie.class); |
129 |
assertNotNull("Line cookie is there", line); |
130 |
assertEquals("Edit cookie", line, s.getLookup().lookup(EditCookie.class)); |
131 |
assertEquals("Editor cookie", line, s.getLookup().lookup(EditorCookie.class)); |
132 |
assertEquals("Editor objservable cookie", line, s.getLookup().lookup(EditorCookie.Observable.class)); |
133 |
assertEquals("SaveAsCapable", line, s.getLookup().lookup(SaveAsCapable.class)); |
134 |
assertEquals("Open cookie", line, s.getLookup().lookup(OpenCookie.class)); |
135 |
assertEquals("Close cookie", line, s.getLookup().lookup(CloseCookie.class)); |
136 |
assertEquals("Print cookie", line, s.getLookup().lookup(PrintCookie.class)); |
137 |
} |
138 |
|
139 |
|
140 |
public static final class Pool extends DataLoaderPool { |
141 |
@Override |
142 |
protected Enumeration loaders() { |
143 |
return Enumerations.singleton(SimpleLoader.getLoader(SimpleLoader.class)); |
144 |
} |
145 |
} |
146 |
|
147 |
public static final class SimpleLoader extends MultiFileLoader { |
148 |
public SimpleLoader() { |
149 |
super(SimpleObject.class); |
150 |
} |
151 |
protected String displayName() { |
152 |
return "SimpleLoader"; |
153 |
} |
154 |
@Override |
155 |
protected FileObject findPrimaryFile(FileObject fo) { |
156 |
if (!fo.isFolder()) { |
157 |
// emulate the behaviour of form data object |
158 |
|
159 |
/* emulate!? this one is written too well ;-) |
160 |
FileObject primary = FileUtil.findBrother(fo, "prima"); |
161 |
FileObject secondary = FileUtil.findBrother(fo, "seconda"); |
162 |
|
163 |
if (primary == null || secondary == null) { |
164 |
return null; |
165 |
} |
166 |
|
167 |
if (primary != fo && secondary != fo) { |
168 |
return null; |
169 |
} |
170 |
*/ |
171 |
|
172 |
// here is the common code for the worse behaviour |
173 |
if (fo.hasExt("prima")) { |
174 |
return FileUtil.findBrother(fo, "seconda") != null ? fo : null; |
175 |
} |
176 |
|
177 |
if (fo.hasExt("seconda")) { |
178 |
return FileUtil.findBrother(fo, "prima"); |
179 |
} |
180 |
} |
181 |
return null; |
182 |
} |
183 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
184 |
return new SimpleObject(this, primaryFile); |
185 |
} |
186 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
187 |
return new FileEntry(obj, primaryFile); |
188 |
} |
189 |
protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { |
190 |
return new FileEntry(obj, secondaryFile); |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
public static final class SimpleObject extends MultiDataObject { |
196 |
private int cnt; |
197 |
public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { |
198 |
super(fo, l); |
199 |
registerEditor("mime/type", false); |
200 |
} |
201 |
|
202 |
@Override |
203 |
protected int associateLookup() { |
204 |
return 1; |
205 |
} |
206 |
|
207 |
@Override |
208 |
protected Node createNodeDelegate() { |
209 |
cnt++; |
210 |
return super.createNodeDelegate(); |
211 |
} |
212 |
|
213 |
|
214 |
} |
215 |
|
216 |
} |