Example usage for org.apache.commons.lang3.reflect FieldUtils readDeclaredField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils readDeclaredField

Introduction

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

Prototype

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

Source Link

Document

Gets a Field value by name.

Usage

From source file:org.force66.circuit.CallableCircuitTest.java

@Test
public void testCallableCircuit() throws Exception {
    CallableCircuit<String> callable = new CallableCircuit<String>(circuit, myCallable);
    Assert.assertEquals(circuit, FieldUtils.readDeclaredField(callable, "localCircuit", true));
    Assert.assertEquals(myCallable, FieldUtils.readDeclaredField(callable, "localOperation", true));

    try {//from  w ww. j a  va  2  s. c  o m
        callable = new CallableCircuit<String>(null, myCallable);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("not allowed"));
    }

    try {
        callable = new CallableCircuit<String>(circuit, null);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("not allowed"));
    }
}

From source file:org.force66.mediator.Slf4jTaskErrorHandlerTest.java

@Test
public void testHandle() throws Exception {
    String errorMessage = (String) FieldUtils.readDeclaredField(taskErrorHandler, "message", true);
    Exception exception = new RuntimeException("crap");

    // Test ERROR logging
    taskErrorHandler.handle(exception, null);
    Mockito.verify(logger).error(errorMessage, exception);

    // Test WARNING logging
    taskErrorHandler = new Slf4jTaskErrorHandler(logger, errorMessage,
            Slf4jTaskErrorHandler.Slf4jLogLevel.WARNING);
    taskErrorHandler.handle(exception, null);
    Mockito.verify(logger).warn(errorMessage, exception);

    // Test INFO logging
    taskErrorHandler = new Slf4jTaskErrorHandler(logger, errorMessage,
            Slf4jTaskErrorHandler.Slf4jLogLevel.INFO);
    taskErrorHandler.handle(exception, null);
    Mockito.verify(logger).info(errorMessage, exception);

    // Test TRACE logging
    taskErrorHandler = new Slf4jTaskErrorHandler(logger, errorMessage,
            Slf4jTaskErrorHandler.Slf4jLogLevel.TRACE);
    taskErrorHandler.handle(exception, null);
    Mockito.verify(logger).trace(errorMessage, exception);
}

From source file:org.force66.mediator.Slf4jTaskErrorHandlerTest.java

@Test
public void testSlf4jTaskErrorHandler() throws Exception {
    // Logger constructor test
    Assert.assertEquals(logger, FieldUtils.readDeclaredField(taskErrorHandler, "logger", true));
    Assert.assertEquals(Slf4jTaskErrorHandler.DEFAULT_MESSAGE,
            FieldUtils.readDeclaredField(taskErrorHandler, "message", true));
    Assert.assertEquals(Slf4jTaskErrorHandler.DEFAULT_LOG_LEVEL.ERROR,
            FieldUtils.readDeclaredField(taskErrorHandler, "logLevel", true));

    // Logger/Message constructor test
    taskErrorHandler = new Slf4jTaskErrorHandler(logger, "MyMessage");
    Assert.assertEquals(logger, FieldUtils.readDeclaredField(taskErrorHandler, "logger", true));
    Assert.assertEquals("MyMessage", FieldUtils.readDeclaredField(taskErrorHandler, "message", true));
    Assert.assertEquals(Slf4jTaskErrorHandler.DEFAULT_LOG_LEVEL.ERROR,
            FieldUtils.readDeclaredField(taskErrorHandler, "logLevel", true));

    // Logger/Message/Error level constructor test
    taskErrorHandler = new Slf4jTaskErrorHandler(logger, "MyMessage",
            Slf4jTaskErrorHandler.Slf4jLogLevel.WARNING);
    Assert.assertEquals(logger, FieldUtils.readDeclaredField(taskErrorHandler, "logger", true));
    Assert.assertEquals("MyMessage", FieldUtils.readDeclaredField(taskErrorHandler, "message", true));
    Assert.assertEquals(Slf4jTaskErrorHandler.Slf4jLogLevel.WARNING,
            FieldUtils.readDeclaredField(taskErrorHandler, "logLevel", true));

    // Null Constructor test
    taskErrorHandler = new Slf4jTaskErrorHandler();
    Assert.assertNotNull(FieldUtils.readDeclaredField(taskErrorHandler, "logger", true));
    Assert.assertEquals(Slf4jTaskErrorHandler.DEFAULT_MESSAGE,
            FieldUtils.readDeclaredField(taskErrorHandler, "message", true));
    Assert.assertEquals(Slf4jTaskErrorHandler.DEFAULT_LOG_LEVEL.ERROR,
            FieldUtils.readDeclaredField(taskErrorHandler, "logLevel", true));
}

