001: /*
002: * Copyright (C) 2004 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 07. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.reflection;
013:
014: import java.io.Serializable;
015:
016: public class PureJavaReflectionProviderTest extends
017: AbstractReflectionProviderTest {
018:
019: // inherits tests from superclass
020:
021: public ReflectionProvider createReflectionProvider() {
022: return new PureJavaReflectionProvider();
023: }
024:
025: // ---------------------------------------------------------
026:
027: private static class PrivateStaticInnerClass {
028: }
029:
030: public void testCanCreatePrivateStaticInnerClasses() {
031: assertCanCreate(PrivateStaticInnerClass.class);
032: }
033:
034: // ---------------------------------------------------------
035:
036: public class PublicNonStaticInnerClass {
037: }
038:
039: private class PrivateNonStaticInnerClass {
040: }
041:
042: public void testIsNotCapableOfConstructingNonStaticInnerClasses() {
043: assertCannotCreate(PublicNonStaticInnerClass.class);
044: assertCannotCreate(PrivateNonStaticInnerClass.class);
045: }
046:
047: // ---------------------------------------------------------
048:
049: public static class WithConstructorThatDoesStuff {
050: public WithConstructorThatDoesStuff() {
051: throw new UnsupportedOperationException(
052: "constructor called");
053: }
054: }
055:
056: public void testUnfortunatelyExecutesCodeInsideConstructor() {
057: try {
058: reflectionProvider
059: .newInstance(WithConstructorThatDoesStuff.class);
060: fail("Expected code in constructor to be executed and throw an exception");
061: } catch (UnsupportedOperationException expectedException) {
062: // good
063: }
064: }
065:
066: // ---------------------------------------------------------
067:
068: public static class WithoutDefaultConstructor {
069: public WithoutDefaultConstructor(String arg) {
070: }
071: }
072:
073: public void testIsNotCapableOfConstructingClassesWithoutDefaultConstructor() {
074: assertCannotCreate(WithoutDefaultConstructor.class);
075: }
076:
077: // ---------------------------------------------------------
078:
079: public static class WithPrivateDefaultConstructor {
080: private WithPrivateDefaultConstructor(String thing) {
081: throw new UnsupportedOperationException(
082: "wrong constructor called");
083: }
084:
085: private WithPrivateDefaultConstructor() {
086: }
087: }
088:
089: public void testUsesPrivateConstructorIfNecessary() {
090: assertCanCreate(WithPrivateDefaultConstructor.class);
091: }
092:
093: // ---------------------------------------------------------
094:
095: private static class SerializableWithoutDefaultConstructor
096: implements Serializable {
097: private int field1, field2;
098:
099: public SerializableWithoutDefaultConstructor(String thing) {
100: throw new UnsupportedOperationException(
101: "constructor called");
102: }
103: }
104:
105: private class NonStaticSerializableWithoutDefaultConstructor
106: implements Serializable {
107: public NonStaticSerializableWithoutDefaultConstructor(
108: String thing) {
109: throw new UnsupportedOperationException(
110: "constructor called");
111: }
112: }
113:
114: public void testBypassesConstructorForSerializableObjectsWithNoDefaultConstructor() {
115: assertCanCreate(SerializableWithoutDefaultConstructor.class);
116: assertCanCreate(NonStaticSerializableWithoutDefaultConstructor.class);
117: }
118:
119: }
|