KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > systemoptions > BasicTestForImport


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.upgrade.systemoptions;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31
32 /**
33  * @author Radek Matous
34  */

35 public abstract class BasicTestForImport extends NbTestCase {
36     private FileObject f;
37     private String JavaDoc fileName;
38     
39     
40     public BasicTestForImport(String JavaDoc testName, String JavaDoc fileName) {
41         super(testName);
42         this.fileName = fileName;
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         URL JavaDoc u = getClass().getResource(getFileName());
47         File JavaDoc ff = new File JavaDoc(u.getFile());//getDataDir(),getFileName()
48
f = FileUtil.toFileObject(ff);
49         assert f != null;
50     }
51     
52     private final String JavaDoc getFileName() {
53         return fileName;
54     }
55     
56
57     /**
58      * overload this test in your TestCase see <code>IDESettingsTest</code>
59      */

60     public void testPropertyNames() throws Exception JavaDoc {
61         assertPropertyNames(new String JavaDoc[] {"just_cause_fail"
62         });
63     }
64     
65     public void testPreferencesNodePath() throws Exception JavaDoc {
66         assertPreferencesNodePath("just_cause_fail");
67     }
68     
69     
70     private DefaultResult readSystemOption(boolean types) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
71         return SystemOptionsParser.parse(f, types);
72     }
73
74     public void assertPropertyNames(final String JavaDoc[] propertyNames) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
75         assertEquals(new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(propertyNames)).toString(),
76                 new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(readSystemOption(false).getPropertyNames())).toString());
77     }
78     
79     public void assertProperty(final String JavaDoc propertyName, final String JavaDoc expected) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
80         Result support = readSystemOption(false);
81         
82         List JavaDoc parsedPropNames = Arrays.asList(support.getPropertyNames());
83         
84         String JavaDoc parsedPropertyName = null;
85         boolean isFakeName = !parsedPropNames.contains(propertyName);
86         if (isFakeName) {
87             assertTrue(propertyName+" (alias: "+parsedPropertyName + ") not found in: " + parsedPropNames,parsedPropNames.contains(parsedPropertyName));
88         } else {
89             parsedPropertyName = propertyName;
90         }
91         
92         assertNotNull(parsedPropertyName);
93         Class JavaDoc expectedClass = null;
94         String JavaDoc actual = support.getProperty(parsedPropertyName);
95         if (actual == null) {
96             assertNull(expectedClass);
97             assertEquals(expected, actual);
98         } else {
99             assertEquals(expected, actual);
100         }
101     }
102     
103     public void assertPropertyType(final String JavaDoc propertyName, final String JavaDoc expected) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
104         Result support = readSystemOption(true);
105         List JavaDoc parsedPropNames = Arrays.asList(support.getPropertyNames());
106         String JavaDoc parsedPropertyName = null;
107         boolean isFakeName = !parsedPropNames.contains(propertyName);
108         if (isFakeName) {
109             assertTrue(propertyName+" (alias: "+parsedPropertyName + ") not found in: " + parsedPropNames,parsedPropNames.contains(parsedPropertyName));
110         } else {
111             parsedPropertyName = propertyName;
112         }
113         
114         assertNotNull(parsedPropertyName);
115         String JavaDoc actual = support.getProperty(parsedPropertyName);
116         if (actual == null) {
117             assertNull(expected);
118         } else {
119             Class JavaDoc expectedClass = null;
120             try {
121                 expectedClass = Class.forName(expected);
122             } catch (ClassNotFoundException JavaDoc ex) {
123             }
124             if (expectedClass != null) {
125                 Class JavaDoc cls = Class.forName(actual);
126                 assertTrue(expectedClass + " but : " + cls,expectedClass.isAssignableFrom(cls));
127             } else {
128                 assertEquals(expected, actual);
129             }
130             assertEquals(expected, actual);
131         }
132     }
133     
134     public void assertPropertyTypeAndValue(String JavaDoc propertyName, String JavaDoc expectedType, String JavaDoc expectedValue) throws Exception JavaDoc {
135         assertPropertyType(propertyName, expectedType);
136         assertProperty(propertyName, expectedValue);
137     }
138     
139     public void assertPreferencesNodePath(final String JavaDoc expectedInstanceName) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
140         DefaultResult support = readSystemOption(true);
141         assertEquals(expectedInstanceName,"/"+support.getModuleName());//NOI18N
142
}
143 }
144
Popular Tags