List of usage examples for org.apache.commons.lang.reflect FieldUtils writeField
public static void writeField(Object target, String fieldName, Object value, boolean forceAccess) throws IllegalAccessException
From source file:org.commonjava.maven.ext.manip.impl.JSONManipulatorTest.java
@Before public void setup() throws IOException, IllegalAccessException, URISyntaxException { FieldUtils.writeField(jsonManipulator, "jsonIO", new JSONIO(), true); URL resource = JSONIOTest.class.getResource("npm-shrinkwrap.json"); npmFile = tf.newFile();/*from w w w . ja va 2 s . co m*/ pluginFile = tf.newFile(); FileUtils.copyURLToFile(resource, npmFile); URL resource2 = JSONIOTest.class.getResource("amg-plugin-registry.json"); FileUtils.copyURLToFile(resource2, pluginFile); }
From source file:org.commonjava.maven.ext.manip.impl.XMLManipulatorTest.java
@Before public void setup() throws IOException, IllegalAccessException, URISyntaxException { FieldUtils.writeField(xmlManipulator, "xmlIO", new XMLIO(), true); URL resource = XMLIOTest.class.getResource("activemq-artemis-dep.xml"); xmlFile = tf.newFile();//from w w w. j a v a 2 s . c o m FileUtils.copyURLToFile(resource, xmlFile); }
From source file:org.commonjava.maven.ext.manip.io.ModelResolverTest.java
@Test(expected = ManipulationException.class) @BMRule(name = "retrieve-first-null", targetClass = "ArtifactManagerImpl", targetMethod = "retrieveFirst(List<? extends Location> locations, ArtifactRef ref)", targetLocation = "AT ENTRY", action = "RETURN null") public void resolveArtifactTest() throws Exception { final ManipulationSession session = new ManipulationSession(); final GalleyInfrastructure galleyInfra = new GalleyInfrastructure(session.getTargetDir(), session.getRemoteRepositories(), session.getLocalRepository(), session.getSettings(), session.getActiveProfiles(), null, null, null, temp.newFolder("cache-dir")); final GalleyAPIWrapper wrapper = new GalleyAPIWrapper(galleyInfra); final ModelIO model = new ModelIO(); FieldUtils.writeField(model, "galleyWrapper", wrapper, true); model.resolveRawModel(SimpleProjectVersionRef.parse("org.commonjava:commonjava:5")); }
From source file:org.eclipse.smarthome.config.core.Configuration.java
public <T> T as(Class<T> configurationClass) { synchronized (this) { T configuration = null;/* ww w.ja v a2 s . co m*/ try { configuration = configurationClass.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { logger.error("Could not create configuration instance: " + ex.getMessage(), ex); return null; } List<Field> fields = getAllFields(configurationClass); for (Field field : fields) { String fieldName = field.getName(); String typeName = field.getType().getSimpleName(); Object value = properties.get(fieldName); if (value == null && field.getType().isPrimitive()) { logger.debug("Skipping field '{}', because it's primitive data type and value is not set", fieldName); continue; } try { if (value != null && value instanceof BigDecimal && !typeName.equals("BigDecimal")) { BigDecimal bdValue = (BigDecimal) value; if (typeName.equalsIgnoreCase("Float")) { value = bdValue.floatValue(); } else if (typeName.equalsIgnoreCase("Double")) { value = bdValue.doubleValue(); } else if (typeName.equalsIgnoreCase("Long")) { value = bdValue.longValue(); } else if (typeName.equalsIgnoreCase("Integer") || typeName.equalsIgnoreCase("int")) { value = bdValue.intValue(); } } if (value != null) { logger.debug("Setting value ({}) {} to field '{}' in configuration class {}", typeName, value, fieldName, configurationClass.getName()); FieldUtils.writeField(configuration, fieldName, value, true); } } catch (Exception ex) { logger.warn("Could not set field value for field '" + fieldName + "': " + ex.getMessage(), ex); } } return configuration; } }
From source file:org.eclipse.smarthome.config.core.internal.ConfigMapper.java
/** * Use this method to automatically map a configuration collection to a Configuration holder object. A common * use-case would be within a service. Usage example: * * <pre>/* w w w . j a v a 2 s . c om*/ * {@code * public void modified(Map<String, Object> properties) { * YourConfig config = ConfigMapper.as(properties, YourConfig.class); * } * } * </pre> * * * @param properties The configuration map. * @param configurationClass The configuration holder class. An instance of this will be created so make sure that * a default constructor is available. * @return The configuration holder object. All fields that matched a configuration option are set. If a required * field is not set, null is returned. */ public static <T> @Nullable T as(Map<String, Object> properties, Class<T> configurationClass) { T configuration = null; try { configuration = configurationClass.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { return null; } List<Field> fields = getAllFields(configurationClass); for (Field field : fields) { // Don't try to write to final fields and ignore transient fields if (Modifier.isFinal(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) { continue; } String fieldName = field.getName(); String configKey = fieldName; Class<?> type = field.getType(); Object value = properties.get(configKey); // Consider RequiredField annotations if (value == null) { logger.trace("Skipping field '{}', because config has no entry for {}", fieldName, configKey); continue; } // Allows to have List<int>, List<Double>, List<String> etc if (value instanceof Collection) { Collection<?> c = (Collection<?>) value; Class<?> innerClass = (Class<?>) ((ParameterizedType) field.getGenericType()) .getActualTypeArguments()[0]; final List<Object> lst = new ArrayList<>(c.size()); for (final Object it : c) { final Object normalized = objectConvert(it, innerClass); lst.add(normalized); } value = lst; } try { value = objectConvert(value, type); logger.trace("Setting value ({}) {} to field '{}' in configuration class {}", type.getSimpleName(), value, fieldName, configurationClass.getName()); FieldUtils.writeField(configuration, fieldName, value, true); } catch (Exception ex) { logger.warn("Could not set field value for field '{}': {}", fieldName, ex.getMessage(), ex); } } return configuration; }
From source file:org.eclipse.smarthome.io.net.http.internal.ExtensibleTrustManagerImplTest.java
@Test public void shouldBeResilientAgainstNullSubjectAlternativeNames() throws CertificateException, IllegalAccessException { FieldUtils.writeField(subject, "defaultTrustManager", defaultTrustManager, true); when(topOfChain.getSubjectX500Principal()) .thenReturn(new X500Principal("CN=example.com, OU=Smarthome, O=Eclipse, C=DE")); when(topOfChain.getSubjectAlternativeNames()).thenReturn(null); subject.checkClientTrusted(chain, "just"); verify(defaultTrustManager).checkClientTrusted(chain, "just", (Socket) null); verifyNoMoreInteractions(trustmanager); }
From source file:org.eclipse.smarthome.io.net.http.internal.ExtensibleTrustManagerImplTest.java
@Test public void shouldBeResilientAgainstMissingCommonNames() throws CertificateException, IllegalAccessException { FieldUtils.writeField(subject, "defaultTrustManager", defaultTrustManager, true); when(topOfChain.getSubjectX500Principal()).thenReturn(new X500Principal("OU=Smarthome, O=Eclipse, C=DE")); subject.checkClientTrusted(chain, "just"); verify(defaultTrustManager).checkClientTrusted(chain, "just", (Socket) null); verifyNoMoreInteractions(trustmanager); }
From source file:org.eclipse.smarthome.io.net.http.internal.ExtensibleTrustManagerImplTest.java
@Test public void shouldBeResilientAgainstInvalidCertificates() throws CertificateException, IllegalAccessException { FieldUtils.writeField(subject, "defaultTrustManager", defaultTrustManager, true); when(topOfChain.getSubjectX500Principal()) .thenReturn(new X500Principal("CN=example.com, OU=Smarthome, O=Eclipse, C=DE")); when(topOfChain.getSubjectAlternativeNames()) .thenThrow(new CertificateParsingException("Invalid certificate!!!")); subject.checkClientTrusted(chain, "just"); verify(defaultTrustManager).checkClientTrusted(chain, "just", (Socket) null); verifyNoMoreInteractions(trustmanager); }
From source file:org.eclipse.smarthome.io.net.http.internal.ExtensibleTrustManagerImplTest.java
@Test public void shouldNotForwardCallsToMockForDifferentCN() throws CertificateException, IllegalAccessException { FieldUtils.writeField(subject, "defaultTrustManager", defaultTrustManager, true); mockSubjectForCertificate(topOfChain, "CN=example.com, OU=Smarthome, O=Eclipse, C=DE"); mockIssuerForCertificate(topOfChain, "CN=Eclipse, OU=Smarthome, O=Eclipse, C=DE"); mockSubjectForCertificate(bottomOfChain, "CN=Eclipse, OU=Smarthome, O=Eclipse, C=DE"); mockIssuerForCertificate(bottomOfChain, ""); when(topOfChain.getEncoded()).thenReturn(new byte[0]); subject.checkServerTrusted(chain, "just"); verify(defaultTrustManager).checkServerTrusted(chain, "just", (Socket) null); verifyZeroInteractions(trustmanager); }
From source file:org.edgexfoundry.scheduling.SchedulerTest.java
@Test public void testCreateScheduleContextForExisting() throws IllegalAccessException { Schedule schedule = ScheduleData.newTestInstance(); ScheduleContext scheduleContext = new ScheduleContext(schedule); Map<String, ScheduleContext> map = new HashMap<>(); map.put(schedule.getId(), scheduleContext); FieldUtils.writeField(scheduler, "scheduleIdToScheduleContextMap", map, true); assertFalse("Schedule context should already exist", scheduler.createScheduleContext(schedule)); }