From source file:org.force66.mediator.TaskMediatorTest.java

@Test
public void testTaskMediatorTTaskErrorHandler() throws Exception {
    // Response/Error handler constructor test
    Assert.assertEquals(TEST_RESPONSE, FieldUtils.readDeclaredField(taskMediator, "defaultResponse", true));
    Assert.assertEquals(errorHandler, FieldUtils.readDeclaredField(taskMediator, "taskErrorHandler", true));

    try {//from w  w w.  j av  a 2  s  .  co m
        taskMediator = new TaskMediator<String>(null, errorHandler);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("not allowed"));
    }
    try {
        taskMediator = new TaskMediator<String>(TEST_RESPONSE, null);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("not allowed"));
    }

    // Response constructor test
    taskMediator = new TaskMediator<String>(TEST_RESPONSE);
    Assert.assertTrue(FieldUtils.readDeclaredField(taskMediator, "taskErrorHandler",
            true) instanceof Slf4jTaskErrorHandler);
}

From source file:org.jodconverter.process.ProcessManagerTest.java

/**
 * Tests the UnixProcessManager class./*w  w w.j  a  v  a  2s . c  om*/
 *
 * @throws Exception if an error occurs.
 */
@Test
public void unixProcessManager() throws Exception {
    assumeTrue(SystemUtils.IS_OS_UNIX && !SystemUtils.IS_OS_MAC);

    final ProcessManager processManager = UnixProcessManager.getDefault();
    final Process process = Runtime.getRuntime().exec("sleep 5s");
    final ProcessQuery query = new ProcessQuery("sleep", "5s");

    final long pid = processManager.findPid(query);
    assertThat(pid).isNotEqualTo(ProcessManager.PID_NOT_FOUND);
    final Number javaPid = (Number) FieldUtils.readDeclaredField(process, "pid", true);
    assertThat(pid).isEqualTo(javaPid.longValue());

    processManager.kill(process, pid);
    assertThat(processManager.findPid(query)).isEqualTo(ProcessManager.PID_NOT_FOUND);
}

From source file:org.jodconverter.process.ProcessManagerTest.java

/**
 * Tests the MacProcessManager class.//w  w  w  .  j  a  va  2s.  c  om
 *
 * @throws Exception if an error occurs.
 */
@Test
public void macProcessManager() throws Exception {
    assumeTrue(SystemUtils.IS_OS_MAC);

    final ProcessManager processManager = MacProcessManager.getDefault();
    final Process process = Runtime.getRuntime().exec("sleep 5s");
    final ProcessQuery query = new ProcessQuery("sleep", "5s");

    final long pid = processManager.findPid(query);
    assertThat(pid).isNotEqualTo(ProcessManager.PID_NOT_FOUND);
    final Number javaPid = (Number) FieldUtils.readDeclaredField(process, "pid", true);

    assertThat(pid).isEqualTo(javaPid.longValue());

    processManager.kill(process, pid);
    assertThat(processManager.findPid(query)).isEqualTo(ProcessManager.PID_NOT_FOUND);
}

From source file:org.silverpeas.components.questionreply.service.SilverpeasQuestionManagerReplayDaoTest.java

@BeforeEach
public void setup() throws IllegalAccessException {
    SilverpeasQuestionManager manager = new SilverpeasQuestionManager();
    replyDao = (SilverpeasBeanDAOImpl) FieldUtils.readDeclaredField(manager, "replyDao", true);
}

From source file:org.silverpeas.components.questionreply.service.SilverpeasQuestionManagerReplayDaoTest.java

@SuppressWarnings("unchecked")
@Test/*from  w  w w. j  av  a  2s . com*/
public void verifyValidProperties() throws IllegalAccessException {
    List<String> replayDaoBeanPropertyNames = ((List<PropertyDescriptor>) FieldUtils.readDeclaredField(replyDao,
            "validProperties", true)).stream().map(FeatureDescriptor::getName).collect(Collectors.toList());
    assertThat(replayDaoBeanPropertyNames, containsInAnyOrder(EXPECTED_PROPERTIES));
}

From source file:org.silverpeas.core.chat.ChatUserTest.java

private ChatUser createChatUserWithLogin(final String login) throws IllegalAccessException {
    final ChatUser chatUser = ChatUser.getById("not the point");
    final UserDetail user = (UserDetail) FieldUtils.readDeclaredField(chatUser, "user", true);
    user.setLogin(login);//from ww w  . j  av  a  2s.  co  m
    return chatUser;
}