Author: adrianc
Date: Wed Apr 14 20:03:46 2010 New Revision: 934166 URL: http://svn.apache.org/viewvc?rev=934166&view=rev Log: Added an Assert class. It should make argument checking easier. Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java (with props) ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java (with props) Modified: ofbiz/trunk/framework/base/build.xml Modified: ofbiz/trunk/framework/base/build.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=934166&r1=934165&r2=934166&view=diff ============================================================================== --- ofbiz/trunk/framework/base/build.xml (original) +++ ofbiz/trunk/framework/base/build.xml Wed Apr 14 20:03:46 2010 @@ -41,6 +41,7 @@ under the License. </patternset> <filelist id="test.classes" dir="${src.dir}"> + <file name="org/ofbiz/base/util/test/AssertTests.java"/> <file name="org/ofbiz/base/lang/test/ComparableRangeTests.java"/> <file name="org/ofbiz/base/util/test/IndentingWriterTests.java"/> <file name="org/ofbiz/base/util/test/ObjectTypeTests.java"/> Added: 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?rev=934166&view=auto ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java (added) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java Wed Apr 14 20:03:46 2010 @@ -0,0 +1,101 @@ +/* + * 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.ofbiz.base.util; + +/** Basic assertions. + * + */ +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> + * + * @param argumentName + * @param argumentObject + * @param targetClass + * @throws IllegalArgumentException + */ + public static void argumentCanBeCastTo(String argumentName, Object argumentObject, Class<?> targetClass) { + argumentNotNull(argumentName, argumentObject); + if (!targetClass.isAssignableFrom(argumentObject.getClass())) { + throw new IllegalArgumentException(argumentName + " is not a " + 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> + * + * @param argumentName + * @param argumentObject + * @param targetObject + * @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); + } + } + + /** + * Tests if an argument is not null and is an instance of a class. + * <p><code>Assert.argumentIsClass("foo", foo, HashMap.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()); + } + } + + /** + * Tests an argument for <code>null</code>. + * <p><code>Assert.argumentNotNull("foo", foo);</code></p> + * + * @param argumentName + * @param objectToTest + * @throws IllegalArgumentException + */ + public static void argumentNotNull(String argumentName, Object objectToTest) { + if (objectToTest == null) { + throw new IllegalArgumentException(argumentName + " cannot be null"); + } + } + + /** + * Tests a list of arguments for <code>null</code>. + * <p><code>Assert.argumentsNotNull("foo", foo, "bar", bar, ...);</code></p> + * + * @param arguments + * @throws IllegalArgumentException + */ + public static void argumentsNotNull(Object... arguments) { + for (int i = 0; i < arguments.length;) { + argumentNotNull((String) arguments[i++], arguments[i++]); + } + } + + private Assert() {} +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Assert.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: 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?rev=934166&view=auto ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java (added) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java Wed Apr 14 20:03:46 2010 @@ -0,0 +1,102 @@ +/* + * 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.ofbiz.base.util.test; + +import java.util.Map; +import org.ofbiz.base.util.Assert; +import junit.framework.TestCase; + +/** + * Assert tests {@link org.ofbiz.base.util.Assert}. + * + */ +public class AssertTests extends TestCase { + + public AssertTests(String name) { + super(name); + } + + public void testAssert(){ + Object testObject = new Object(); + + //----------------------------------------------------------------------- + try { + Assert.argumentNotNull("foo", new Object()); + } catch (Exception e) { + fail("argumentNotNull threw an exception - " + e); + } + try { + Assert.argumentNotNull("foo", null); + fail("argumentNotNull - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.argumentsNotNull("foo", testObject, "bar", testObject); + } catch (Exception e) { + fail("argumentsNotNull threw an exception - " + e); + } + try { + Assert.argumentsNotNull("foo", testObject, "bar", null); + fail("argumentsNotNull - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.argumentIsClass("foo", testObject, Object.class); + } catch (Exception e) { + fail("argumentIsClass threw an exception - " + e); + } + try { + Assert.argumentIsClass("foo", testObject, AssertTests.class); + fail("argumentIsClass - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.argumentEqualsObject("foo", testObject, testObject); + } catch (Exception e) { + fail("argumentEqualsObject threw an exception - " + e); + } + try { + Assert.argumentEqualsObject("foo", testObject, this); + fail("argumentEqualsObject - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.argumentEqualsObject("foo", testObject, testObject); + } catch (Exception e) { + fail("argumentEqualsObject threw an exception - " + e); + } + try { + Assert.argumentEqualsObject("foo", testObject, this); + fail("argumentEqualsObject - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + + //----------------------------------------------------------------------- + try { + Assert.argumentCanBeCastTo("foo", testObject, Object.class); + } catch (Exception e) { + fail("argumentCanBeCastTo threw an exception - " + e); + } + try { + Assert.argumentCanBeCastTo("foo", this, Map.class); + fail("argumentCanBeCastTo - IllegalArgumentException not thrown"); + } catch (IllegalArgumentException e) {} + } +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/AssertTests.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
Free forum by Nabble | Edit this page |