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.apache.jackrabbit.oak.jcr.session.SessionStatsTest.java

private String getInitStackTrace(Session session) throws IllegalAccessException {
    SessionDelegate sessionDelegate = (SessionDelegate) FieldUtils.readDeclaredField(session, "sd", true);
    SessionStats sessionStats = sessionDelegate.getSessionStats();
    return sessionStats.getInitStackTrace();
}

From source file:org.artofsolving.jodconverter.office.ExternalOfficeManagerTest.java

public void executeTask() throws Exception {
    UnoUrl unoUrl = UnoUrlUtils.socket(2002);
    OfficeProcess officeProcess = new OfficeProcess(OfficeUtils.getDefaultOfficeHome(), unoUrl, null, null,
            new File(System.getProperty("java.io.tmpdir")), new PureJavaProcessManager(), true);
    officeProcess.start();//w  w w  . j a  v a  2s  . co m
    Thread.sleep(2000);
    Integer exitCode = officeProcess.getExitCode();
    if (exitCode != null && exitCode.equals(Integer.valueOf(81))) {
        officeProcess.start(true);
        Thread.sleep(2000);
    }

    ExternalOfficeManager manager = new ExternalOfficeManager(unoUrl, true);
    manager.start();

    MockOfficeTask task = new MockOfficeTask();
    manager.execute(task);
    assertTrue(task.isCompleted());

    manager.stop();
    //TODO replace when OfficeProcess has a forciblyTerminate()
    Process process = (Process) FieldUtils.readDeclaredField(officeProcess, "process", true);
    process.destroy();
}

From source file:org.artofsolving.jodconverter.office.PooledOfficeManagerTest.java

public void executeTask() throws Exception {

    final PooledOfficeManagerSettings settings = new PooledOfficeManagerSettings(CONNECTION_MODE);
    settings.setProcessManager(OfficeUtils.findBestProcessManager());
    final PooledOfficeManager officeManager = new PooledOfficeManager(settings);
    ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) FieldUtils
            .readDeclaredField(officeManager, "managedOfficeProcess", true);
    OfficeProcess process = (OfficeProcess) FieldUtils.readDeclaredField(managedOfficeProcess, "process", true);
    OfficeConnection connection = (OfficeConnection) FieldUtils.readDeclaredField(managedOfficeProcess,
            "connection", true);

    try {/*from   w  ww  .j a va 2  s.c  om*/
        officeManager.start();
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        MockOfficeTask task = new MockOfficeTask();
        officeManager.execute(task);
        assertTrue(task.isCompleted());

    } finally {
        officeManager.stop();
        assertFalse(connection.isConnected());
        assertFalse(process.isRunning());
        assertEquals(process.getExitCode(0, 0), 0);
    }
}

From source file:org.artofsolving.jodconverter.office.PooledOfficeManagerTest.java

public void restartAfterCrash() throws Exception {

    final PooledOfficeManagerSettings settings = new PooledOfficeManagerSettings(CONNECTION_MODE);
    settings.setProcessManager(OfficeUtils.findBestProcessManager());
    final PooledOfficeManager officeManager = new PooledOfficeManager(settings);
    ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) FieldUtils
            .readDeclaredField(officeManager, "managedOfficeProcess", true);
    OfficeProcess process = (OfficeProcess) FieldUtils.readDeclaredField(managedOfficeProcess, "process", true);
    OfficeConnection connection = (OfficeConnection) FieldUtils.readDeclaredField(managedOfficeProcess,
            "connection", true);
    assertNotNull(connection);/*from   ww w  .  j  a va  2  s  .  c om*/

    try {
        officeManager.start();
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        new Thread() {
            public void run() {
                MockOfficeTask badTask = new MockOfficeTask(10 * 1000);
                try {
                    officeManager.execute(badTask);
                    fail("task should be cancelled");
                    //FIXME being in a separate thread the test won't actually fail
                } catch (OfficeException officeEx) {
                    assertTrue(officeEx.getCause() instanceof CancellationException);
                }
            }
        }.start();
        Thread.sleep(500);
        Process underlyingProcess = (Process) FieldUtils.readDeclaredField(process, "process", true);
        assertNotNull(underlyingProcess);
        logger.debug("Simulating the crash");
        underlyingProcess.destroy(); // simulate crash

        Thread.sleep(RESTART_WAIT_TIME);
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        MockOfficeTask goodTask = new MockOfficeTask();
        officeManager.execute(goodTask);
        assertTrue(goodTask.isCompleted());

    } finally {
        officeManager.stop();
        assertFalse(connection.isConnected());
        assertFalse(process.isRunning());
        assertEquals(process.getExitCode(0, 0), 0);
    }
}

From source file:org.artofsolving.jodconverter.office.PooledOfficeManagerTest.java

public void restartAfterTaskTimeout() throws Exception {
    final PooledOfficeManagerSettings settings = new PooledOfficeManagerSettings(CONNECTION_MODE);
    settings.setProcessManager(OfficeUtils.findBestProcessManager());
    settings.setTaskExecutionTimeout(1500L);
    final PooledOfficeManager officeManager = new PooledOfficeManager(settings);

    ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) FieldUtils
            .readDeclaredField(officeManager, "managedOfficeProcess", true);
    OfficeProcess process = (OfficeProcess) FieldUtils.readDeclaredField(managedOfficeProcess, "process", true);
    OfficeConnection connection = (OfficeConnection) FieldUtils.readDeclaredField(managedOfficeProcess,
            "connection", true);
    assertNotNull(connection);/* ww  w .ja v a 2  s  .  co  m*/

    try {
        officeManager.start();
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        MockOfficeTask task = new MockOfficeTask(2000);
        try {
            officeManager.execute(task);
            fail("task should be timed out");
        } catch (OfficeException officeEx) {
            assertTrue(officeEx.getCause() instanceof TimeoutException);
        }

        Thread.sleep(RESTART_WAIT_TIME);

        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        MockOfficeTask goodTask = new MockOfficeTask();
        officeManager.execute(goodTask);
        assertTrue(goodTask.isCompleted());
    } finally {

        officeManager.stop();
        assertFalse(connection.isConnected());
        assertFalse(process.isRunning());
        assertEquals(process.getExitCode(0, 0), 0);
    }
}

