Line 0
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 |
package org.openide.text; |
45 |
|
46 |
import java.io.IOException; |
47 |
import java.io.OutputStream; |
48 |
|
49 |
|
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.openide.cookies.EditCookie; |
52 |
|
53 |
import org.openide.cookies.OpenCookie; |
54 |
import org.openide.filesystems.FileLock; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.loaders.DataObject; |
57 |
import org.openide.loaders.DataObjectExistsException; |
58 |
import org.openide.loaders.MultiDataObject; |
59 |
import org.openide.nodes.CookieSet; |
60 |
|
61 |
/** |
62 |
*/ |
63 |
public class FileSizeThreshholdExceptionTest extends NbTestCase { |
64 |
|
65 |
private final String BIG_FILE_PROP = "org.openide.text.big.file.size"; |
66 |
|
67 |
static { |
68 |
System.setProperty("org.openide.windows.DummyWindowManager.VISIBLE", "false"); |
69 |
} |
70 |
// for file object support |
71 |
String content = ""; |
72 |
long expectedSize = -1; |
73 |
java.util.Date date = new java.util.Date(); |
74 |
FileObject fileObject; |
75 |
org.openide.filesystems.FileSystem fs; |
76 |
static FileSizeThreshholdExceptionTest RUNNING; |
77 |
|
78 |
static { |
79 |
System.setProperty("org.openide.util.Lookup", "org.openide.text.FileSizeThreshholdExceptionTest$Lkp"); |
80 |
} |
81 |
|
82 |
public FileSizeThreshholdExceptionTest(String s) { |
83 |
super(s); |
84 |
} |
85 |
|
86 |
@Override |
87 |
protected void setUp() throws Exception { |
88 |
RUNNING = this; |
89 |
|
90 |
fs = org.openide.filesystems.FileUtil.createMemoryFileSystem(); |
91 |
org.openide.filesystems.Repository.getDefault().addFileSystem(fs); |
92 |
org.openide.filesystems.FileObject root = fs.getRoot(); |
93 |
fileObject = org.openide.filesystems.FileUtil.createData(root, "my.obj"); |
94 |
} |
95 |
|
96 |
@Override |
97 |
protected void tearDown() throws Exception { |
98 |
waitEQ(); |
99 |
|
100 |
RUNNING = null; |
101 |
org.openide.filesystems.Repository.getDefault().removeFileSystem(fs); |
102 |
} |
103 |
|
104 |
@Override |
105 |
protected boolean runInEQ() { |
106 |
return false; |
107 |
} |
108 |
|
109 |
private void waitEQ() throws Exception { |
110 |
javax.swing.SwingUtilities.invokeAndWait(new Runnable() { |
111 |
@Override |
112 |
public void run() { |
113 |
} |
114 |
}); |
115 |
} |
116 |
|
117 |
DES support() throws Exception { |
118 |
DataObject obj = DataObject.find(fileObject); |
119 |
|
120 |
assertEquals("My object was created", MyDataObject.class, obj.getClass()); |
121 |
Object cookie = obj.getLookup().lookup(org.openide.cookies.OpenCookie.class); |
122 |
assertNotNull("Our object has this cookie", cookie); |
123 |
assertEquals("It is my cookie", DES.class, cookie.getClass()); |
124 |
|
125 |
return (DES) cookie; |
126 |
} |
127 |
|
128 |
public void doTestThreshholdValue(int sizeInMb) throws Exception { |
129 |
final int size = 1024 * 1024 * sizeInMb; |
130 |
DES des = support(); |
131 |
DataEditorSupport.Env env = des.getEnv(); |
132 |
OutputStream out = fileObject.getOutputStream(); |
133 |
out.write(new byte[size + 1]); |
134 |
out.close(); |
135 |
try { |
136 |
env.inputStream(); |
137 |
assertTrue("File size is greater than limit, but no exception appeared", false); |
138 |
} catch (IOException ex) { |
139 |
// ignoring, as this is expected exception |
140 |
} |
141 |
out = fileObject.getOutputStream(); |
142 |
out.write(new byte[size]); |
143 |
out.close(); |
144 |
try { |
145 |
env.inputStream(); |
146 |
} catch (IOException ex) { |
147 |
assertTrue("File size is lower than limit, but exception appeared", false); |
148 |
} |
149 |
} |
150 |
|
151 |
public void testThreshholdDefaultValue() throws Exception { |
152 |
System.getProperties().remove(BIG_FILE_PROP); |
153 |
doTestThreshholdValue(1); |
154 |
} |
155 |
|
156 |
public void testThreshholdNotDefaultValue() throws Exception { |
157 |
final int SIZE = 2; |
158 |
System.setProperty(BIG_FILE_PROP, "" + SIZE); |
159 |
doTestThreshholdValue(SIZE); |
160 |
} |
161 |
|
162 |
/** |
163 |
* Implementation of the DES |
164 |
*/ |
165 |
private static final class DES extends DataEditorSupport |
166 |
implements OpenCookie, EditCookie { |
167 |
|
168 |
private final Env env; |
169 |
|
170 |
public DES(DataObject obj, Env env) { |
171 |
super(obj, env); |
172 |
this.env = env; |
173 |
} |
174 |
|
175 |
public Env getEnv() { |
176 |
return env; |
177 |
} |
178 |
} |
179 |
|
180 |
/** |
181 |
* MyEnv that uses DataEditorSupport.Env |
182 |
*/ |
183 |
private static final class MyEnv extends DataEditorSupport.Env { |
184 |
|
185 |
static final long serialVersionUID = 1L; |
186 |
|
187 |
public MyEnv(MyDataObject obj) { |
188 |
super(obj); |
189 |
} |
190 |
|
191 |
@Override |
192 |
protected FileObject getFile() { |
193 |
return super.getDataObject().getPrimaryFile(); |
194 |
} |
195 |
|
196 |
@Override |
197 |
protected FileLock takeLock() throws IOException { |
198 |
MyDataObject my = (MyDataObject) getDataObject(); |
199 |
return my.getPrimaryEntry().takeLock(); |
200 |
} |
201 |
} |
202 |
|
203 |
public static final class Lkp extends org.openide.util.lookup.AbstractLookup { |
204 |
|
205 |
public Lkp() { |
206 |
this(new org.openide.util.lookup.InstanceContent()); |
207 |
} |
208 |
|
209 |
private Lkp(org.openide.util.lookup.InstanceContent ic) { |
210 |
super(ic); |
211 |
|
212 |
ic.add(new Pool()); |
213 |
} |
214 |
} // end of Lkp |
215 |
|
216 |
private static final class Pool extends org.openide.loaders.DataLoaderPool { |
217 |
|
218 |
@Override |
219 |
protected java.util.Enumeration loaders() { |
220 |
return org.openide.util.Enumerations.singleton(MyLoader.get()); |
221 |
} |
222 |
} |
223 |
|
224 |
public static final class MyLoader extends org.openide.loaders.UniFileLoader { |
225 |
|
226 |
public int primary; |
227 |
|
228 |
public static MyLoader get() { |
229 |
return MyLoader.findObject(MyLoader.class, true); |
230 |
} |
231 |
|
232 |
public MyLoader() { |
233 |
super(MyDataObject.class.getName()); |
234 |
getExtensions().addExtension("obj"); |
235 |
} |
236 |
|
237 |
protected String displayName() { |
238 |
return "MyPart"; |
239 |
} |
240 |
|
241 |
@Override |
242 |
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { |
243 |
return new MyDataObject(this, primaryFile); |
244 |
} |
245 |
|
246 |
@Override |
247 |
protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { |
248 |
primary++; |
249 |
return new org.openide.loaders.FileEntry(obj, primaryFile); |
250 |
} |
251 |
} |
252 |
|
253 |
public static final class MyDataObject extends MultiDataObject |
254 |
implements CookieSet.Factory { |
255 |
|
256 |
public MyDataObject(MyLoader l, FileObject folder) throws DataObjectExistsException { |
257 |
super(folder, l); |
258 |
getCookieSet().add(OpenCookie.class, this); |
259 |
} |
260 |
|
261 |
@Override |
262 |
public org.openide.nodes.Node.Cookie createCookie(Class klass) { |
263 |
return new DES(this, new MyEnv(this)); |
264 |
} |
265 |
} |
266 |
} |