KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > mimelookup > MimePathTest


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.netbeans.api.editor.mimelookup;
21
22 import org.netbeans.junit.NbTestCase;
23
24
25 /**
26  * Testing basic functionality of MimePath
27  *
28  * @author Martin Roskanin
29  */

30 public class MimePathTest extends NbTestCase {
31
32     public MimePathTest(java.lang.String JavaDoc testName) {
33         super(testName);
34     }
35
36     public void testParsing(){
37         String JavaDoc path = "text/x-java/text/x-ant+xml/text/html/text/xml";
38         MimePath mp = MimePath.parse(path);
39         String JavaDoc parsedPath = mp.getPath();
40         assertTrue(path.equals(parsedPath));
41
42         int size = mp.size();
43         assertTrue(size == 4);
44         
45         String JavaDoc one = mp.getMimeType(0);
46         String JavaDoc two = mp.getMimeType(1);
47         String JavaDoc three = mp.getMimeType(2);
48         String JavaDoc four = mp.getMimeType(3);
49         
50         assertTrue("text/x-java".equals(one));
51         assertTrue("text/x-ant+xml".equals(two));
52         assertTrue("text/html".equals(three));
53         assertTrue("text/xml".equals(four));
54         
55         MimePath mpPrefix = mp.getPrefix(2);
56         assertTrue("text/x-java/text/x-ant+xml".equals(mpPrefix.getPath()));
57     }
58     
59     public void testMimeTypeCorrectnessCheck() {
60         String JavaDoc [] valid = new String JavaDoc [] {
61             "text/plain",
62             "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$&.+-^_/abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!#$&.+-^_"
63         };
64         String JavaDoc [] invalid = new String JavaDoc [] {
65             "/",
66             "text",
67             "text//aaa",
68             "text@aaa",
69             "text/aaa/bb",
70             "text/ aaa",
71         };
72
73         // Check an empty mime type.
74
// This is an exception to the mime type specs, we allow empty mime types
75
// and they denote MimePath.EMPTY
76
{
77             MimePath mimePath = MimePath.get("");
78             assertNotNull("MimePath should not be null", mimePath);
79             assertEquals("Wrong MimePath size", 0, mimePath.size());
80             assertSame("Wrong empty MimePath", MimePath.EMPTY, mimePath);
81         }
82         
83         // Check valid mime types
84
for(String JavaDoc mimeType : valid) {
85             MimePath mimePath = MimePath.get(mimeType);
86             assertNotNull("MimePath should not be null", mimePath);
87             assertEquals("Wrong MimePath size", 1, mimePath.size());
88             assertEquals("Wrong mime type", mimeType, mimePath.getMimeType(0));
89         }
90         
91         // Check invalid mime types
92
for(String JavaDoc mimeType : invalid) {
93             try {
94                 MimePath mimePath = MimePath.get(mimeType);
95                 fail("Should not create MimePath for an invalid mime type: '" + mimeType + "'");
96             } catch (IllegalArgumentException JavaDoc iae) {
97                 // passed
98
}
99         }
100     }
101 }
102
Popular Tags