Java Code Examples for org.junit.platform.commons.support.ReflectionSupport

The following examples show how to use org.junit.platform.commons.support.ReflectionSupport. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
void testProcessToDoShouldReplaceSingleValueRangeSubscriptArgumentPlaceholderUsingOnlyNegativeIndices() {
    // Given:
    final Method testMethod = ReflectionSupport
            .findMethod(Assertions.class, "assertTrue", new Class<?>[] { boolean.class })
            .orElseThrow(() -> new IllegalStateException("Could not find method"));

    final List<Object> arguments = list('x');


    ReplacementData data = ReplacementData.of(testMethod, 0, arguments);

    // When:
    String result = underTest.process(data, "%na[0]");

    // Then:
    assertThat(result).isEqualTo("arg0=x");
    assertThat(getTestCapturedLog()).containsPattern(
            "WARNING: Parameter names on method '.*' are not available. To store formal parameter names, compile the source file with the '-parameters' option");
}
 
Example 2
Source Project: microprofile-sandbox   Source File: JAXRSUtilities.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> T createRestClient(Class<T> clazz, String appContextRoot, String jwt) {
    String resourcePackage = clazz.getPackage().getName();

    // First check for a javax.ws.rs.core.Application in the same package as the resource
    List<Class<?>> appClasses = ReflectionSupport.findAllClassesInPackage(resourcePackage,
                                                                          c -> Application.class.isAssignableFrom(c) &&
                                                                               AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                          n -> true);
    if (appClasses.size() == 0) {
        // If not found, check under the 3rd package, so com.foo.bar.*
        // Classpath scanning can be expensive, so we jump straight to the 3rd package from root instead
        // of recursing up one package at a time and scanning the entire CP for each step
        String[] pkgs = resourcePackage.split("(.*)\\.(.*)\\.(.*)\\.", 2);
        if (pkgs.length > 0 && !pkgs[0].isEmpty() && !pkgs[0].equals(resourcePackage)) {
            appClasses = ReflectionSupport.findAllClassesInPackage(pkgs[0],
                                                                   c -> Application.class.isAssignableFrom(c) &&
                                                                        AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                   n -> true);
        }
    }

    if (appClasses.size() == 0) {
        LOGGER.info("No classes implementing 'javax.ws.rs.core.Application' found on classpath to set as context root for " + clazz +
                    ". Defaulting context root to '/'");
        return createRestClient(clazz, appContextRoot, "", jwt);
    }

    Class<?> selectedClass = appClasses.get(0);
    if (appClasses.size() > 1) {
        appClasses.sort((c1, c2) -> c1.getCanonicalName().compareTo(c2.getCanonicalName()));
        LOGGER.warn("Found multiple classes implementing 'javax.ws.rs.core.Application' on classpath: " + appClasses +
                    ". Setting context root to the first class discovered (" + selectedClass.getCanonicalName() + ")");
    }
    ApplicationPath appPath = AnnotationSupport.findAnnotation(selectedClass, ApplicationPath.class).get();
    return createRestClient(clazz, appContextRoot, appPath.value(), jwt);
}
 
Example 3
@Override
protected ConverterContext getConverterContext(CustomConverterDataProvider annotation) {
    return new ConverterContext(ReflectionSupport.newInstance(annotation.objectArrayConverter()),
            ReflectionSupport.newInstance(annotation.singleArgConverter()),
            ReflectionSupport.newInstance(annotation.stringConverter()), annotation.splitBy(),
            annotation.convertNulls(), annotation.trimValues(), annotation.ignoreEnumCase());
}
 
Example 4
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 5
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 6
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 7
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 8
@Override
protected ConverterContext getConverterContext(CustomConverterDataProvider annotation) {
    return new ConverterContext(ReflectionSupport.newInstance(annotation.objectArrayConverter()),
            ReflectionSupport.newInstance(annotation.singleArgConverter()),
            ReflectionSupport.newInstance(annotation.stringConverter()), annotation.splitBy(),
            annotation.convertNulls(), annotation.trimValues(), annotation.ignoreEnumCase());
}
 
Example 9
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 10
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 11
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 12
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 13
Source Project: junit-dataprovider   Source File: DataProviderExtension.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected ConverterContext getConverterContext(DataProvider dataProvider) {
    return new ConverterContext(ReflectionSupport.newInstance(dataProvider.objectArrayConverter()),
            ReflectionSupport.newInstance(dataProvider.singleArgConverter()),
            ReflectionSupport.newInstance(dataProvider.stringConverter()), dataProvider.splitBy(),
            dataProvider.convertNulls(), dataProvider.trimValues(), dataProvider.ignoreEnumCase());
}
 
Example 14
@BeforeEach
void setup() {
    tenParamMethod = ReflectionSupport
            .findMethod(getClass(), "tenParamMethod",
                    new Class<?>[] { char.class, int.class, long.class, double.class, String.class, char.class,
                            int.class, long.class, double.class, float.class })
            .orElseThrow(
                    () -> new IllegalStateException("Could not find method having ten parameters for testing."));
}
 
Example 15
Source Project: microshed-testing   Source File: RestClientBuilder.java    License: Apache License 2.0 4 votes vote down vote up
private static String locateApplicationPath(Class<?> clazz) {
    String resourcePackage = clazz.getPackage().getName();

    // If the rest client directly extends Application, look for ApplicationPath on it
    if (AnnotationSupport.isAnnotated(clazz, ApplicationPath.class))
        return AnnotationSupport.findAnnotation(clazz, ApplicationPath.class).get().value();

    // First check for a javax.ws.rs.core.Application in the same package as the resource
    List<Class<?>> appClasses = ReflectionSupport.findAllClassesInPackage(resourcePackage,
                                                                          c -> Application.class.isAssignableFrom(c) &&
                                                                               AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                          n -> true);
    if (appClasses.size() == 0) {
        LOG.debug("no classes implementing Application found in pkg: " + resourcePackage);
        // If not found, check under the 3rd package, so com.foo.bar.*
        // Classpath scanning can be expensive, so we jump straight to the 3rd package from root instead
        // of recursing up one package at a time and scanning the entire CP for each step
        String[] pkgs = resourcePackage.split("\\.");
        if (pkgs.length > 3) {
            String checkPkg = pkgs[0] + '.' + pkgs[1] + '.' + pkgs[2];
            LOG.debug("checking in pkg: " + checkPkg);
            appClasses = ReflectionSupport.findAllClassesInPackage(checkPkg,
                                                                   c -> Application.class.isAssignableFrom(c) &&
                                                                        AnnotationSupport.isAnnotated(c, ApplicationPath.class),
                                                                   n -> true);
        }
    }

    if (appClasses.size() == 0) {
        LOG.info("No classes implementing 'javax.ws.rs.core.Application' found on classpath to set as context root for " + clazz +
                 ". Defaulting context root to '/'");
        return "";
    }

    Class<?> selectedClass = appClasses.stream()
                    .sorted((c1, c2) -> c1.getName().compareTo(c2.getName()))
                    .findFirst()
                    .get();
    ApplicationPath appPath = AnnotationSupport.findAnnotation(selectedClass, ApplicationPath.class).get();
    if (appClasses.size() > 1) {
        LOG.warn("Found multiple classes implementing 'javax.ws.rs.core.Application' on classpath: " + appClasses +
                 ". Setting context root to the first class discovered (" + selectedClass.getCanonicalName() + ") with path: " +
                 appPath.value());
    }
    LOG.debug("Using ApplicationPath of '" + appPath.value() + "'");
    return appPath.value();
}