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 Evgueni V. Brevnov, Roman S. Bushmanov
020: * @version $Revision$
021: */package java.lang;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * tested class: java.lang.Class
027: * tested method: newInstance
028: */
029: public class ClassTestNewInstance extends TestCase {
030:
031: /**
032: * Attempt to create an abstract class. InstantiationException exception
033: * must be thrown in this case.
034: */
035: public void test1() {
036: try {
037: ClassTestNewInstance.AbstractClassPublicConstructor.class
038: .newInstance();
039: } catch (InstantiationException e) {
040: return;
041: } catch (Exception e) {
042: }
043: fail("InstantiationException exception expected");
044: }
045:
046: /**
047: * Attempt to create a class with a private constructor.
048: * IllegalAccessException must be thrown in this case.
049: */
050: public void test2() {
051: try {
052: java.lang.PrivateConstructor.class.newInstance();
053: } catch (IllegalAccessException e) {
054: return;
055: } catch (Exception e) {
056: }
057: fail("IllegalAccessException exception expected");
058: }
059:
060: /**
061: * Attempt to create aclass which throws an exception.
062: * InstantiationException must be thrown in this case.
063: */
064: public void test3() {
065: try {
066: ClassTestNewInstance.ExceptionThrowner.class.newInstance();
067: } catch (InstantiationException e) {
068: return;
069: } catch (Exception e) {
070: }
071: fail("InstantiationException exception expected");
072: }
073:
074: /**
075: * This is normal use of newInstance method. No exceptions must be thrown.
076: * The reated object must have correct type.
077: */
078: public void test4() {
079: try {
080: Object o = ClassTestNewInstance.class.newInstance();
081: assertTrue("Wrong type", o instanceof ClassTestNewInstance);
082: } catch (Exception e) {
083: fail(e.toString());
084: }
085: }
086:
087: /**
088: * Attempt to instantiate an object of the primitive type.
089: */
090: public void test5() {
091: try {
092: Character.TYPE.newInstance();
093: } catch (InstantiationException e) {
094: return;
095: } catch (Exception e) {
096: }
097: fail("InstantiationException exception expected");
098: }
099:
100: /**
101: * Attempt to instantiate an object of the primitive type.
102: */
103: public void test6() {
104: try {
105: new int[0].getClass().newInstance();
106: } catch (InstantiationException e) {
107: return;
108: } catch (Exception e) {
109: }
110: fail("InstantiationException exception expected");
111: }
112:
113: public void testBug521() {
114: try {
115: Class cls = Class.forName(Inner.class.getName());
116: cls.newInstance();
117: } catch (Exception e) {
118: fail(e.toString());
119: }
120: }
121:
122: /**
123: * Helper inner class.
124: */
125: private abstract class AbstractClassPublicConstructor {
126:
127: public AbstractClassPublicConstructor() {
128: }
129: }
130:
131: /**
132: * Helper inner class.
133: */
134: private class ExceptionThrowner {
135:
136: public ExceptionThrowner() throws Exception {
137: throw new Exception("This class cannot be instatiated");
138: }
139: }
140:
141: static class Inner {
142: }
143: }
|