patterntesting.runtime.util.AssertArg Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-rt Show documentation
Show all versions of patterntesting-rt Show documentation
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
/*
* Copyright (c) 2013 by Oli B.
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 29.11.2013 by Oli B. ([email protected])
*/
package patterntesting.runtime.util;
import java.util.Set;
import javax.validation.*;
import org.slf4j.*;
/**
* This utility class is intended to check arguments. It is like the Assert
* class in Springframework and throws an {@link IllegalArgumentException} if
* the argument is not valid.
*
* This class is abstract to avoid that it is instantiated. There are only
* static methods so there is no need to instantiate it. This is the same way
* like the Springframework does it.
*
*
* For more information about the use of bean validation have a look at Bean Validation with Hibernate Validator framework
*
.
*
* @author oliver ([email protected])
* @since 1.4 (29.11.2013)
*/
public abstract class AssertArg {
private static final Logger log = LoggerFactory.getLogger(AssertArg.class);
/**
* Checks if the argument is valid. If not (or if is null) an
* {@link IllegalArgumentException} will be thrown.
*
* @param validatable the validatable argument
*/
public static void isValid(final Validator validatable) {
isValid(validatable, validatable);
}
/**
* Checks if the argument is valid. If not (or if is null) an
* {@link IllegalArgumentException} will be thrown.
*
* @param argument the argument
* @param validator the validator
*/
public static void isValid(final Object argument, final Validator validator) {
if (argument == null) {
throw new IllegalArgumentException("argument is null");
}
if (validator == null) {
log.debug("No validator given to validate {}.", argument);
} else {
Set> violations = validator.validate(argument);
if ((violations != null) && (violations.size() > 0)) {
throw new IllegalArgumentException(argument + " is invalid: " + violations);
}
}
}
}