patterntesting.runtime.junit.internal.DescriptionUtils 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.
/*
* $Id: DescriptionUtils.java,v 1.1 2012/04/20 18:57:54 oboehm Exp $
*
* Copyright (c) 2012 by Oliver Boehm
*
* 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 19.04.2012 by oliver ([email protected])
*/
package patterntesting.runtime.junit.internal;
import java.lang.annotation.Annotation;
import java.util.regex.*;
import org.junit.runner.Description;
import org.slf4j.*;
import patterntesting.annotation.check.runtime.MayReturnNull;
/**
* This class provides some utiltis for the Description class for older
* versions of JUnit 4. In newer versions we have some more methods to use
* but because we want to support also JUnit 4.4 we provide the missing
* methods here.
*
* @author oliver
* @since 1.2.20 (19.04.2012)
*/
public class DescriptionUtils {
private static final Logger log = LoggerFactory.getLogger(DescriptionUtils.class);
private static final Pattern METHOD_NAME_PATTTERN = Pattern.compile("(.*)\\((.*)\\)");
/** Utility class - no need to instantiate it. */
private DescriptionUtils() {}
/**
* Here we extract the test class from the given descripiton.
* In JUnit 4.4 we don't have the Description does not have a method
* getTestClass(). We cannot provide our own "Description44" subclass of
* it because it is not designed for subclassing (no public constructor).
* Se we (re)implement the missing features for the SmokeFilter here.
*
* @param description the description
* @return the test class from
* @since 1.2.20
*/
@MayReturnNull
public static Class> getTestClassOf(final Description description) {
String name = description.toString();
Matcher matcher = Pattern.compile("(.*)\\((.*)\\)").matcher(name);
if (matcher.matches()) {
name = matcher.group(2);
}
if (name == null)
return null;
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
log.debug("Cannot get test class from description \"{}\"", description);
return null;
}
}
/**
* Gets the method name the given description. This method is needed for
* JUnit 4.5 and older. In JUnit 4.6 and newer there is a getMethodName()
* available in the {@link Description} class.
*
* @param description the description
* @return the method name of
*/
@MayReturnNull
public static String getMethodNameOf(final Description description) {
Matcher matcher = METHOD_NAME_PATTTERN.matcher(description.getDisplayName());
if (matcher.matches()) {
return matcher.group(1);
}
return null;
}
/**
* Creates the test description.
*
* @param description the description
* @param annotations the annotations
* @return the description
*/
public static Description createTestDescription(final Description description,
final Annotation[] annotations) {
return Description.createTestDescription(getTestClassOf(description),
description.getDisplayName(), annotations);
}
}