001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package java.awt.datatransfer;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.Reader;
025: import java.io.Serializable;
026: import java.nio.ByteBuffer;
027: import java.nio.CharBuffer;
028:
029: import junit.framework.TestCase;
030:
031: public class DataFlavorTest extends TestCase {
032:
033: public final void testHashCode() {
034: assertEquals(new DataFlavor("x/y; class=java.util.LinkedList",
035: "").hashCode(), new DataFlavor(
036: "x/y; class=java.util.LinkedList", "").hashCode());
037: assertTrue(new DataFlavor("z/y; class=java.util.LinkedList", "")
038: .hashCode() != new DataFlavor(
039: "x/y; class=java.util.LinkedList", "").hashCode());
040: assertTrue(new DataFlavor("x/z; class=java.util.LinkedList", "")
041: .hashCode() != new DataFlavor(
042: "x/y; class=java.util.LinkedList", "").hashCode());
043: assertTrue(new DataFlavor("x/y; class=java.util.LinkedList", "")
044: .hashCode() != new DataFlavor(
045: "x/y; class=java.util.List", "").hashCode());
046: assertTrue(new DataFlavor(
047: "x/y; class=java.lang.String; charset=c1", "")
048: .hashCode() == new DataFlavor(
049: "x/y; class=java.lang.String; charset=c2", "")
050: .hashCode());
051: assertTrue(new DataFlavor(
052: "x/y; class=java.util.LinkedList; charset=c1", "")
053: .hashCode() != new DataFlavor(
054: "x/y; class=java.util.List; charset=c2", "").hashCode());
055: }
056:
057: /*
058: * Class under test for boolean equals(Object)
059: */
060: public final void testEqualsObject() {
061: assertTrue(new DataFlavor("x/y; class=java.util.LinkedList", "")
062: .equals((Object) new DataFlavor(
063: "x/y; class=java.util.LinkedList", "")));
064: assertTrue(!new DataFlavor("z/y; class=java.util.LinkedList",
065: "").equals((Object) new DataFlavor(
066: "x/y; class=java.util.LinkedList", "")));
067: assertTrue(!new DataFlavor("x/z; class=java.util.LinkedList",
068: "").equals((Object) new DataFlavor(
069: "x/y; class=java.util.LinkedList", "")));
070: assertTrue(!new DataFlavor("x/y; class=java.util.LinkedList",
071: "").equals((Object) new DataFlavor(
072: "x/y; class=java.util.List", "")));
073: assertTrue(new DataFlavor(
074: "x/y; class=java.lang.String; charset=c1", "")
075: .equals((Object) new DataFlavor(
076: "x/y; class=java.lang.String; charset=c2", "")));
077: assertTrue(!new DataFlavor(
078: "x/y; class=java.util.LinkedList; charset=c1", "")
079: .equals((Object) new DataFlavor(
080: "x/y; class=java.util.List; charset=c2", "")));
081: assertTrue(!new DataFlavor("z/y; class=java.util.LinkedList",
082: "").equals(new RuntimeException()));
083:
084: // Regression for HARMONY-2033
085: assertFalse(new DataFlavor().equals("")); //$NON-NLS-1$
086: }
087:
088: /*
089: * Class under test for boolean equals(DataFlavor)
090: */
091: public final void testEqualsDataFlavor() {
092: assertTrue(new DataFlavor("x/y; class=java.util.LinkedList", "")
093: .equals(new DataFlavor(
094: "x/y; class=java.util.LinkedList", "")));
095: assertTrue(!new DataFlavor("z/y; class=java.util.LinkedList",
096: "").equals(new DataFlavor(
097: "x/y; class=java.util.LinkedList", "")));
098: assertTrue(!new DataFlavor("x/z; class=java.util.LinkedList",
099: "").equals(new DataFlavor(
100: "x/y; class=java.util.LinkedList", "")));
101: assertTrue(!new DataFlavor("x/y; class=java.util.LinkedList",
102: "").equals(new DataFlavor("x/y; class=java.util.List",
103: "")));
104: assertTrue(new DataFlavor(
105: "x/y; class=java.lang.String; charset=c1", "")
106: .equals(new DataFlavor(
107: "x/y; class=java.lang.String; charset=c2", "")));
108: assertTrue(!new DataFlavor(
109: "x/y; class=java.util.LinkedList; charset=c1", "")
110: .equals(new DataFlavor(
111: "x/y; class=java.util.List; charset=c2", "")));
112: }
113:
114: public final void testMatch() {
115: assertTrue(new DataFlavor("x/y; class=java.util.LinkedList", "")
116: .match(new DataFlavor(
117: "x/y; class=java.util.LinkedList", "")));
118: assertTrue(!new DataFlavor("z/y; class=java.util.LinkedList",
119: "").match(new DataFlavor(
120: "x/y; class=java.util.LinkedList", "")));
121: assertTrue(!new DataFlavor("x/z; class=java.util.LinkedList",
122: "").match(new DataFlavor(
123: "x/y; class=java.util.LinkedList", "")));
124: assertTrue(!new DataFlavor("x/y; class=java.util.LinkedList",
125: "").match(new DataFlavor("x/y; class=java.util.List",
126: "")));
127: assertTrue(new DataFlavor(
128: "x/y; class=java.lang.String; charset=c1", "")
129: .match(new DataFlavor(
130: "x/y; class=java.lang.String; charset=c2", "")));
131: assertTrue(!new DataFlavor(
132: "x/y; class=java.util.LinkedList; charset=c1", "")
133: .match(new DataFlavor(
134: "x/y; class=java.util.List; charset=c2", "")));
135: }
136:
137: public final void testTryToLoadClass() {
138: try {
139: DataFlavor.tryToLoadClass("java.lang.Thread", null);
140: assertTrue(true);
141: } catch (ClassNotFoundException e) {
142: fail();
143: }
144: }
145:
146: public final void testGetTextPlainUnicodeFlavor() {
147: String charset = null;
148: String osName = System.getProperty("os.name").toLowerCase();
149:
150: if (osName.startsWith("linux")) {
151: charset = "iso-10646-ucs-2";
152: }
153: if (osName.startsWith("windows")) {
154: charset = "utf-16le";
155: }
156:
157: assertEquals(DataFlavor.getTextPlainUnicodeFlavor(),
158: new DataFlavor("text/plain; charset=" + charset, ""));
159: }
160:
161: /*
162: * Class under test for void DataFlavor(Class, String)
163: */
164: public final void testDataFlavorClassString() {
165: assertEquals(
166: new DataFlavor(String.class, "z"),
167: new DataFlavor(
168: "application/x-java-serialized-object; class=java.lang.String",
169: "z"));
170: assertEquals(
171: new DataFlavor(String.class, null),
172: new DataFlavor(
173: "application/x-java-serialized-object; class=java.lang.String",
174: "application/x-java-serialized-object"));
175: }
176:
177: /*
178: * Class under test for void DataFlavor(String, String)
179: */
180: public final void testDataFlavorStringString() {
181: assertEquals(new DataFlavor("x/y; param=value", "z")
182: .getParameter("param"), "value");
183: assertEquals(new DataFlavor("x/y; param=value", "z")
184: .getRepresentationClass(), java.io.InputStream.class);
185: assertEquals(
186: new DataFlavor(
187: "application/x-java-serialized-object; class=java.lang.String",
188: null),
189: new DataFlavor(
190: "application/x-java-serialized-object; class=java.lang.String",
191: "application/x-java-serialized-object"));
192: assertEquals(new DataFlavor("x/y; class=java.lang.String", "z")
193: .getRepresentationClass(), String.class);
194:
195: try {
196: new DataFlavor("x/y class=java.lang.String", "z");
197: fail();
198: } catch (IllegalArgumentException e) {
199: assertTrue(true);
200: }
201: }
202:
203: /*
204: * Class under test for void DataFlavor(String)
205: */
206: public final void testDataFlavorString() {
207: try {
208: assertEquals(new DataFlavor("x/y; param=value")
209: .getParameter("param"), "value");
210: assertEquals(new DataFlavor("x/y; param=value")
211: .getRepresentationClass(),
212: java.io.InputStream.class);
213: assertEquals(
214: new DataFlavor(
215: "application/x-java-serialized-object; class=java.lang.String"),
216: new DataFlavor(
217: "application/x-java-serialized-object; class=java.lang.String",
218: "application/x-java-serialized-object"));
219: assertEquals(new DataFlavor("x/y; class=java.lang.String")
220: .getRepresentationClass(), String.class);
221: } catch (ClassNotFoundException e1) {
222: fail();
223: }
224:
225: try {
226: new DataFlavor("x/y class=java.lang.String", "z");
227: fail();
228: } catch (IllegalArgumentException e) {
229: assertTrue(true);
230: }
231:
232: try {
233: new DataFlavor("x/y; class=java.lang.Strng");
234: fail();
235: } catch (ClassNotFoundException e) {
236: assertTrue(true);
237: }
238: }
239:
240: /*
241: * Class under test for void DataFlavor(String, String, ClassLoader)
242: */
243: public final void testDataFlavorStringStringClassLoader() {
244: try {
245: assertEquals(new DataFlavor("x/y; param=value", "z", Thread
246: .currentThread().getContextClassLoader())
247: .getParameter("param"), "value");
248: assertEquals(new DataFlavor("x/y; param=value", "z", Thread
249: .currentThread().getContextClassLoader())
250: .getRepresentationClass(),
251: java.io.InputStream.class);
252: assertEquals(
253: new DataFlavor(
254: "application/x-java-serialized-object; class=java.lang.String",
255: null, Thread.currentThread()
256: .getContextClassLoader()),
257: new DataFlavor(
258: "application/x-java-serialized-object; class=java.lang.String",
259: "application/x-java-serialized-object"));
260: assertEquals(
261: new DataFlavor("x/y; class=java.lang.String", "z",
262: Thread.currentThread()
263: .getContextClassLoader())
264: .getRepresentationClass(), String.class);
265: } catch (ClassNotFoundException e1) {
266: fail();
267: }
268:
269: try {
270: new DataFlavor("x/y class=java.lang.String", "z");
271: fail();
272: } catch (IllegalArgumentException e) {
273: assertTrue(true);
274: }
275: }
276:
277: public final void testGetPrimaryType() {
278: assertEquals(new DataFlavor("x/y; class=java.lang.String", "z")
279: .getPrimaryType(), "x");
280: }
281:
282: public final void testGetSubType() {
283: assertEquals(new DataFlavor("x/y; class=java.lang.String", "z")
284: .getSubType(), "y");
285: }
286:
287: public final void testGetMimeType() {
288: assertEquals(new DataFlavor("x/y; param=value", "z")
289: .getMimeType(),
290: "x/y; class=\"java.io.InputStream\"; param=\"value\"");
291: }
292:
293: public final void testGetParameter() {
294: assertEquals(new DataFlavor("x/y; param=value", "z")
295: .getParameter("param"), "value");
296: assertEquals(new DataFlavor("x/y; param=value", "z")
297: .getParameter("humanPresentableName"), "z");
298:
299: // Regression for HARMONY-2033
300: assertNull(new DataFlavor().getParameter("")); //$NON-NLS-1$
301: }
302:
303: public final void testGetHumanPresentableName() {
304: DataFlavor flavor = new DataFlavor("x/y; param=value", "z");
305:
306: assertEquals(flavor.getHumanPresentableName(), "z");
307: flavor.setHumanPresentableName("zz");
308: assertEquals(flavor.getHumanPresentableName(), "zz");
309: }
310:
311: public final void testSetHumanPresentableName() {
312: testGetHumanPresentableName();
313: }
314:
315: public final void testGetRepresentationClass() {
316: assertEquals(new DataFlavor("x/y; class=java.io.Reader", "z")
317: .getRepresentationClass(), java.io.Reader.class);
318: }
319:
320: public final void testGetDefaultRepresentationClass() {
321: assertEquals(new DataFlavor("x/y; param=value", "z")
322: .getDefaultRepresentationClass(),
323: java.io.InputStream.class);
324: }
325:
326: public final void testGetDefaultRepresentationClassAsString() {
327: assertEquals(new DataFlavor("x/y; param=value", "z")
328: .getDefaultRepresentationClassAsString(),
329: "java.io.InputStream");
330: }
331:
332: public final void testIsRepresentationClassSerializable() {
333: assertTrue(new DataFlavor(Serializable.class, "")
334: .isRepresentationClassSerializable());
335: assertFalse(new DataFlavor(Object.class, "")
336: .isRepresentationClassSerializable());
337: }
338:
339: // Enable this test when RMI is supported
340: // public final void testIsRepresentationClassRemote() {
341: // assertTrue(new DataFlavor(Remote.class, "").
342: // isRepresentationClassRemote());
343: // assertFalse(new DataFlavor(Object.class, "").
344: // isRepresentationClassRemote());
345: // }
346:
347: public final void testIsRepresentationClassReader() {
348: assertTrue(new DataFlavor(new Reader() {
349: @Override
350: public void close() throws IOException {
351: }
352:
353: @Override
354: public int read(char[] arg0, int arg1, int arg2)
355: throws IOException {
356: return 0;
357: }
358: }.getClass(), "").isRepresentationClassReader());
359: assertFalse(new DataFlavor(Object.class, "")
360: .isRepresentationClassReader());
361: }
362:
363: public final void testIsRepresentationClassInputStream() {
364: assertTrue(new DataFlavor(new InputStream() {
365: @Override
366: public int read() throws IOException {
367: return 0;
368: }
369: }.getClass(), "").isRepresentationClassInputStream());
370: assertFalse(new DataFlavor(Object.class, "")
371: .isRepresentationClassInputStream());
372: }
373:
374: public final void testIsRepresentationClassCharBuffer() {
375: assertTrue(new DataFlavor(CharBuffer.class, "")
376: .isRepresentationClassCharBuffer());
377: assertFalse(new DataFlavor(Object.class, "")
378: .isRepresentationClassCharBuffer());
379: }
380:
381: public final void testIsRepresentationClassByteBuffer() {
382: assertTrue(new DataFlavor(ByteBuffer.class, "")
383: .isRepresentationClassByteBuffer());
384: assertFalse(new DataFlavor(Object.class, "")
385: .isRepresentationClassByteBuffer());
386: }
387:
388: public final void testIsMimeTypeSerializedObject() {
389: assertTrue(new DataFlavor(Serializable.class, "")
390: .isMimeTypeSerializedObject());
391: assertFalse(new DataFlavor("x/y", "")
392: .isMimeTypeSerializedObject());
393: }
394:
395: // Enable this test when RMI is supported
396: // public final void testIsFlavorSerializedObjectType() {
397: // assertTrue(new DataFlavor(Serializable.class, "").
398: // isFlavorSerializedObjectType());
399: // assertFalse(new DataFlavor("x/y; class=java.io.Serializable", "").
400: // isFlavorSerializedObjectType());
401: //
402: // assertFalse(new DataFlavor(Remote.class, "").
403: // isFlavorSerializedObjectType());
404: // }
405:
406: // Enable this test when RMI is supported
407: // public final void testIsFlavorRemoteObjectType() {
408: // assertTrue(new DataFlavor(DataFlavor.javaRemoteObjectMimeType + "; class=java.rmi.Remote", "").
409: // isFlavorRemoteObjectType());
410: // assertFalse(new DataFlavor(Serializable.class, "").
411: // isFlavorRemoteObjectType());
412: // assertFalse(new DataFlavor("x/y; class=java.rmi.Remote", "").
413: // isFlavorRemoteObjectType());
414: // }
415:
416: public final void testIsFlavorJavaFileListType() {
417: assertTrue(new DataFlavor(
418: "application/x-java-file-list; class=java.util.LinkedList",
419: "").isFlavorJavaFileListType());
420: assertFalse(new DataFlavor(
421: "application/x-java-file; class=java.util.LinkedList",
422: "").isFlavorJavaFileListType());
423: assertFalse(new DataFlavor("application/x-java-file-list", "")
424: .isFlavorJavaFileListType());
425: }
426:
427: public final void testIsFlavorTextType() {
428: assertTrue(DataFlavor.stringFlavor.isFlavorTextType());
429: assertTrue(new DataFlavor("text/z", "").isFlavorTextType());
430: assertFalse(new DataFlavor("text/z; charset=dummy", "")
431: .isFlavorTextType());
432: }
433:
434: /*
435: * Class under test for boolean isMimeTypeEqual(DataFlavor)
436: */
437: public final void testIsMimeTypeEqualDataFlavor() {
438: assertTrue(new DataFlavor("x/y; class=java.io.Serializable",
439: "1").isMimeTypeEqual(new DataFlavor("x/y; param=value",
440: "2")));
441: assertFalse(new DataFlavor("x/y; class=java.io.Serializable",
442: "1").isMimeTypeEqual(new DataFlavor("z/y; param=value",
443: "2")));
444: assertFalse(new DataFlavor("x/y; class=java.io.Serializable",
445: "1").isMimeTypeEqual(new DataFlavor("x/z; param=value",
446: "2")));
447:
448: // Regression for HARMONY-2033
449: assertTrue(new DataFlavor().isMimeTypeEqual(new DataFlavor()));
450: }
451:
452: /*
453: * Class under test for boolean isMimeTypeEqual(String)
454: */
455: public final void testIsMimeTypeEqualString() {
456: assertTrue(new DataFlavor("x/y; class=java.io.Serializable",
457: "1").isMimeTypeEqual("x/y; param=value"));
458: assertFalse(new DataFlavor("x/y; class=java.io.Serializable",
459: "1").isMimeTypeEqual("z/y; param=value"));
460: assertFalse(new DataFlavor("x/y; class=java.io.Serializable",
461: "1").isMimeTypeEqual("x/z; param=value"));
462:
463: // Regression for HARMONY-2033
464: assertFalse(new DataFlavor().isMimeTypeEqual("")); //$NON-NLS-1$
465: }
466:
467: /*
468: * Class under test for Object clone()
469: */
470: public final void testClone() throws CloneNotSupportedException {
471: DataFlavor flavor = new DataFlavor(
472: "x/y; class=java.io.Serializable; param=value", "z");
473: DataFlavor clone = (DataFlavor) flavor.clone();
474:
475: assertEquals(flavor, clone);
476: assertEquals(flavor.getParameter("param"), clone
477: .getParameter("param"));
478: assertEquals(flavor.getHumanPresentableName(), clone
479: .getHumanPresentableName());
480: }
481:
482: public final void testSelectBestTextFlavor() {
483: assertEquals(DataFlavor.selectBestTextFlavor(new DataFlavor[] {
484: new DataFlavor("x/y", ""), DataFlavor.stringFlavor }),
485: DataFlavor.stringFlavor);
486:
487: assertEquals(DataFlavor.selectBestTextFlavor(new DataFlavor[] {
488: new DataFlavor("text/xml", ""),
489: new DataFlavor("text/plain", "") }), new DataFlavor(
490: "text/xml", ""));
491:
492: assertEquals(DataFlavor.selectBestTextFlavor(new DataFlavor[] {
493: new DataFlavor("text/xml; class=java.lang.String", ""),
494: new DataFlavor("text/xml; class=java.nio.CharBuffer",
495: "") }), new DataFlavor(
496: "text/xml; class=java.lang.String", ""));
497:
498: assertEquals(
499: DataFlavor
500: .selectBestTextFlavor(new DataFlavor[] {
501: new DataFlavor(
502: "text/xml; class=java.lang.String; charset=US-ASCII",
503: ""),
504: new DataFlavor(
505: "text/xml; class=java.lang.String; charset=UTF-16",
506: "") }),
507: new DataFlavor(
508: "text/xml; class=java.lang.String; charset=UTF-16",
509: ""));
510:
511: // Regression for HARMONY-2033
512: assertNull(DataFlavor
513: .selectBestTextFlavor(new DataFlavor[] { new DataFlavor() }));
514: }
515:
516: @SuppressWarnings("deprecation")
517: public void testHarmony1477Regression() {
518: // Regression for HARMONY-1477
519: DataFlavor df = new DataFlavor();
520: try {
521: assertFalse(df.equals(""));
522: assertFalse(df.isMimeTypeEqual(""));
523: } catch (IllegalArgumentException iae) {
524: fail("Regression test failed");
525: }
526: }
527:
528: public void testCloneNullMimeInfo()
529: throws CloneNotSupportedException, NullPointerException {
530: // Regression for HARMONY-2069
531: DataFlavor df = new DataFlavor();
532: df.clone();
533: }
534: }
|