private static <T> T newInstance(ConfigurationMetadata<T> configurationMetadata) { try { return configurationMetadata.getConstructor().newInstance(); } catch (Throwable e) { if (e instanceof InvocationTargetException && e.getCause() != null) { e = e.getCause(); } throw exceptionFor(e, "Error creating instance of configuration class [%s]", configurationMetadata.getConfigClass().getName()); } }
private static <T> T newInstance(ConfigurationMetadata<T> configurationMetadata) { try { return configurationMetadata.getConstructor().newInstance(); } catch (Throwable e) { if (e instanceof InvocationTargetException && e.getCause() != null) { e = e.getCause(); } throw exceptionFor(e, "Error creating instance of configuration class [%s]", configurationMetadata.getConfigClass().getName()); } }
private <T> T newInstance(ConfigurationMetadata<T> configurationMetadata) { try { return configurationMetadata.getConstructor().newInstance(); } catch (Throwable e) { if (e instanceof InvocationTargetException && e.getCause() != null) { e = e.getCause(); } throw exceptionFor(e, "Error creating instance of configuration class [%s]", configurationMetadata.getConfigClass().getName()); } }
@Test public void testNotPublicConstructorClass() throws Exception { TestMonitor monitor = new TestMonitor(); ConfigurationMetadata<?> metadata = ConfigurationMetadata.getConfigurationMetadata(NotPublicConstructorClass.class, monitor); Map<String, Set<String>> expectedAttributes = new HashMap<>(); expectedAttributes.put("Value", ImmutableSet.of("value")); verifyMetaData(metadata, NotPublicConstructorClass.class, null, false, expectedAttributes); monitor.assertNumberOfErrors(1); monitor.assertNumberOfWarnings(0); monitor.assertMatchingErrorRecorded("not public", metadata.getConfigClass().getName() + "()"); }
@Test public void testNotPublicConstructorClass() throws Exception { TestMonitor monitor = new TestMonitor(); ConfigurationMetadata<?> metadata = ConfigurationMetadata.getConfigurationMetadata(NotPublicConstructorClass.class, monitor); Map<String, Set<String>> expectedAttributes = new HashMap<>(); expectedAttributes.put("Value", ImmutableSet.of("value")); verifyMetaData(metadata, NotPublicConstructorClass.class, null, false, expectedAttributes); monitor.assertNumberOfErrors(1); monitor.assertNumberOfWarnings(0); monitor.assertMatchingErrorRecorded("not public", metadata.getConfigClass().getName() + "()"); }
@Test public void testNotPublicConstructorClass() throws Exception { TestMonitor monitor = new TestMonitor(); ConfigurationMetadata<?> metadata = ConfigurationMetadata.getConfigurationMetadata(NotPublicConstructorClass.class, monitor); Map<String, Set<String>> expectedAttributes = Maps.newHashMap(); expectedAttributes.put("Value", ImmutableSet.of("value")); verifyMetaData(metadata, NotPublicConstructorClass.class, null, expectedAttributes); monitor.assertNumberOfErrors(1); monitor.assertNumberOfWarnings(0); monitor.assertMatchingErrorRecorded("not public", metadata.getConfigClass().getName() + "()"); }
private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, Map<String, Set<String>> attributeProperties) throws Exception { Assert.assertEquals(metadata.getConfigClass(), configClass); if (metadata.getConstructor() != null) { Assert.assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor()); } else { try { configClass.getDeclaredConstructor(); Assert.fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName())); } catch (NoSuchMethodException expected) { } } Assert.assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size()); for (String name : attributeProperties.keySet()) { AttributeMetadata attribute = metadata.getAttributes().get(name); Assert.assertEquals(attribute.getConfigClass(), configClass); Set<String> namesToTest = Sets.newHashSet(); namesToTest.add(attribute.getInjectionPoint().getProperty()); for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) { namesToTest.add(legacyInjectionPoint.getProperty()); } Assert.assertEquals(namesToTest, attributeProperties.get(name)); Assert.assertEquals(attribute.getDescription(), description); } }
private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, boolean securitySensitive, Map<String, Set<String>> attributeProperties) throws Exception { assertEquals(metadata.getConfigClass(), configClass); if (metadata.getConstructor() != null) { assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor()); } else { try { configClass.getDeclaredConstructor(); fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName())); } catch (NoSuchMethodException expected) { } } assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size()); for (String name : attributeProperties.keySet()) { AttributeMetadata attribute = metadata.getAttributes().get(name); assertEquals(attribute.getConfigClass(), configClass); Set<String> namesToTest = new HashSet<>(); namesToTest.add(attribute.getInjectionPoint().getProperty()); for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) { namesToTest.add(legacyInjectionPoint.getProperty()); } assertEquals(namesToTest, attributeProperties.get(name)); assertEquals(attribute.getDescription(), description); assertEquals(attribute.isSecuritySensitive(), securitySensitive); } }
private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, boolean securitySensitive, Map<String, Set<String>> attributeProperties) throws Exception { assertEquals(metadata.getConfigClass(), configClass); if (metadata.getConstructor() != null) { assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor()); } else { try { configClass.getDeclaredConstructor(); fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName())); } catch (NoSuchMethodException expected) { } } assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size()); for (String name : attributeProperties.keySet()) { AttributeMetadata attribute = metadata.getAttributes().get(name); assertEquals(attribute.getConfigClass(), configClass); Set<String> namesToTest = new HashSet<>(); namesToTest.add(attribute.getInjectionPoint().getProperty()); for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) { namesToTest.add(legacyInjectionPoint.getProperty()); } assertEquals(namesToTest, attributeProperties.get(name)); assertEquals(attribute.getDescription(), description); assertEquals(attribute.isSecuritySensitive(), securitySensitive); } }