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: /**
019: * @author Serguei S.Zapreyev
020: * @version $Revision$
021: */package java.lang;
022:
023: import junit.framework.TestCase;
024:
025: /*
026: * Created on 02.02.2006
027: *
028: * This PackageTest class is used to test the Core API java.lang.Package
029: * class
030: *
031: */
032:
033: public class PackageTest extends TestCase {
034:
035: static String vendor = System.getProperty("java.vm.vendor");
036:
037: /**
038: *
039: */
040: public void test_getImplementationTitle_V() {
041: //System.out.println("test_getImplementationTitle_V");
042: Package p = java.lang.Character.class.getPackage();
043: if (p == null)
044: return;
045: String s = p.getImplementationTitle();
046: if (s == null)
047: return;
048: if (vendor.equals("Sun Microsystems Inc.")) {
049: assertEquals("Error1: unexpected title:",
050: "Java Runtime Environment", s);
051: } else if (vendor.equals("BEA Systems, Inc.")) {
052: assertEquals("Error2: unexpected title:", "...", s);
053: } else if (vendor.equals("Apache Software Foundation")) {
054: assertEquals("Error3: unexpected title:", "Apache Harmony",
055: s);
056: }
057: }
058:
059: /**
060: *
061: */
062: public void test_getImplementationVendor_V() {
063: //System.out.println("test_getImplementationVendor_V");
064: Package p = java.lang.Character.class.getPackage();
065: if (p == null)
066: return;
067: String s = p.getImplementationVendor();
068: if (s == null)
069: return; // XXX:It's not explained by spec
070: if (vendor.equals("Sun Microsystems Inc.")) {
071: assertEquals("Error1: unexpected vendor:",
072: "Sun Microsystems, Inc.", s);
073: } else if (vendor.equals("BEA Systems, Inc.")) {
074: assertEquals("Error2: unexpected vendor:", "...", s);
075: } else if (vendor.equals("Apache Software Foundation")) {
076: assertEquals("Error3: unexpected vendor:",
077: "The Apache Software Foundation", s);
078: }
079: }
080:
081: /**
082: *
083: */
084: public void test_getImplementationVersion_V() {
085: //System.out.println("test_getImplementationVersion_V");
086: Package p = java.lang.Character.class.getPackage();
087: if (p == null)
088: return;
089: String s = p.getImplementationVersion();
090: if (s == null)
091: return;
092: assertTrue("unexpected implementation version: " + s, s
093: .matches("[[0-9]._]+"));
094: }
095:
096: /**
097: *
098: */
099: public void test_getName_V() {
100: //System.out.println("test_getName_V");
101: Package p = java.lang.Character.class.getPackage();
102: if (p == null)
103: return;
104: String s = p.getName();
105: assertEquals("unexpected name", "java.lang", s);
106: }
107:
108: /**
109: *
110: */
111: public void test_getPackage_Str_1() {
112: //System.out.println("test_getPackage_Str_1");
113: assertNull("null expected", Package
114: .getPackage("ABSOLUTELY.UNKNOWN.PACKAGE"));
115: }
116:
117: /**
118: *
119: */
120: public void test_getPackage_Str_2() {
121: //System.out.println("test_getPackage_Str_2");
122: try {
123: Package.getPackage((String) null);
124: fail("NullPointerException should be thrown");
125: } catch (NullPointerException _) {
126: return;
127: }
128: }
129:
130: /**
131: *
132: */
133: public void test_getPackage_Str_3() {
134: //System.out.println("test_getPackage_Str");
135: Package p = Package.getPackage("java.lang");
136: if (p == null)
137: return;
138: String s = p.getName();
139: assertEquals("unexpected package", "java.lang", s);
140: }
141:
142: /**
143: *
144: */
145: public void test_getPackages_V() {
146: //System.out.println("test_getPackages_V");
147: Package ap[] = Package.getPackages();
148: for (int i = 0; i < ap.length; i++) {
149: if (ap[i].getName().indexOf("java.lang") != -1)
150: return;
151: }
152: fail("at least java.lang should be returned");
153: }
154:
155: /**
156: *
157: */
158: public void test_getSpecificationTitle_V() {
159: //System.out.println("test_getSpecificationTitle_V");
160: Package p = java.lang.Character.class.getPackage();
161: if (p == null)
162: return;
163: String s = p.getSpecificationTitle();
164: if (s == null)
165: return;
166: String specName = System.getProperty("java.specification.name");
167: assertEquals("unexpected specification title:", specName, s);
168: }
169:
170: /**
171: *
172: */
173: public void test_getSpecificationVendor_V() {
174: //System.out.println("test_getSpecificationVendor_V");
175: Package p = java.lang.Character.class.getPackage();
176: if (p == null)
177: return;
178: String s = p.getSpecificationVendor();
179: if (s == null)
180: return;
181: assertEquals("unexpected specification vendor:",
182: "Sun Microsystems, Inc.", s);
183: }
184:
185: /**
186: *
187: */
188: public void test_getSpecificationVersion_V() {
189: //System.out.println("test_getSpecificationVersion_V");
190: Package p = java.lang.Character.class.getPackage();
191: if (p == null)
192: return;
193: String s = p.getSpecificationVersion();
194: assertNotNull("spec version is null", s);
195: if (s == null)
196: return;
197: assertTrue("unexpected specification version: " + s, s
198: .matches("([0-9]+?\\.)\\*?[0-9]+?"));
199: }
200:
201: /**
202: *
203: */
204: public void test_hashCode_V() {
205: //System.out.println("test_hashCode_V");
206: Package p1 = java.lang.Character.class.getPackage();
207: Package p2 = java.io.File.class.getPackage();
208: if (p1 == null || p2 == null)
209: return;
210: assertTrue("hash codes should differ", p1.hashCode() != p2
211: .hashCode());
212: }
213:
214: /**
215: *
216: */
217: public void test_isCompatibleWith_Str_1() {
218: //System.out.println("test_isCompatibleWith_Str_1");
219: Package p = Package.getPackage("java.lang");
220: if (p == null)
221: return;
222: try {
223: p.isCompatibleWith("");
224: } catch (NumberFormatException _) {
225: } catch (Throwable e) {
226: fail("unexpected error: " + e.toString());
227: }
228: }
229:
230: /**
231: *
232: */
233: public void test_isCompatibleWith_Str_2() {
234: //System.out.println("test_isCompatibleWith_Str_2");
235: Package p = java.lang.Character.class.getPackage();
236: if (p == null)
237: return;
238: String s = p.getSpecificationVersion();
239: if (s == null)
240: return;
241: try {
242: assertTrue(
243: "should be compatible with its own spec version", p
244: .isCompatibleWith(s));
245: } catch (NumberFormatException _) {
246: fail("wrong version format");
247: }
248: }
249:
250: /**
251: *
252: */
253: public void test_isSealed_V() {
254: //System.out.println("test_isSealed_V");
255: Package p1 = java.lang.Character.class.getPackage();
256: Package p2 = java.lang.Void.class.getPackage();
257: if (p1 == null || p2 == null)
258: return;
259: assertEquals("values should be equal", p1.isSealed(), p2
260: .isSealed());
261: }
262:
263: /**
264: *
265: */
266: public void test_isSealed_URL_1() {
267: //System.out.println("test_isSealed_URL_1");
268: try {
269: Package p1 = java.lang.Character.class.getPackage();
270: Package p2 = java.lang.Void.class.getPackage();
271: if (p1 == null || p2 == null)
272: return;
273: java.net.URL url = new java.net.URL("http://intel.com/");
274: assertEquals("values should be equal", p1.isSealed(url), p2
275: .isSealed(url));
276: } catch (java.net.MalformedURLException _) {
277: fail("unexpected MalformedURLException");
278: }
279: }
280:
281: /**
282: *
283: */
284: public void test_isSealed_URL_2() {
285: //System.out.println("test_isSealed_URL_2");
286: try {
287: Package p = Package.getPackage("java.lang");
288: if (p == null)
289: return;
290: p.isSealed((java.net.URL) null);
291: fail("NullPointerException has not been thrown");
292: } catch (NullPointerException _) {
293: return;
294: }
295: }
296:
297: /**
298: *
299: */
300: public void test_toString_V() {
301: //System.out.println("test_toString_V");
302: Package p = PackageTest.class.getPackage();
303: if (p == null)
304: return;
305: //assertTrue("Error1"+p.toString(), p.toString().equals("package "+"
306: // "+p.getName()+", "+p.getSpecificationTitle()+",
307: // "+p.getImplementationVersion()));
308: assertTrue("unexpected: package name must be printed: "
309: + p.toString(), p.toString().indexOf(
310: "package " + p.getName()) != -1);
311: }
312: }
|