From source file:org.artofsolving.jodconverter.office.PooledOfficeManagerTest.java

public void restartWhenMaxTasksPerProcessReached() throws Exception {
    PooledOfficeManagerSettings configuration = new PooledOfficeManagerSettings(CONNECTION_MODE);
    configuration.setMaxTasksPerProcess(3);
    final PooledOfficeManager officeManager = new PooledOfficeManager(configuration);

    ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) FieldUtils
            .readDeclaredField(officeManager, "managedOfficeProcess", true);
    OfficeProcess process = (OfficeProcess) FieldUtils.readDeclaredField(managedOfficeProcess, "process", true);
    OfficeConnection connection = (OfficeConnection) FieldUtils.readDeclaredField(managedOfficeProcess,
            "connection", true);
    assertNotNull(connection);//from  www  .j  a v a 2s  . c o  m

    try {
        officeManager.start();
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        for (int i = 0; i < 3; i++) {
            MockOfficeTask task = new MockOfficeTask();
            officeManager.execute(task);
            assertTrue(task.isCompleted());
            int taskCount = ((AtomicInteger) FieldUtils.readDeclaredField(officeManager, "taskCount", true))
                    .get();
            assertEquals(taskCount, i + 1);
        }

        MockOfficeTask task = new MockOfficeTask();
        officeManager.execute(task);
        assertTrue(task.isCompleted());
        int taskCount = ((AtomicInteger) FieldUtils.readDeclaredField(officeManager, "taskCount", true)).get();
        assertEquals(taskCount, 1);

    } finally {
        officeManager.stop();
        assertFalse(connection.isConnected());
        assertFalse(process.isRunning());
        assertEquals(process.getExitCode(0, 0), 0);
    }
}

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

public void unixProcessManager() throws Exception {
    if (PlatformUtils.isMac() || PlatformUtils.isWindows()) {
        throw new SkipException("UnixProcessManager only works on Unix");
    }/*  w  ww . j  a  v a 2s. c  o m*/
    ProcessManager processManager = new UnixProcessManager();
    Process process = new ProcessBuilder("sleep", "5s").start();
    ProcessQuery query = new ProcessQuery("sleep", "5s");

    long pid = processManager.findPid(query);
    assertFalse(pid == ProcessManager.PID_NOT_FOUND);
    Number javaPid = (Number) FieldUtils.readDeclaredField(process, "pid", true);
    assertEquals(pid, javaPid.longValue());

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

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

public void macProcessManager() throws Exception {
    if (!PlatformUtils.isMac()) {
        throw new SkipException("MacProcessManager only works on Mac");
    }//from www.  j  a  va2 s. c  om
    ProcessManager processManager = new MacProcessManager();
    Process process = new ProcessBuilder("sleep", "5s").start();
    ProcessQuery query = new ProcessQuery("sleep", "5s");

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

    assertEquals(pid, javaPid.longValue());

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

From source file:org.dbg4j.core.DebugUtils.java

/**
 * Method creates <code>Map&lt;String, String&gt;</code> that contains instance field names
 * and their values. By default parameter value evaluates by {@link DefaultEvaluationAdapter} unless another
 * evaluator is set by {@link Adapter} annotation.
 *
 * @param instance// w  w  w  .j  a v  a2s. c o m
 * @param fieldsForDebug
 * @return
 */
@Nonnull
public static Map<String, String> getFieldValues(@Nonnull Object instance,
        @Nonnull Collection<Field> fieldsForDebug) {
    Map<String, String> result = new HashMap<String, String>();

    for (Field field : fieldsForDebug) {
        String fieldValue = DefaultDebuggingAdapter.UNKNOWN_VALUE;
        try {
            EvaluationAdapter evaluationAdapter = null;
            if (field.isAnnotationPresent(Adapter.class)) {
                Adapter adapterAnnotation = field.getAnnotation(Adapter.class);
                evaluationAdapter = adapterAnnotation.value().newInstance();
            }
            if (evaluationAdapter == null) {
                evaluationAdapter = new DefaultEvaluationAdapter();
            }

            if (Modifier.isStatic(field.getModifiers())) {
                fieldValue = evaluationAdapter.evaluate(field.getDeclaringClass(),
                        FieldUtils.readDeclaredStaticField(instance.getClass(), field.getName(), true));
            } else {
                fieldValue = evaluationAdapter.evaluate(field.getDeclaringClass(),
                        FieldUtils.readDeclaredField(instance, field.getName(), true));
            }
        } catch (Exception ignored) {
        }
        result.put(field.getName(), fieldValue);
    }

    return result;
}

From source file:org.force66.beantester.BeanTesterTest.java

private BeanTesterConfiguration findConfiguration() throws IllegalAccessException {
    BeanTesterConfiguration config = (BeanTesterConfiguration) FieldUtils.readDeclaredField(beanTester,
            "configuration", true);
    return config;
}