001 /*****************************************************************************
002 * Copyright (C) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * Original Code By: Centerline Computers, Inc. *
008 *****************************************************************************/
009 package org.picocontainer.injectors;
010
011 import org.junit.Test;
012
013 import static org.junit.Assert.assertFalse;
014 import static org.junit.Assert.assertTrue;
015 import static org.junit.Assert.fail;
016 import static org.picocontainer.injectors.PrimitiveMemberChecker.isPrimitiveArgument;
017
018 import java.lang.reflect.Constructor;
019 import java.lang.reflect.Field;
020 import java.lang.reflect.Method;
021
022 /**
023 * @author Michael Rimov
024 *
025 */
026 public class PrimitiveMemberCheckerTestCase {
027
028
029 @SuppressWarnings("unused")
030 public static class TestClass {
031
032 public String able;
033
034 public int baker;
035
036 public TestClass(int value) {
037 //Does nothing.
038 }
039
040 public TestClass(String value) {
041 //Does nothing.
042 }
043
044
045 public void doSomething(String avalue) {
046 //Does nothing.
047 }
048
049 public void doSomething(int anotherValue ) {
050 //Does nothing.
051 }
052 }
053
054 /*
055 * Test method for {@link org.picocontainer.injectors.PrimitiveMemberChecker#isPrimitiveArgument(java.lang.reflect.AccessibleObject, int)}.
056 */
057 @Test
058 public void testIsPrimitiveField() throws NoSuchFieldException {
059 Field targetOne = TestClass.class.getField("able");
060 assertFalse(isPrimitiveArgument(targetOne, 0));
061
062 Field targetTwo = TestClass.class.getField("baker");
063 assertTrue(isPrimitiveArgument(targetTwo, 0));
064 }
065
066 @Test
067 public void testIsPrimitiveConstructorArg() throws NoSuchMethodException {
068 Constructor cOne = TestClass.class.getConstructor(Integer.TYPE);
069 assertTrue(isPrimitiveArgument(cOne, 0));
070
071 Constructor cTwo = TestClass.class.getConstructor(String.class);
072 assertFalse(isPrimitiveArgument(cTwo, 0));
073 }
074
075 @Test
076 public void testIsPrimitiveMethodArg() throws NoSuchMethodException {
077 Method mOne = TestClass.class.getMethod("doSomething", Integer.TYPE);
078 assertTrue(isPrimitiveArgument(mOne, 0));
079
080 Method mTwo = TestClass.class.getMethod("doSomething", String.class);
081 assertFalse(isPrimitiveArgument(mTwo, 0));
082 }
083
084
085 @Test
086 public void testArrayIndexOutOfBoundsIfIntegerArgTooBig() throws SecurityException, NoSuchMethodException {
087 Method mOne = TestClass.class.getMethod("doSomething", Integer.TYPE);
088 try {
089 boolean result = isPrimitiveArgument(mOne, 1);
090 fail("Should have thrown exception, instead got return value " + result);
091 } catch (ArrayIndexOutOfBoundsException e) {
092 //Message contents differentiate from a generic exception
093 assertTrue(e.getMessage().contains("Index i > types array length "));
094 }
095 }
096
097 }