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

The following examples show how to use org.junit.platform.commons.support.ReflectionSupport. 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
Source File: NamedArgumentPlaceholderTest.java    From junit-dataprovider with Apache License 2.0 6 votes vote down vote up
@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 File: JAXRSUtilities.java    From microprofile-sandbox with 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
Source File: CustomConverterDataProviderArgumentProvider.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
@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
Source File: DataProviderTestProvider.java    From junit-dataprovider with 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 #5
Source File: CustomResolverUseDataProviderArgumentProvider.java    From junit-dataprovider with 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 #6
Source File: UseDataProviderArgumentProvider.java    From junit-dataprovider with 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 #7
Source File: StringDataProviderArgumentProvider.java    From junit-dataprovider with 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 #8
Source File: CustomConverterDataProviderExtension.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
@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
Source File: CustomPlaceholderDataProviderExtension.java    From junit-dataprovider with 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 #10
Source File: DataProviderTestExtension.java    From junit-dataprovider with 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 #11
Source File: CustomResolverDataProviderTestExtension.java    From junit-dataprovider with 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 #12
Source File: UseDataProviderExtension.java    From junit-dataprovider with 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 #13
Source File: DataProviderExtension.java    From junit-dataprovider with 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
Source File: NamedArgumentPlaceholderTest.java    From junit-dataprovider with Apache License 2.0 5 votes vote down vote up
@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 File: RestClientBuilder.java    From microshed-testing with 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();
}