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

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

Introduction

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

Prototype

public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName,
        final boolean forceAccess) throws IllegalAccessException 

Source Link

Document

Gets the value of a static Field by name.

Usage

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//  www .ja v a  2s.  c  om
 * @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.silverpeas.components.gallery.notification.user.GalleryAlbumMediaSubscriptionNotificationBuilderTest.java

@SuppressWarnings("unchecked")
@BeforeEach/*  w  ww.  j  a  v  a2  s.  co  m*/
void setup(@TestManagedMock OrganizationController organizationController,
        @TestManagedMock SilverpeasComponentInstanceProvider componentInstanceProvider,
        @TestManagedMock NodeService nodeService) throws Exception {
    when(organizationController.getComponentInstLight(componentInstance.getId())).thenReturn(componentInstance);
    final Optional<SilverpeasComponentInstance> optionalInstance = Optional.of(componentInstance);
    when(componentInstanceProvider.getById(componentInstance.getId())).thenReturn(optionalInstance);
    componentImplementations = (Map) FieldUtils.readDeclaredStaticField(ResourceSubscriptionProvider.class,
            "componentImplementations", true);
    componentImplementations.put(componentInstance.getName(), subscriptionService);
    when(subscriptionService.getSubscribersOfSubscriptionResource(any(SubscriptionResource.class)))
            .thenReturn(subscriptionSubscribers);
    subscriptionSubscribers.add(UserSubscriptionSubscriber.from("1"));
    when(nodeService.getPath(albumDetail.getNodePK())).thenReturn(albumPath);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.process.MailContentProcessTest.java

@SuppressWarnings("unchecked")
@BeforeEach//from w  w w  .  j a v  a 2  s  . c o  m
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified from
    // method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.process.MailContentProcessTest.java

@SuppressWarnings("unchecked")
@AfterEach/*from  w ww.jav  a2 s .  c om*/
public void destroy() throws Exception {
    FileUtils.deleteQuietly(originalImageWithResize100x.getParentFile());
    FileUtils.deleteQuietly(originalImageWithResize100x100.getParentFile());

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.process.TestMailContentProcess.java

@SuppressWarnings("unchecked")
@Before//w w w.j  ava 2s . c  om
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified from
    // method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.process.TestMailContentProcess.java

@SuppressWarnings("unchecked")
@After//w  w w.j  av a2s. co  m
public void destroy() throws Exception {
    FileUtils.deleteQuietly(originalImageWithResize100x.getParentFile());
    FileUtils.deleteQuietly(originalImageWithResize100x100.getParentFile());

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.TestWysiwygContentTransformer.java

@SuppressWarnings("unchecked")
@Before//from ww  w . j a  v  a 2  s .  c o m
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified
    // from method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.WysiwygContentTransformerTest.java

@SuppressWarnings("unchecked")
@BeforeEach//w  w  w .  j  av a2 s. c o  m
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified
    // from method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.notification.sse.AbstractServerEventDispatcherTaskTest.java

@BeforeEach
@AfterEach//from   www  . j a va2s.c  o m
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    asyncContextMap = (Set<AsyncContext>) FieldUtils.readDeclaredStaticField(ServerEventDispatcherTask.class,
            "synchronizedContexts", true);
    serverEventStore = (ServerEventStore) FieldUtils.readDeclaredStaticField(ServerEventDispatcherTask.class,
            "serverEventStore", true);
    asyncContextMap.clear();
    serverEventStore.clear();
    FieldUtils.writeDeclaredStaticField(AbstractServerEvent.class, "idCounter", 0L, true);

    new SseLogger().init();
}

From source file:org.silverpeas.core.subscription.AbstractCommonSubscriptionIntegrationTest.java

@After
public void clear() throws Exception {
    Map map = (Map) FieldUtils.readDeclaredStaticField(ResourceSubscriptionProvider.class,
            "componentImplementations", true);
    SilverLogger.getLogger(this)
            .info("Clearing ResourceSubscriptionProvider.componentImplementations which contains {0} "
                    + "implementation {0,choice, 1#instance| 1<instances}", map.size());
    map.clear();/*  w w w. j  a  v  a2  s.co m*/
}