You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2010/03/25 21:01:07 UTC
svn commit: r927573 - in /tiles/sandbox/trunk/tiles3/tiles-api/src:
main/java/org/apache/tiles/ main/java/org/apache/tiles/reflect/
test/java/org/apache/tiles/ test/java/org/apache/tiles/reflect/
Author: apetrelli
Date: Thu Mar 25 20:01:07 2010
New Revision: 927573
URL: http://svn.apache.org/viewvc?rev=927573&view=rev
Log:
TILESSB-11
Added test cases to ClassUtil and some exceptions.
Added:
tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java (with props)
tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java (with props)
tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java (with props)
Modified:
tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/NoSuchContainerException.java
tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotAccessMethodException.java
tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotInstantiateObjectException.java
tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/ClassUtil.java
tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/ClassUtilTest.java
Modified: tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/NoSuchContainerException.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/NoSuchContainerException.java?rev=927573&r1=927572&r2=927573&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/NoSuchContainerException.java (original)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/NoSuchContainerException.java Thu Mar 25 20:01:07 2010
@@ -53,7 +53,7 @@ public class NoSuchContainerException ex
* @param e The exception to be wrapped.
* @since 2.1.0
*/
- public NoSuchContainerException(Exception e) {
+ public NoSuchContainerException(Throwable e) {
super(e);
}
@@ -64,7 +64,7 @@ public class NoSuchContainerException ex
* @param e The exception to be wrapped.
* @since 2.1.0
*/
- public NoSuchContainerException(String message, Exception e) {
+ public NoSuchContainerException(String message, Throwable e) {
super(message, e);
}
Modified: tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotAccessMethodException.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotAccessMethodException.java?rev=927573&r1=927572&r2=927573&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotAccessMethodException.java (original)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotAccessMethodException.java Thu Mar 25 20:01:07 2010
@@ -54,7 +54,7 @@ public class CannotAccessMethodException
* @param e The exception to be wrapped.
* @since 2.1.0
*/
- public CannotAccessMethodException(Exception e) {
+ public CannotAccessMethodException(Throwable e) {
super(e);
}
@@ -65,7 +65,7 @@ public class CannotAccessMethodException
* @param e The exception to be wrapped.
* @since 2.1.0
*/
- public CannotAccessMethodException(String message, Exception e) {
+ public CannotAccessMethodException(String message, Throwable e) {
super(message, e);
}
Modified: tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotInstantiateObjectException.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotInstantiateObjectException.java?rev=927573&r1=927572&r2=927573&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotInstantiateObjectException.java (original)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/CannotInstantiateObjectException.java Thu Mar 25 20:01:07 2010
@@ -54,7 +54,7 @@ public class CannotInstantiateObjectExce
* @param e The exception to be wrapped.
* @since 2.1.0
*/
- public CannotInstantiateObjectException(Exception e) {
+ public CannotInstantiateObjectException(Throwable e) {
super(e);
}
@@ -65,7 +65,7 @@ public class CannotInstantiateObjectExce
* @param e The exception to be wrapped.
* @since 2.1.0
*/
- public CannotInstantiateObjectException(String message, Exception e) {
+ public CannotInstantiateObjectException(String message, Throwable e) {
super(message, e);
}
Modified: tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/ClassUtil.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/ClassUtil.java?rev=927573&r1=927572&r2=927573&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/ClassUtil.java (original)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/main/java/org/apache/tiles/reflect/ClassUtil.java Thu Mar 25 20:01:07 2010
@@ -23,8 +23,6 @@ package org.apache.tiles.reflect;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.util.Map;
import org.slf4j.Logger;
@@ -121,62 +119,6 @@ public final class ClassUtil {
}
/**
- * Gets a method and forces it to be accessible, even if it is not.
- *
- * @param clazz The class from which the method will be got.
- * @param methodName The name of the method.
- * @param parameterTypes The parameter types that the method must match.
- * @return The method, if it is found.
- * @since 2.0.7
- */
- public static Method getForcedAccessibleMethod(Class<?> clazz,
- String methodName, Class<?>... parameterTypes) {
- Method method;
- try {
- method = clazz.getMethod(methodName, parameterTypes);
- } catch (SecurityException e) {
- throw new CannotAccessMethodException("Cannot access method '"
- + methodName + "' in class '" + clazz.getName()
- + "' for security reasons", e);
- } catch (NoSuchMethodException e) {
- throw new CannotAccessMethodException("The method '"
- + methodName + "' in class '" + clazz.getName()
- + "' does not exist", e);
- }
- if (!method.isAccessible()) {
- method.setAccessible(true);
- }
- return method;
- }
-
- /**
- * Invokes a method, masking with a runtime exception all the exceptions.
- *
- * @param obj The object from which a method will be called.
- * @param method The method to call.
- * @param args The arguments of the method.
- * @return The object returned, if the method is not "void".
- * @since 2.0.7
- */
- public static Object invokeMethod(Object obj, Method method, Object... args) {
- try {
- return method.invoke(obj, args);
- } catch (IllegalArgumentException e) {
- throw new CannotAccessMethodException("The arguments for '"
- + method.getName() + "' in class '"
- + obj.getClass().getName() + "' are not valid", e);
- } catch (IllegalAccessException e) {
- throw new CannotAccessMethodException("Cannot access '"
- + method.getName() + "' in class '"
- + obj.getClass().getName() + "'", e);
- } catch (InvocationTargetException e) {
- throw new CannotAccessMethodException(
- "An exception has been thrown inside '" + method.getName()
- + "' in class '" + obj.getClass().getName() + "'", e);
- }
- }
-
- /**
* Collects bean infos from a class and filling a list.
*
* @param clazz The class to be inspected.
Added: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java?rev=927573&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java (added)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java Thu Mar 25 20:01:07 2010
@@ -0,0 +1,77 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tiles;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests {@link NoSuchContainerException}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class NoSuchContainerExceptionTest {
+
+ /**
+ * Test method for {@link org.apache.tiles.NoSuchContainerException#NoSuchContainerException()}.
+ */
+ @Test
+ public void testNoSuchContainerException() {
+ NoSuchContainerException exception = new NoSuchContainerException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.NoSuchContainerException#NoSuchContainerException(java.lang.String)}.
+ */
+ @Test
+ public void testNoSuchContainerExceptionString() {
+ NoSuchContainerException exception = new NoSuchContainerException("my message");
+ assertEquals("my message", exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.NoSuchContainerException#NoSuchContainerException(java.lang.Throwable)}.
+ */
+ @Test
+ public void testNoSuchContainerExceptionThrowable() {
+ Throwable cause = new Throwable();
+ NoSuchContainerException exception = new NoSuchContainerException(cause);
+ assertEquals(cause.toString(), exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.NoSuchContainerException#NoSuchContainerException(java.lang.String, java.lang.Throwable)}.
+ */
+ @Test
+ public void testNoSuchContainerExceptionStringThrowable() {
+ Throwable cause = new Throwable();
+ NoSuchContainerException exception = new NoSuchContainerException("my message", cause);
+ assertEquals("my message", exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+}
Propchange: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/NoSuchContainerExceptionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java?rev=927573&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java (added)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java Thu Mar 25 20:01:07 2010
@@ -0,0 +1,77 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tiles.reflect;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests {@link CannotAccessMethodException}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class CannotAccessMethodExceptionTest {
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotAccessMethodException#CannotAccessMethodException()}.
+ */
+ @Test
+ public void testCannotAccessMethodException() {
+ CannotAccessMethodException exception = new CannotAccessMethodException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotAccessMethodException#CannotAccessMethodException(java.lang.String)}.
+ */
+ @Test
+ public void testCannotAccessMethodExceptionString() {
+ CannotAccessMethodException exception = new CannotAccessMethodException("my message");
+ assertEquals("my message", exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotAccessMethodException#CannotAccessMethodException(java.lang.Throwable)}.
+ */
+ @Test
+ public void testCannotAccessMethodExceptionThrowable() {
+ Throwable cause = new Throwable();
+ CannotAccessMethodException exception = new CannotAccessMethodException(cause);
+ assertEquals(cause.toString(), exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotAccessMethodException#CannotAccessMethodException(java.lang.String, java.lang.Throwable)}.
+ */
+ @Test
+ public void testCannotAccessMethodExceptionStringThrowable() {
+ Throwable cause = new Throwable();
+ CannotAccessMethodException exception = new CannotAccessMethodException("my message", cause);
+ assertEquals("my message", exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+}
Propchange: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotAccessMethodExceptionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java?rev=927573&view=auto
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java (added)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java Thu Mar 25 20:01:07 2010
@@ -0,0 +1,77 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tiles.reflect;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests {@link CannotInstantiateObjectException}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class CannotInstantiateObjectExceptionTest {
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotInstantiateObjectException#CannotInstantiateObjectException()}.
+ */
+ @Test
+ public void testCannotInstantiateObjectException() {
+ CannotInstantiateObjectException exception = new CannotInstantiateObjectException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotInstantiateObjectException#CannotInstantiateObjectException(java.lang.String)}.
+ */
+ @Test
+ public void testCannotInstantiateObjectExceptionString() {
+ CannotInstantiateObjectException exception = new CannotInstantiateObjectException("my message");
+ assertEquals("my message", exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotInstantiateObjectException#CannotInstantiateObjectException(java.lang.Throwable)}.
+ */
+ @Test
+ public void testCannotInstantiateObjectExceptionThrowable() {
+ Throwable cause = new Throwable();
+ CannotInstantiateObjectException exception = new CannotInstantiateObjectException(cause);
+ assertEquals(cause.toString(), exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.CannotInstantiateObjectException#CannotInstantiateObjectException(java.lang.String, java.lang.Throwable)}.
+ */
+ @Test
+ public void testCannotInstantiateObjectExceptionStringThrowable() {
+ Throwable cause = new Throwable();
+ CannotInstantiateObjectException exception = new CannotInstantiateObjectException("my message", cause);
+ assertEquals("my message", exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+}
Propchange: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/CannotInstantiateObjectExceptionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/ClassUtilTest.java
URL: http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/ClassUtilTest.java?rev=927573&r1=927572&r2=927573&view=diff
==============================================================================
--- tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/ClassUtilTest.java (original)
+++ tiles/sandbox/trunk/tiles3/tiles-api/src/test/java/org/apache/tiles/reflect/ClassUtilTest.java Thu Mar 25 20:01:07 2010
@@ -67,11 +67,76 @@ public class ClassUtilTest {
}
/**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#getClass(String, Class)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test
+ public void testGetClass() throws ClassNotFoundException {
+ assertEquals(TestInterface.class, ClassUtil.getClass(
+ TestInterface.class.getName(), Object.class));
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#getClass(String, Class)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test(expected=ClassNotFoundException.class)
+ public void testGetClassException() throws ClassNotFoundException {
+ ClassUtil.getClass("this.class.does.not.Exist", Object.class);
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#instantiate(String, boolean)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test
+ public void testInstantiate() {
+ assertNotNull(ClassUtil.instantiate(TestClass.class.getName(), true));
+ assertNull(ClassUtil.instantiate("this.class.does.not.Exist", true));
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#instantiate(String, boolean)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test
+ public void testInstantiateOneParameter() {
+ assertNotNull(ClassUtil.instantiate(TestClass.class.getName()));
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#instantiate(String)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test(expected=CannotInstantiateObjectException.class)
+ public void testInstantiateOneParameterException() {
+ assertNotNull(ClassUtil.instantiate("this.class.does.not.Exist"));
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#instantiate(String)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test(expected=CannotInstantiateObjectException.class)
+ public void testInstantiateInstantiationException() {
+ ClassUtil.instantiate(TestInterface.class.getName());
+ }
+
+ /**
+ * Test method for {@link org.apache.tiles.reflect.ClassUtil#instantiate(String)}.
+ * @throws ClassNotFoundException If something goes wrong.
+ */
+ @Test(expected=CannotInstantiateObjectException.class)
+ public void testInstantiateIllegalAccessException() {
+ ClassUtil.instantiate(TestPrivateClass.class.getName());
+ }
+
+ /**
* Interface to be used as test.
*
* @version $Rev$ $Date$
*/
- private static interface TestInterface {
+ public static interface TestInterface {
/**
* The value.
@@ -101,4 +166,10 @@ public class ClassUtilTest {
*/
void setValue3(String value3);
}
+
+ public static class TestClass {
+ }
+
+ private static class TestPrivateClass {
+ }
}