diff options
Diffstat (limited to 'utils-test/src/main/java/org')
-rw-r--r-- | utils-test/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java | 109 | ||||
-rw-r--r-- | utils-test/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java | 230 |
2 files changed, 201 insertions, 138 deletions
diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java b/utils-test/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java index 3a43b943..7208748a 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/test/ExceptionsTester.java @@ -1,21 +1,21 @@ /* - * ============LICENSE_START======================================================= + * ============LICENSE_START==================================================== * Common Utils-Test - * ================================================================================ + * ============================================================================= * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ + * ============================================================================= * Licensed 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. - * ============LICENSE_END========================================================= + * ============LICENSE_END====================================================== */ package org.onap.policy.common.utils.test; @@ -34,18 +34,19 @@ public class ExceptionsTester extends ThrowablesTester { /** * Runs tests, on an Exception subclass, for all of the standard - * constructors. If the Exception subclass does not support a given type of - * constructor, then it skips that test. - * - * @param claz - * subclass to be tested + * constructors. If the Exception subclass does not support a given + * type of constructor, then it skips that test. + * + * @param claz subclass to be tested + * @param <T> Type of the class + * * @return the number of constructors that were found/tested * @throws ConstructionError * if the Exception subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public <T extends Exception> int test(Class<T> claz) { + public <T extends Exception> int test(final Class<T> claz) { int ncons = testAllException(claz); assertTrue(ncons > 0); @@ -55,19 +56,20 @@ public class ExceptionsTester extends ThrowablesTester { /** * Runs tests, on an Exception subclass, for all of the standard - * constructors. If the Exception subclass does not support a given type of - * constructor, then it skips that test. Does <i>not</i> throw an exception - * if no standard constructors are found. - * - * @param claz - * subclass to be tested + * constructors. If the Exception subclass does not support a given + * type of constructor, then it skips that test. Does <i>not</i> throw + * an exception if no standard constructors are found. + * + * @param claz subclass to be tested + * @param <T> type of the class + * * @return the number of constructors that were found/tested * @throws ConstructionError * if the Exception subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public <T extends Exception> int testAllException(Class<T> claz) { + public <T extends Exception> int testAllException(final Class<T> claz) { int ncons = 0; ncons += testAllThrowable(claz); @@ -87,12 +89,13 @@ public class ExceptionsTester extends ThrowablesTester { * <li><i>getCause()</i> returns the original cause passed to the * constructor</li> * </ul> - * - * If the Exception subclass does not support this type of constructor, then - * this method simply returns. - * - * @param claz - * subclass to be tested + * + * If the Exception subclass does not support this type of constructor, + * then this method simply returns. + * + * @param claz subclass to be tested + * @param <T> Type of the class + * * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError @@ -100,8 +103,10 @@ public class ExceptionsTester extends ThrowablesTester { * @throws AssertionError * if the constructed objects fail to pass various tests */ - public <T extends Exception> int testException(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "exception", Exception.class); + public <T extends Exception> int testException(final Class<T> claz) { + Constructor<T> cons = getConstructor(claz, "exception", + Exception.class); + if (cons == null) { return 0; } @@ -117,28 +122,31 @@ public class ExceptionsTester extends ThrowablesTester { } /** - * Tests exceptions created via the constructor that takes a String and an - * Exception. Verifies that: + * Tests exceptions created via the constructor that takes a String + * and an Exception. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> - * <li><i>getMessage()</i> returns the original message passed to the - * constructor</li> + * <li><i>getMessage()</i> returns the original message passed to + * the constructor</li> * <li><i>getCause()</i> returns the original cause passed to the * constructor</li> * </ul> - * - * If the Exception subclass does not support this type of constructor, then - * this method simply returns. - * - * @param claz - * subclass to be tested + * + * If the Exception subclass does not support this type of + * constructor, then this method simply returns. + * + * @param claz subclass to be tested + * @param <T> Type of the class + * * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError * if the Exception subclass cannot be constructed */ - public <T extends Exception> int testStringException(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "string-exception", String.class, Exception.class); + public <T extends Exception> int testStringException( + final Class<T> claz) { + Constructor<T> cons = getConstructor(claz, "string-exception", + String.class, Exception.class); if (cons == null) { return 0; } @@ -165,20 +173,25 @@ public class ExceptionsTester extends ThrowablesTester { * <li>suppressed exceptions can be added, if enabled</li> * <li>the stack trace can be added, if enabled</li> * </ul> - * - * If the Exception subclass does not support this type of constructor, then - * this method simply returns. - * - * @param claz - * subclass to be tested + * + * If the Exception subclass does not support this type of constructor, + * then this method simply returns. + * + * @param claz subclass to be tested + * @param <T> Type of the class + * * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError * if the Exception subclass cannot be constructed */ - public <T extends Exception> int testStringExceptionBooleanBoolean(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "string-exception-flags", String.class, Exception.class, - Boolean.TYPE, Boolean.TYPE); + public <T extends Exception> int testStringExceptionBooleanBoolean( + final Class<T> claz) { + + Constructor<T> cons = getConstructor(claz, + "string-exception-flags", String.class, Exception.class, + Boolean.TYPE, Boolean.TYPE); + if (cons == null) { return 0; } diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java b/utils-test/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java index 4a8da3b4..c77b7813 100644 --- a/utils-test/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java +++ b/utils-test/src/main/java/org/onap/policy/common/utils/test/ThrowablesTester.java @@ -1,21 +1,21 @@ /* - * ============LICENSE_START======================================================= + * ============LICENSE_START==================================================== * Common Utils-Test - * ================================================================================ + * ============================================================================= * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ + * ============================================================================= * Licensed 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. - * ============LICENSE_END========================================================= + * ============LICENSE_END====================================================== */ package org.onap.policy.common.utils.test; @@ -37,36 +37,43 @@ import org.slf4j.LoggerFactory; */ public class ThrowablesTester { - private static Logger logger = LoggerFactory.getLogger(ThrowablesTester.class); + private static Logger logger = + LoggerFactory.getLogger(ThrowablesTester.class); - public static final String EXPECTED_EXCEPTION_MSG = "expected exception"; - private static final String EXPECTED_SUPPRESSED_EXCEPTION_MSG = "expected suppressed exception"; + public static final String EXPECTED_EXCEPTION_MSG = + "expected exception"; + private static final String EXPECTED_SUPPRESSED_EXCEPTION_MSG = + "expected suppressed exception"; /** * Passed as a "cause" to constructors. */ - public static final Exception CAUSE = new Exception(EXPECTED_EXCEPTION_MSG); + public static final Exception CAUSE = + new Exception(EXPECTED_EXCEPTION_MSG); /** * Passed to new objects via the <i>addSuppressed()</i> method.. */ - public static final Throwable SUPPRESSED = new Throwable(EXPECTED_SUPPRESSED_EXCEPTION_MSG); + public static final Throwable SUPPRESSED = + new Throwable(EXPECTED_SUPPRESSED_EXCEPTION_MSG); /** - * Runs tests, on an Throwable subclass, for all of the standard - * constructors. If the Throwable subclass does not support a given type of - * constructor, then it skips that test. Does <i>not</i> throw an exception - * if no standard constructors are found. - * - * @param claz - * subclass to be tested + * Runs tests, on an Throwable subclass, for all of the + * standard constructors. If the Throwable subclass does + * not support a given type of constructor, then it skips + * that test. Does <i>not</i> throw an exception if no + * standard constructors are found. + * + * @param claz subclass to be tested + * @param <T> To be defined * @return the number of constructors that were found/tested * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> int testAllThrowable(Class<T> claz) { + public final <T extends Throwable> int testAllThrowable( + final Class<T> claz) { int ncons = 0; ncons += testDefault(claz); @@ -86,12 +93,12 @@ public class ThrowablesTester { * <li><i>getMessage()</i> returns null</li> * <li><i>getCause()</i> returns null</li> * </ul> - * - * If the Throwable subclass does not support this type of constructor, then - * this method simply returns. - * - * @param claz - * subclass to be tested + * + * If the Throwable subclass does not support this type of + * constructor, then this method simply returns. + * + * @param claz subclass to be tested + * @param <T> to be defined * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError @@ -99,7 +106,8 @@ public class ThrowablesTester { * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> int testDefault(Class<T> claz) { + public final <T extends Throwable> int testDefault( + final Class<T> claz) { Constructor<T> cons = getConstructor(claz, "default"); if (cons == null) { return 0; @@ -123,12 +131,13 @@ public class ThrowablesTester { * constructor</li> * <li><i>getCause()</i> returns null</li> * </ul> - * - * If the Throwable subclass does not support this type of constructor, then - * this method simply returns. - * + * + * If the Throwable subclass does not support this type of constructor, + * then this method simply returns. + * * @param claz * subclass to be tested + * @param <T> to be defined * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError @@ -136,8 +145,9 @@ public class ThrowablesTester { * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> int testString(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "string", String.class); + public final <T extends Throwable> int testString(final Class<T> claz) { + Constructor<T> cons = getConstructor(claz, "string", + String.class); if (cons == null) { return 0; } @@ -160,12 +170,13 @@ public class ThrowablesTester { * <li><i>getCause()</i> returns the original cause passed to the * constructor</li> * </ul> - * - * If the Throwable subclass does not support this type of constructor, then - * this method simply returns. - * + * + * If the Throwable subclass does not support this type of constructor, + * then this method simply returns. + * * @param claz * subclass to be tested + * @param <T> to be defined * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError @@ -173,8 +184,10 @@ public class ThrowablesTester { * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> int testThrowable(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "throwable", Throwable.class); + public final <T extends Throwable> int testThrowable( + final Class<T> claz) { + Constructor<T> cons = getConstructor(claz, "throwable", + Throwable.class); if (cons == null) { return 0; } @@ -189,8 +202,8 @@ public class ThrowablesTester { } /** - * Tests Throwable objects created via the constructor that takes a String - * and a Throwable. Verifies that: + * Tests Throwable objects created via the constructor that takes + * a String and a Throwable. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> * <li><i>getMessage()</i> returns the original message passed to the @@ -198,12 +211,12 @@ public class ThrowablesTester { * <li><i>getCause()</i> returns the original cause passed to the * constructor</li> * </ul> - * - * If the Throwable subclass does not support this type of constructor, then - * this method simply returns. - * - * @param claz - * subclass to be tested + * + * If the Throwable subclass does not support this type of constructor, + * then this method simply returns. + * + * @param claz subclass to be tested + * @param <T> to be defined * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError @@ -211,8 +224,10 @@ public class ThrowablesTester { * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> int testStringThrowable(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "string-throwable", String.class, Throwable.class); + public final <T extends Throwable> int testStringThrowable( + final Class<T> claz) { + Constructor<T> cons = getConstructor(claz, "string-throwable", + String.class, Throwable.class); if (cons == null) { return 0; } @@ -227,8 +242,8 @@ public class ThrowablesTester { } /** - * Tests Throwable objects created via the constructor that takes a String, - * a Throwable, and two booleans. Verifies that: + * Tests Throwable objects created via the constructor that takes + * a String, a Throwable, and two booleans. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> * <li><i>getMessage()</i> returns the original message passed to the @@ -238,12 +253,13 @@ public class ThrowablesTester { * <li>suppressed exceptions can be added, if enabled</li> * <li>the stack trace can be added, if enabled</li> * </ul> - * - * If the Throwable subclass does not support this type of constructor, then - * this method simply returns. - * + * + * If the Throwable subclass does not support this type of constructor, + * then this method simply returns. + * * @param claz * subclass to be tested + * @param <T> to be defined * @return {@code 1}, if the subclass supports this type of constructor, * {@code 0} otherwise * @throws ConstructionError @@ -251,9 +267,13 @@ public class ThrowablesTester { * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> int testStringThrowableBooleanBoolean(Class<T> claz) { - Constructor<T> cons = getConstructor(claz, "string-throwable-flags", String.class, Throwable.class, - Boolean.TYPE, Boolean.TYPE); + public final <T extends Throwable> int + testStringThrowableBooleanBoolean( + final Class<T> claz) { + Constructor<T> cons = getConstructor(claz, + "string-throwable-flags", + String.class, Throwable.class, + Boolean.TYPE, Boolean.TYPE); if (cons == null) { return 0; } @@ -271,20 +291,25 @@ public class ThrowablesTester { } /** - * Tests each combination of values for the "message" and the "cause" when - * using the constructor that takes a String, a Throwable/Exception, and two - * booleans. Verifies that expected values are returned by <i>toString()/i>, + * Tests each combination of values for the "message" and the "cause" + * when using the constructor that takes a String, + * a Throwable/Exception, and two booleans. Verifies that expected + * values are returned + * <ul> + * <i>toString()</i>, * <i>getMessage()</i>, and <i>getCause()</i>. * </ul> - * + * * @param cons * constructor to be invoked + * @param <T> to be defined * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> void testMessageCauseCombos(Constructor<T> cons) { + public final <T extends Throwable> void testMessageCauseCombos( + final Constructor<T> cons) { T ex; ex = newInstance(cons, null, null, true, true); @@ -309,20 +334,25 @@ public class ThrowablesTester { } /** - * Tests each combination of values for the "message" and the "cause" when - * using the constructor that takes a String, a Throwable/Exception, and two - * booleans. Verifies that expected values are returned by <i>toString()/i>, + * Tests each combination of values for the "message" and the + * "cause" when using the constructor that takes a String, + * a Throwable/Exception, and two booleans. Verifies that + * expected values are returned by + * <ul> + * <i>toString()</i>, * <i>getMessage()</i>, and <i>getCause()</i>. * </ul> - * + * * @param cons * constructor to be invoked + * @param <T> to be defined * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> void testFlagCombos(Constructor<T> cons) { + public final <T extends Throwable> void testFlagCombos( + final Constructor<T> cons) { testSuppressStack(cons); testSuppressNoStack(cons); testNoSuppressStack(cons); @@ -330,8 +360,9 @@ public class ThrowablesTester { } /** - * Tests Throwable objects constructed with {@code enableSuppression=true} - * and {@code writableStackTrace=true}. Verifies that: + * Tests Throwable objects constructed with + * {@code enableSuppression=true} and + * {@code writableStackTrace=true}. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> * <li><i>getMessage()</i> returns the original message passed to the @@ -341,15 +372,17 @@ public class ThrowablesTester { * <li>suppressed exceptions are added</li> * <li>the stack trace is added</li> * </ul> - * + * * @param cons * the throwable's class constructor + * @param <T> to be defined * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> void testSuppressStack(Constructor<T> cons) { + public final <T extends Throwable> void testSuppressStack( + final Constructor<T> cons) { T ex = newInstance(cons, "yes,yes", CAUSE, true, true); ex.addSuppressed(SUPPRESSED); @@ -365,8 +398,9 @@ public class ThrowablesTester { } /** - * Tests Throwable objects constructed with {@code enableSuppression=true} - * and {@code writableStackTrace=false}. Verifies that: + * Tests Throwable objects constructed with + * {@code enableSuppression=true} and + * {@code writableStackTrace=false}. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> * <li><i>getMessage()</i> returns the original message passed to the @@ -376,15 +410,17 @@ public class ThrowablesTester { * <li>suppressed exceptions are added</li> * <li>the stack trace is <i>not</i> added</li> * </ul> - * + * * @param cons * the throwable's class constructor + * @param <T> to be defined * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> void testSuppressNoStack(Constructor<T> cons) { + public final <T extends Throwable> void testSuppressNoStack( + final Constructor<T> cons) { T ex = newInstance(cons, "yes,no", CAUSE, true, false); ex.addSuppressed(SUPPRESSED); @@ -400,8 +436,9 @@ public class ThrowablesTester { } /** - * Tests Throwable objects constructed with {@code enableSuppression=false} - * and {@code writableStackTrace=true}. Verifies that: + * Tests Throwable objects constructed with + * {@code enableSuppression=false} and + * {@code writableStackTrace=true}. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> * <li><i>getMessage()</i> returns the original message passed to the @@ -411,15 +448,17 @@ public class ThrowablesTester { * <li>suppressed exceptions are <i>not</i> added</li> * <li>the stack trace is added</li> * </ul> - * + * * @param cons * the throwable's class constructor + * @param <T> to be defined * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> void testNoSuppressStack(Constructor<T> cons) { + public final <T extends Throwable> void testNoSuppressStack( + final Constructor<T> cons) { T ex = newInstance(cons, "no,yes", CAUSE, false, true); ex.addSuppressed(SUPPRESSED); @@ -434,8 +473,9 @@ public class ThrowablesTester { } /** - * Tests Throwable objects constructed with {@code enableSuppression=false} - * and {@code writableStackTrace=false}. Verifies that: + * Tests Throwable objects constructed with + * {@code enableSuppression=false} and + * {@code writableStackTrace=false}. Verifies that: * <ul> * <li><i>toString()</i> returns a non-null value</li> * <li><i>getMessage()</i> returns the original message passed to the @@ -444,15 +484,17 @@ public class ThrowablesTester { * constructor</li> * <li>suppressed exceptions are <i>not</i> added</li> * <li>the stack trace is <i>not</i> added</li> - * + * </ul> * @param cons * the throwable's class constructor + * @param <T> to be defined * @throws ConstructionError * if the Throwable subclass cannot be constructed * @throws AssertionError * if the constructed objects fail to pass various tests */ - public final <T extends Throwable> void testNoSuppressNoStack(Constructor<T> cons) { + public final <T extends Throwable> void testNoSuppressNoStack( + final Constructor<T> cons) { T ex = newInstance(cons, "no,no", CAUSE, false, false); ex.addSuppressed(SUPPRESSED); @@ -467,9 +509,10 @@ public class ThrowablesTester { /** * Attempts to get a constructor for objects of a given type. - * + * * @param claz * class of objects whose constructor is to be gotten + * @param <T> to be defined * @param testType * type of test being run * @param argTypes @@ -477,35 +520,42 @@ public class ThrowablesTester { * @return the desired constructor, or {@code null} if the desired * constructor is not available */ - protected <T extends Throwable> Constructor<T> getConstructor(Class<T> claz, String testType, - Class<?>... argTypes) { + protected <T extends Throwable> Constructor<T> getConstructor( + final Class<T> claz, + final String testType, + final Class<?>... argTypes) { try { return claz.getConstructor(argTypes); } catch (NoSuchMethodException | SecurityException e) { // this constructor is not defined so nothing to test - logger.debug("skipped test, no constructor for: " + claz + " due to: " + e); + logger.debug("skipped test, no constructor for: " + + claz + " due to: " + e); return null; } } /** * Creates a new instance of an Throwable subclass. - * + * * @param cons * subclass constructor + * @param <T> to be defined * @param args * arguments to be passed to the constructor * @return a new instance of the Throwable subclass * @throws ConstructionError * if the Throwable subclass cannot be constructed */ - protected <T extends Throwable> T newInstance(Constructor<T> cons, Object... args) { + protected <T extends Throwable> T newInstance( + final Constructor<T> cons, + final Object... args) { try { return cons.newInstance(args); - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + } catch (InstantiationException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException e) { throw new ConstructionError(e); |