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

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

Introduction

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

Prototype

public static void writeField(final Object target, final String fieldName, final Object value,
        final boolean forceAccess) throws IllegalAccessException 

Source Link

Document

Writes a Field .

Usage

From source file:org.silverpeas.core.calendar.CalendarEventStubBuilder.java

public CalendarEvent build() {
    try {/*from w ww.j  a v a  2  s  . c o  m*/
        FieldUtils.writeField(event, "id", UuidIdentifier.from(id), true);
        FieldUtils.writeField(event, "component", component, true);
        FieldUtils.writeField(event, "categories", categorySet, true);
        FieldUtils.writeField(event, "recurrence", recurrence, true);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
    return event;
}

From source file:org.silverpeas.core.calendar.CalendarEventStubBuilder.java

public CalendarEventStubBuilder withCreator(final User creator) {
    component.withCreatedBy(creator.getId());
    try {/*ww w  . jav a  2 s.  com*/
        FieldUtils.writeField(component, "creator", creator, true);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
    return this;
}

From source file:org.silverpeas.core.calendar.notification.CalendarContributionReminderUserNotificationTest.java

private void triggerDateTime(DurationReminder reminder) throws IllegalAccessException {
    Optional<Contribution> contribution = contributionManager.getById(reminder.getContributionId());
    if (contribution.isPresent()) {
        final CalendarEvent event = (CalendarEvent) contribution.get();
        final OffsetDateTime occStartDate;
        if (event.isOnAllDay()) {
            occStartDate = ((LocalDate) currentPeriod.getStartDate())
                    .atStartOfDay(event.getCalendar().getZoneId()).toOffsetDateTime();
        } else {/*www.  j ava  2  s  . c  om*/
            occStartDate = (OffsetDateTime) currentPeriod.getStartDate();
        }
        final OffsetDateTime finalDateTime = occStartDate
                .minus(reminder.getDuration(), reminder.getTimeUnit().toChronoUnit()).toZonedDateTime()
                .withZoneSameInstant(ZoneId.systemDefault()).toOffsetDateTime();
        FieldUtils.writeField(reminder, "triggerDateTime", finalDateTime, true);
    }
}

From source file:org.silverpeas.core.contribution.attachment.model.SimpleDocumentTest.java

@Test
public void testGetWebdavContentEditionLanguage(@TestManagedMock WebdavService webDavService) throws Exception {
    SimpleDocument document = new SimpleDocument();
    SimpleAttachment attachment = new SimpleAttachment();
    document.setAttachment(attachment);/* ww w.  j ava 2 s  .  co  m*/
    attachment.setFilename("file.mov");

    /*
    Current file is not an open office compatible one
     */

    reset(webDavService);
    document.release();
    when(webDavService.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn(null);

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    // Trying a test case that must never happen but that shows that the open office compatible
    // condition is respected.
    reset(webDavService);
    document.release();
    when(webDavService.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn("fr");

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    /*
    Current file is now an open office compatible one
     */
    attachment.setFilename("file.odp");

    reset(webDavService);
    document.release();
    when(webDavService.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn(null);

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    reset(webDavService);
    document.release();
    when(webDavService.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn("fr");

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    reset(webDavService);
    document.release();
    FieldUtils.writeField(document, "editedBy", "26", true);
    when(webDavService.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn("fr");

    assertThat(document.getWebdavContentEditionLanguage(), is("fr"));
}

From source file:org.silverpeas.core.contribution.attachment.model.TestSimpleDocument.java

@Test
public void testGetWebdavContentEditionLanguage() throws Exception {
    WebdavService mock = commonAPI4Test.injectIntoMockedBeanContainer(mock(WebdavService.class));

    SimpleDocument document = new SimpleDocument();
    SimpleAttachment attachment = new SimpleAttachment();
    document.setAttachment(attachment);/*from   w w w  .  java2s.c o  m*/
    attachment.setFilename("file.mov");

    /*
    Current file is not an open office compatible one
     */

    reset(mock);
    document.release();
    when(mock.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn(null);

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    // Trying a test case that must never happen but that shows that the open office compatible
    // condition is respected.
    reset(mock);
    document.release();
    when(mock.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn("fr");

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    /*
    Current file is now an open office compatible one
     */
    attachment.setFilename("file.odp");

    reset(mock);
    document.release();
    when(mock.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn(null);

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    reset(mock);
    document.release();
    when(mock.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn("fr");

    assertThat(document.getWebdavContentEditionLanguage(), isEmptyString());

    reset(mock);
    document.release();
    FieldUtils.writeField(document, "editedBy", "26", true);
    when(mock.getContentEditionLanguage(any(SimpleDocument.class))).thenReturn("fr");

    assertThat(document.getWebdavContentEditionLanguage(), is("fr"));
}

From source file:org.silverpeas.core.reminder.ContributionReminderUserNotificationTest.java

private void triggerDateTime(Reminder reminder, OffsetDateTime dateTime) throws IllegalAccessException {
    FieldUtils.writeField(reminder, "triggerDateTime", dateTime, true);
}

From source file:org.silverpeas.core.test.extention.SilverTestEnv.java

private void mockManagedThreadFactory() {
    try {/* w ww . j  a  v  a  2s.  c o m*/
        Constructor<ManagedThreadPool> managedThreadPoolConstructor = ManagedThreadPool.class
                .getDeclaredConstructor();
        managedThreadPoolConstructor.setAccessible(true);
        ManagedThreadPool managedThreadPool = managedThreadPoolConstructor.newInstance();
        ManagedThreadFactory managedThreadFactory = Thread::new;
        FieldUtils.writeField(managedThreadPool, "managedThreadFactory", managedThreadFactory, true);
        when(TestBeanContainer.getMockedBeanContainer().getBeanByType(ManagedThreadPool.class))
                .thenReturn(managedThreadPool);
    } catch (IllegalAccessException | NoSuchMethodException | InstantiationException
            | InvocationTargetException e) {
        throw new SilverpeasRuntimeException(e);
    }
}

From source file:org.silverpeas.core.test.rule.CommonAPI4Test.java

private void managedThreadFactory() {
    try {//  www  .j a  va 2  s .  c  o  m
        Constructor<ManagedThreadPool> managedThreadPoolConstructor = ManagedThreadPool.class
                .getDeclaredConstructor();
        managedThreadPoolConstructor.setAccessible(true);
        ManagedThreadPool managedThreadPool = managedThreadPoolConstructor.newInstance();
        ManagedThreadFactory managedThreadFactory = Thread::new;
        FieldUtils.writeField(managedThreadPool, "managedThreadFactory", managedThreadFactory, true);
        when(TestBeanContainer.getMockedBeanContainer().getBeanByType(ManagedThreadPool.class))
                .thenReturn(managedThreadPool);
    } catch (IllegalAccessException | NoSuchMethodException | InstantiationException
            | InvocationTargetException e) {
        throw new SilverpeasRuntimeException(e);
    }
}

From source file:org.treeingwalker.LessCompilerTest.java

@Before
public void setUp() throws Exception {
    lessCompiler = new LessCompiler();

    when(logger.isDebugEnabled()).thenReturn(false);
    FieldUtils.writeField(lessCompiler, "logger", logger, true);
}

From source file:org.treeingwalker.LessCompilerTest.java

@Test
public void testCompileStringToString() throws Exception {
    mockStatic(Context.class);
    when(Context.enter()).thenReturn(cx);
    FieldUtils.writeField(lessCompiler, "scope", scope, true);
    FieldUtils.writeField(lessCompiler, "compiler", compiler, true);
    FieldUtils.writeField(lessCompiler, "out", out, true);
    when(cx.newObject(scope)).thenReturn(compileScope);

    when(out.toString()).thenReturn(css);

    assertEquals(css, lessCompiler.compile(less));

    verify(compiler).call(cx, compileScope, null, new Object[] {});
}