Example usage for org.apache.commons.lang.reflect FieldUtils readField

List of usage examples for org.apache.commons.lang.reflect FieldUtils readField

Introduction

In this page you can find the example usage for org.apache.commons.lang.reflect FieldUtils readField.

Prototype

public static Object readField(Object target, String fieldName, boolean forceAccess)
        throws IllegalAccessException 

Source Link

Document

Read the named field.

Usage

From source file:org.alfresco.solr.tracker.AclTrackerTest.java

@Test
public void checkTrackingWhenAclChangeSetsToPurge() throws IllegalAccessException, IOException {
    @SuppressWarnings("unchecked")
    ConcurrentLinkedQueue<Long> aclChangeSetsToPurge = (ConcurrentLinkedQueue<Long>) FieldUtils
            .readField(tracker, "aclChangeSetsToPurge", true);
    aclChangeSetsToPurge.add(101L);//  w w w  . j  a  v a  2  s . c  om
    aclChangeSetsToPurge.add(102L);
    aclChangeSetsToPurge.add(103L);

    // Invoke the behaviour we're testing
    testTrackChangesRan();

    // aclChangeSetsToPurge
    verify(informationServer).deleteByAclChangeSetId(101L);
    verify(informationServer).deleteByAclChangeSetId(102L);
    verify(informationServer).deleteByAclChangeSetId(103L);

    // TODO: verify checkShutdown
    verify(informationServer).commit();
}

From source file:org.alfresco.solr.tracker.AclTrackerTest.java

@Test
public void checkTrackingWhenAclsToPurge() throws IllegalAccessException, IOException {
    @SuppressWarnings("unchecked")
    ConcurrentLinkedQueue<Long> aclsToPurge = (ConcurrentLinkedQueue<Long>) FieldUtils.readField(tracker,
            "aclsToPurge", true);
    aclsToPurge.add(201L);//ww  w  .j a va2s.co m
    aclsToPurge.add(202L);
    aclsToPurge.add(203L);

    // Invoke the behaviour we're testing
    testTrackChangesRan();

    // aclsToPurge
    verify(informationServer).deleteByAclId(201L);
    verify(informationServer).deleteByAclId(202L);
    verify(informationServer).deleteByAclId(203L);

    // TODO: verify checkShutdown
    verify(informationServer).commit();
}

From source file:org.alfresco.solr.tracker.AclTrackerTest.java

@Test
public void canClose() throws IllegalAccessException {
    ThreadHandler threadHandler = (ThreadHandler) FieldUtils.readField(tracker, "threadHandler", true);
    threadHandler = spy(threadHandler);/*from   w w  w . j a  v a 2s  .  c  o m*/
    FieldUtils.writeField(tracker, "threadHandler", threadHandler, true);

    tracker.close();

    // AclTracker specific
    verify(threadHandler).shutDownThreadPool();

    // Applicable to all AbstractAclTracker
    verify(client).close();
}

From source file:org.apache.camel.component.spring.batch.SpringBatchEndpointTest.java

@Test
public void shouldInjectJobToEndpoint() throws IllegalAccessException {
    SpringBatchEndpoint batchEndpoint = getMandatoryEndpoint("spring-batch:mockJob", SpringBatchEndpoint.class);
    Job batchEndpointJob = (Job) FieldUtils.readField(batchEndpoint, "job", true);
    assertSame(job, batchEndpointJob);/*from   w w  w .j a  v  a2 s .c  om*/
}

From source file:org.apache.camel.component.spring.batch.SpringBatchEndpointTest.java

@Test
public void shouldInjectJobLauncherByReferenceName() throws Exception {
    // Given/*w w w.  j  a  v  a 2 s  .c om*/
    context().addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:launcherRefTest").to("spring-batch:mockJob?jobLauncherRef=alternativeJobLauncher");
        }
    });

    // When
    template.sendBody("direct:launcherRefTest", "Start the job, please.");

    // Then
    SpringBatchEndpoint batchEndpoint = context().getEndpoint(
            "spring-batch:mockJob?jobLauncherRef=alternativeJobLauncher", SpringBatchEndpoint.class);
    JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher",
            true);
    assertSame(alternativeJobLauncher, batchEndpointJobLauncher);
}

From source file:org.apache.camel.component.spring.batch.SpringBatchEndpointTest.java

