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
020: * @version $Revision$
021: */package java.lang.reflect;
022:
023: import junit.framework.TestCase;
024:
025: /**
026: * tested class: java.lang.Constructor
027: * tested method: newInstance
028: *
029: */
030: public class ConstructorTestNewInstace extends TestCase {
031:
032: /**
033: * Attempt to create a class with a protected constructor
034: * from another package.
035: * This situation must produce IllegalAccessException.
036: */
037: public void test1() {
038: try {
039: Constructor c = java.lang.ProtectedConstructor.class
040: .getDeclaredConstructor((Class[]) null);
041: c.newInstance((Object[]) null);
042: fail("Exception expected");
043: } catch (Exception e) {
044: assertTrue(e.getMessage(),
045: e instanceof IllegalAccessException);
046: }
047: }
048:
049: /**
050: * Attempt to create a class with a protected constructor
051: * from another package.
052: * Now we've changed the constructor accessiblity before creating.
053: * This situation must not produce any exception.
054: */
055: public void test2() {
056: try {
057: Constructor c = java.lang.ProtectedConstructor.class
058: .getDeclaredConstructor((Class[]) null);
059: c.setAccessible(true);
060: c.newInstance((Object[]) null);
061: } catch (Exception e) {
062: fail(e.getMessage());
063: }
064: }
065:
066: /**
067: * Attempt to create a class with a protected constructor
068: * in the same package.
069: * This situation must not produce any exception.
070: */
071: public void test3() {
072: try {
073: Constructor c = java.lang.reflect.ProtectedConstructor.class
074: .getDeclaredConstructor((Class[]) null);
075: c.newInstance((Object[]) null);
076: } catch (Exception e) {
077: fail(e.getMessage());
078: }
079: }
080:
081: /**
082: * Attempt to create an inner class which is package accessible.
083: * This situation must produce IllegalAccessException.
084: */
085: public void test4() {
086: try {
087: Object o = java.lang.PackageAccessible
088: .getProtectedClassInstance();
089: Constructor c = o.getClass().getDeclaredConstructor(
090: (Class[]) null);
091: c.newInstance((Object[]) null);
092: fail("Exception expected");
093: } catch (Exception e) {
094: assertTrue(e.getMessage(),
095: e instanceof IllegalAccessException);
096: }
097: }
098:
099: /**
100: * Attempt to create an inner class which is package accessible.
101: * Now we've changed the class accessiblity.
102: * This situation must not produce any exception.
103: */
104: public void test5() {
105: try {
106: Object o = java.lang.PackageAccessible
107: .getProtectedClassInstance();
108: Constructor c = o.getClass().getDeclaredConstructor(
109: (Class[]) null);
110: c.setAccessible(true);
111: c.newInstance((Object[]) null);
112: } catch (Exception e) {
113: fail(e.getMessage());
114: }
115: }
116:
117: public void test6() {
118: try {
119: A.class.getDeclaredConstructor((Class[]) null).newInstance(
120: (Object[]) null);
121: } catch (InvocationTargetException e) {
122: return;
123: } catch (Exception e) {
124: }
125: fail("The InvocationTargetException exception expected");
126: }
127:
128: static class A {
129:
130: public A() {
131: throw new NullPointerException();
132: }
133: }
134: }
|