Author: adrianc
Date: Fri Apr 16 23:29:07 2010 New Revision: 935096 URL: http://svn.apache.org/viewvc?rev=935096&view=rev Log: An improved Assert class - replaces the one I took out. Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java - copied, changed from r934178, ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java - copied, changed from r934178, ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java Copied: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java (from r934178, ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java) URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java?p2=ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java&p1=ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java&r1=934178&r2=935096&rev=935096&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java Fri Apr 16 23:29:07 2010 @@ -17,6 +17,9 @@ package org.ofbiz.base.util; +import java.util.Collection; +import java.util.Map; + /** Basic assertions. * */ @@ -24,76 +27,182 @@ public class Assert { /** * Tests if an argument is not null and can be cast to a specified class. - * <p><code>Assert.argumentCanBeCastTo("foo", foo, Map.class);</code></p> + * <p><code>Assert.isAssignableTo("foo", foo, Foo.class);</code></p> * * @param argumentName * @param argumentObject * @param targetClass * @throws IllegalArgumentException */ - public static void argumentCanBeCastTo(String argumentName, Object argumentObject, Class<?> targetClass) { - argumentNotNull(argumentName, argumentObject); + public static void isAssignableTo(String argumentName, Object argumentObject, Class<?> targetClass) { + notNull(argumentName, argumentObject); if (!targetClass.isAssignableFrom(argumentObject.getClass())) { - throw new IllegalArgumentException(argumentName + " is not a " + targetClass.getName()); + throw new IllegalArgumentException(argumentName + " cannot be assigned to " + targetClass.getName()); } } /** - * Tests if an argument is not null and is equal to an object. - * <p><code>Assert.argumentEqualsObject("foo", foo, new Foo());</code></p> + * Tests if an argument is not null and is an instance of a specified class. + * <p><code>Assert.isInstanceOf("foo", foo, Foo.class);</code></p> * * @param argumentName * @param argumentObject - * @param targetObject + * @param targetClass * @throws IllegalArgumentException */ - public static void argumentEqualsObject(String argumentName, Object argumentObject, Object targetObject) { - argumentNotNull(argumentName, argumentObject); - if (!argumentObject.equals(targetObject)) { - throw new IllegalArgumentException(argumentName + " is not equal to " + targetObject); + public static void isInstanceOf(String argumentName, Object argumentObject, Class<?> targetClass) { + notNull(argumentName, argumentObject); + if (!targetClass.isInstance(argumentObject)) { + throw new IllegalArgumentException(argumentName + " is not an instance of " + targetClass.getName()); } } /** - * Tests if an argument is not null and is an instance of a class. - * <p><code>Assert.argumentIsClass("foo", foo, HashMap.class);</code></p> + * Tests if an argument is not null and is an instance of a specified class. + * <p><code>Assert.isInstanceOf("foo", foo, Foo.class, Bar.class, ...);</code></p> + * + * @param argumentName + * @param argumentObject + * @param targetClasses + * @throws IllegalArgumentException + */ + public static void isInstanceOf(String argumentName, Object argumentObject, Class<?>... targetClasses) { + notNull(argumentName, argumentObject); + for (int i = 0; i < targetClasses.length;) { + if (targetClasses[i++].isInstance(argumentObject)) { + return; + } + } + StringBuilder sb = new StringBuilder(argumentName); + sb.append(" must be an instance of"); + for (int i = 0; i < targetClasses.length;) { + if (i != 0) { + sb.append(","); + } + sb.append(" ").append(targetClasses[i++].getName()); + } + throw new IllegalArgumentException(sb.toString()); + } + + /** + * Tests if an argument is not null and is not an instance of a specified class. + * <p><code>Assert.isNotInstanceOf("foo", foo, Foo.class);</code></p> * * @param argumentName * @param argumentObject * @param targetClass * @throws IllegalArgumentException */ - public static void argumentIsClass(String argumentName, Object argumentObject, Class<?> targetClass) { - argumentNotNull(argumentName, argumentObject); - if (argumentObject.getClass() != targetClass) { - throw new IllegalArgumentException(argumentName + " is not a " + targetClass.getName()); + public static void isNotInstanceOf(String argumentName, Object argumentObject, Class<?> targetClass) { + notNull(argumentName, argumentObject); + if (targetClass.isInstance(argumentObject)) { + throw new IllegalArgumentException(argumentName + " cannot be an instance of " + targetClass.getName()); } } /** - * Tests an argument for <code>null</code>. - * <p><code>Assert.argumentNotNull("foo", foo);</code></p> + * Tests if an argument is not null and is not an instance of a specified class. + * <p><code>Assert.isNotInstanceOf("foo", foo, Foo.class, Bar.class, ...);</code></p> * * @param argumentName - * @param objectToTest + * @param argumentObject + * @param targetClasses * @throws IllegalArgumentException */ - public static void argumentNotNull(String argumentName, Object objectToTest) { - if (objectToTest == null) { - throw new IllegalArgumentException(argumentName + " cannot be null"); + public static void isNotInstanceOf(String argumentName, Object argumentObject, Class<?>... targetClasses) { + notNull(argumentName, argumentObject); + for (int i = 0; i < targetClasses.length;) { + Class<?> targetClass = targetClasses[i++]; + if (targetClass.isInstance(argumentObject)) { + throw new IllegalArgumentException(argumentName + " cannot be an instance of " + targetClass.getName()); + } + } + } + + /** + * Tests if an argument is not null and is not empty. + * <p><code>Assert.notEmpty("foo", foo);</code></p> + * + * @param argumentName + * @param argumentObject + * @throws IllegalArgumentException + */ + public static void notEmpty(String argumentName, String argumentObject) { + notNull(argumentName, argumentObject); + if (argumentObject.length() == 0) { + throw new IllegalArgumentException(argumentName + " cannot be empty"); + } + } + + /** + * Tests if an argument is not null and is not empty. + * <p><code>Assert.notEmpty("foo", foo);</code></p> + * + * @param argumentName + * @param argumentObject + * @throws IllegalArgumentException + */ + public static <T extends Map<?, ?>> void notEmpty(String argumentName, T argumentObject) { + notNull(argumentName, argumentObject); + if (argumentObject.size() == 0) { + throw new IllegalArgumentException(argumentName + " cannot be empty"); + } + } + + /** + * Tests if an argument is not null and is not empty. + * <p><code>Assert.notEmpty("foo", foo);</code></p> + * + * @param argumentName + * @param argumentObject + * @throws IllegalArgumentException + */ + public static <T extends Collection<?>> void notEmpty(String argumentName, T argumentObject) { + notNull(argumentName, argumentObject); + if (argumentObject.size() == 0) { + throw new IllegalArgumentException(argumentName + " cannot be empty"); + } + } + + /** + * Tests if an argument is not null and is not empty. + * <p><code>Assert.notEmpty("foo", foo);</code></p> + * + * @param argumentName + * @param argumentObject + * @throws IllegalArgumentException + */ + public static <T> void notEmpty(String argumentName, T[] argumentObject) { + notNull(argumentName, argumentObject); + if (argumentObject.length == 0) { + throw new IllegalArgumentException(argumentName + " cannot be empty"); } } /** * Tests a list of arguments for <code>null</code>. - * <p><code>Assert.argumentsNotNull("foo", foo, "bar", bar, ...);</code></p> + * <p><code>Assert.notNull("foo", foo, "bar", bar, ...);</code></p> * * @param arguments * @throws IllegalArgumentException */ - public static void argumentsNotNull(Object... arguments) { + public static void notNull(Object... arguments) { for (int i = 0; i < arguments.length;) { - argumentNotNull((String) arguments[i++], arguments[i++]); + notNull((String) arguments[i++], arguments[i++]); + } + } + + /** + * Tests an argument for <code>null</code>. + * <p><code>Assert.notNull("foo", foo);</code></p> + * + * @param argumentName + * @param objectToTest + * @throws IllegalArgumentException + */ + public static void notNull(String argumentName, Object objectToTest) { + if (objectToTest == null) { + throw new IllegalArgumentException(argumentName + " cannot be null"); } } Copied: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java (from r934178, ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java) URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java?p2=ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java&p1=ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java&r1=934178&r2=935096&rev=935096&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java Fri Apr 16 23:29:07 2010 @@ -16,10 +16,15 @@ */ package org.ofbiz.base.util.test; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; -import org.ofbiz.base.util.Assert; + import junit.framework.TestCase; +import org.ofbiz.base.util.Assert; + /** * Assert tests {@link org.ofbiz.base.util.Assert}. * @@ -30,73 +35,135 @@ public class AssertTests extends TestCas super(name); } + @SuppressWarnings("unchecked") public void testAssert(){ Object testObject = new Object(); //----------------------------------------------------------------------- try { - Assert.argumentNotNull("foo", testObject); + Assert.notNull("foo", testObject); + } catch (Exception e) { + fail("notNull threw an exception - " + e); + } + try { + Assert.notNull("foo", null); + fail("notNull - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.notNull("foo", testObject, "bar", testObject); + } catch (Exception e) { + fail("notNull (argument list) threw an exception - " + e); + } + try { + Assert.notNull("foo", testObject, "bar", null); + fail("notNull (argument list) - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.notEmpty("foo", "foo"); } catch (Exception e) { - fail("argumentNotNull threw an exception - " + e); + fail("notEmpty(String) threw an exception - " + e); } try { - Assert.argumentNotNull("foo", null); - fail("argumentNotNull - IllegalArgumentException not thrown"); + Assert.notEmpty("foo", ""); + fail("notEmpty(String) - IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) {} //----------------------------------------------------------------------- + String[] strArray = {"foo", "bar"}; + try { + Assert.notEmpty("foo", strArray); + } catch (Exception e) { + fail("notEmpty(Array) threw an exception - " + e); + } + try { + Assert.notEmpty("foo", new String[0]); + fail("notEmpty(Array) - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + List<String> strList = new ArrayList<String>(); + try { + Assert.notEmpty("foo", strList); + fail("notEmpty(Collection) - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + strList.add("foo"); + try { + Assert.notEmpty("foo", strList); + } catch (Exception e) { + fail("notEmpty(Collection) threw an exception - " + e); + } + + //----------------------------------------------------------------------- + Map<String,String> strMap = new HashMap<String, String>(); + try { + Assert.notEmpty("foo", strMap); + fail("notEmpty(Map) - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + strMap.put("foo", "foo"); + try { + Assert.notEmpty("foo", strMap); + } catch (Exception e) { + fail("notEmpty(Map) threw an exception - " + e); + } + + //----------------------------------------------------------------------- try { - Assert.argumentsNotNull("foo", testObject, "bar", testObject); + Assert.isInstanceOf("foo", strMap, Map.class); } catch (Exception e) { - fail("argumentsNotNull threw an exception - " + e); + fail("isInstanceOf threw an exception - " + e); } try { - Assert.argumentsNotNull("foo", testObject, "bar", null); - fail("argumentsNotNull - IllegalArgumentException not thrown"); + Assert.isInstanceOf("foo", strMap, AssertTests.class); + fail("isInstanceOf - IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) {} //----------------------------------------------------------------------- try { - Assert.argumentIsClass("foo", testObject, Object.class); + Assert.isInstanceOf("foo", strMap, String.class, Map.class); } catch (Exception e) { - fail("argumentIsClass threw an exception - " + e); + fail("isInstanceOf (argument list) threw an exception - " + e); } try { - Assert.argumentIsClass("foo", testObject, AssertTests.class); - fail("argumentIsClass - IllegalArgumentException not thrown"); + Assert.isInstanceOf("foo", strMap, String.class, AssertTests.class); + fail("isInstanceOf (argument list) - IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) {} //----------------------------------------------------------------------- try { - Assert.argumentEqualsObject("foo", testObject, testObject); + Assert.isNotInstanceOf("foo", strMap, String.class); } catch (Exception e) { - fail("argumentEqualsObject threw an exception - " + e); + fail("isNotInstanceOf threw an exception - " + e); } try { - Assert.argumentEqualsObject("foo", testObject, this); - fail("argumentEqualsObject - IllegalArgumentException not thrown"); + Assert.isNotInstanceOf("foo", strMap, Map.class); + fail("isNotInstanceOf - IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) {} //----------------------------------------------------------------------- try { - Assert.argumentEqualsObject("foo", testObject, testObject); + Assert.isNotInstanceOf("foo", strMap, String.class, AssertTests.class); } catch (Exception e) { - fail("argumentEqualsObject threw an exception - " + e); + fail("isNotInstanceOf (argument list) threw an exception - " + e); } try { - Assert.argumentEqualsObject("foo", testObject, this); - fail("argumentEqualsObject - IllegalArgumentException not thrown"); + Assert.isNotInstanceOf("foo", strMap, String.class, Map.class); + fail("isNotInstanceOf (argument list) - IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) {} //----------------------------------------------------------------------- try { - Assert.argumentCanBeCastTo("foo", testObject, Object.class); + Assert.isAssignableTo("foo", strArray, strArray.getClass()); } catch (Exception e) { - fail("argumentCanBeCastTo threw an exception - " + e); + fail("isNotInstanceOf (argument list) threw an exception - " + e); } try { - Assert.argumentCanBeCastTo("foo", this, Map.class); - fail("argumentCanBeCastTo - IllegalArgumentException not thrown"); + Map[] mapArray = {strMap}; + Assert.isAssignableTo("foo", strArray, mapArray.getClass()); + fail("isNotInstanceOf (argument list) - IllegalArgumentException not thrown"); } catch (IllegalArgumentException e) {} } } |
Free forum by Nabble | Edit this page |