@Test
public void shouldResolveAnyJobLauncher() throws Exception {
    // Given/*from  w  ww .j  av  a 2 s  . c o  m*/
    SimpleRegistry registry = new SimpleRegistry();
    registry.put("mockJob", job);
    registry.put("someRandomName", jobLauncher);
    CamelContext camelContext = new DefaultCamelContext(registry);
    camelContext.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("spring-batch:mockJob");
        }
    });

    // When
    camelContext.start();

    // Then
    SpringBatchEndpoint batchEndpoint = camelContext.getEndpoint("spring-batch:mockJob",
            SpringBatchEndpoint.class);
    JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher",
            true);
    assertSame(jobLauncher, batchEndpointJobLauncher);
}

From source file:org.apache.camel.component.spring.batch.SpringBatchEndpointTest.java

@Test
public void shouldUseJobLauncherFromComponent() throws Exception {
    // Given//from w w w .jav  a2 s .  c o m
    SpringBatchComponent batchComponent = new SpringBatchComponent();
    batchComponent.setJobLauncher(alternativeJobLauncher);
    context.addComponent("customBatchComponent", batchComponent);

    // When
    context().addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:startCustom").to("customBatchComponent:mockJob");
        }
    });

    // Then
    SpringBatchEndpoint batchEndpoint = context().getEndpoint("customBatchComponent:mockJob",
            SpringBatchEndpoint.class);
    JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher",
            true);
    assertSame(alternativeJobLauncher, batchEndpointJobLauncher);
}

From source file:org.apache.hadoop.fs.s3a.TestS3AConfiguration.java

/**
 * Reads and returns a field from an object using reflection.  If the field
 * cannot be found, is null, or is not the expected type, then this method
 * fails the test.// w  w  w.  j  a v a 2  s.  c o m
 *
 * @param target object to read
 * @param fieldType type of field to read, which will also be the return type
 * @param fieldName name of field to read
 * @return field that was read
 * @throws IllegalAccessException if access not allowed
 */
private static <T> T getField(Object target, Class<T> fieldType, String fieldName)
        throws IllegalAccessException {
    Object obj = FieldUtils.readField(target, fieldName, true);
    assertNotNull(String.format("Could not read field named %s in object with class %s.", fieldName,
            target.getClass().getName()), obj);
    assertTrue(String.format("Unexpected type found for field named %s, expected %s, actual %s.", fieldName,
            fieldType.getName(), obj.getClass().getName()), fieldType.isAssignableFrom(obj.getClass()));
    return fieldType.cast(obj);
}

From source file:org.apache.zeppelin.interpreter.Interpreter.java

/**
 * Replace markers #{contextFieldName} by values from {@link InterpreterContext} fields
 * with same name and marker #{user}. If value == null then replace by empty string.
 *//*w  ww  .j  av a  2s .  c  o  m*/
private void replaceContextParameters(Properties properties) {
    InterpreterContext interpreterContext = InterpreterContext.get();
    if (interpreterContext != null) {
        String markerTemplate = "#\\{%s\\}";
        List<String> skipFields = Arrays.asList("paragraphTitle", "paragraphId", "paragraphText");
        List typesToProcess = Arrays.asList(String.class, Double.class, Float.class, Short.class, Byte.class,
                Character.class, Boolean.class, Integer.class, Long.class);
        for (String key : properties.stringPropertyNames()) {
            String p = properties.getProperty(key);
            if (StringUtils.isNotEmpty(p)) {
                for (Field field : InterpreterContext.class.getDeclaredFields()) {
                    Class clazz = field.getType();
                    if (!skipFields.contains(field.getName())
                            && (typesToProcess.contains(clazz) || clazz.isPrimitive())) {
                        Object value = null;
                        try {
                            value = FieldUtils.readField(field, interpreterContext, true);
                        } catch (Exception e) {
                            logger.error("Cannot read value of field {0}", field.getName());
                        }
                        p = p.replaceAll(String.format(markerTemplate, field.getName()),
                                value != null ? value.toString() : StringUtils.EMPTY);
                    }
                }
                p = p.replaceAll(String.format(markerTemplate, "user"),
                        StringUtils.defaultString(userName, StringUtils.EMPTY));
                properties.setProperty(key, p);
            }
        }
    }
}

From source file:org.commonjava.maven.ext.cli.CliTest.java

@Test
public void checkTargetMatches() throws Exception {
    Cli c = new Cli();
    File pom1 = temp.newFile();/*w w  w . ja  va 2s.c om*/
    File settings = writeSettings(temp.newFile());

    executeMethod(c, "createSession", new Object[] { pom1, settings });

    assertTrue("Session file should match",
            pom1.equals(((ManipulationSession) FieldUtils.readField(c, "session", true)).getPom()));
}