ConfigMetadataCompiler { public ConfigMetadataNode compile(String name, Type type) { return compile(new Descriptor(name, type)); } @Deprecated  ConfigMetadataCompiler(Function<Class<?>, Stream<Class<?>>> subclassProvider);  ConfigMetadataCompiler(Function<Class<?>, Stream<Class<?>>> subclassProvider, Map<Class<?>, ValueObjectDescriptor> descriptorMap); ConfigMetadataNode compile(String name, Type type);  }
@Test public void testCompile_InheritanceFromInterface() { ConfigObjectMetadata c9 = (ConfigObjectMetadata) createCompiler(t -> { if (Config9.class.equals(t)) { return Stream.of(Config10.class, Config11.class); } return Stream.empty(); }).compile("prefix", Config9.class); assertNotNull(c9); assertEquals("prefix", c9.getName()); assertEquals(Config9.class, c9.getType()); assertTrue(c9.isAbstractType()); Map<Type, ConfigMetadataNode> sc9 = c9.getSubConfigs().stream() .collect(Collectors.toMap(ConfigMetadataNode::getType, Function.identity())); assertEquals(2, sc9.size()); ConfigObjectMetadata c10 = (ConfigObjectMetadata) sc9.get(Config10.class); assertNotNull(c10); assertEquals("c10", c10.getTypeLabel()); assertFalse(c10.isAbstractType()); assertEquals(0, c10.getSubConfigs().size()); ConfigObjectMetadata c11 = (ConfigObjectMetadata) sc9.get(Config11.class); assertNotNull(c11); assertEquals("c11", c11.getTypeLabel()); assertFalse(c11.isAbstractType()); assertEquals(0, c11.getSubConfigs().size()); }
@Test public void testCompile() { ConfigObjectMetadata md = (ConfigObjectMetadata) createCompiler().compile("prefix", Config1.class); assertNotNull(md); assertEquals("prefix", md.getName()); assertEquals("Describes Config1", md.getDescription()); assertEquals(Config1.class, md.getType()); assertEquals(10, md.getProperties().size()); Map<String, ConfigMetadataNode> propMap = md .getProperties() .stream() .collect(Collectors.toMap(MetadataNode::getName, Function.identity())); ConfigValueMetadata config1 = (ConfigValueMetadata) propMap.get("config1"); assertEquals(String.class, config1.getType()); assertEquals("constructor with params", config1.getDescription()); ConfigValueMetadata p1 = (ConfigValueMetadata) propMap.get("p1"); assertEquals(Integer.TYPE, p1.getType()); assertNull(p1.getDescription()); ConfigValueMetadata p2 = (ConfigValueMetadata) propMap.get("p2"); assertEquals(BigDecimal.class, p2.getType()); assertEquals("description of p2", p2.getDescription()); ConfigMetadataNode p3 = propMap.get("p3"); assertEquals(Config2.class, p3.getType()); ConfigListMetadata p4 = (ConfigListMetadata) propMap.get("p4"); assertEquals("java.util.List<java.lang.String>", p4.getType().getTypeName()); assertEquals(String.class, p4.getElementType().getType()); assertEquals(ConfigValueMetadata.class, p4.getElementType().getClass()); ConfigListMetadata p5 = (ConfigListMetadata) propMap.get("p5"); assertEquals("java.util.List<io.bootique.meta.config.ConfigMetadataCompilerTest$Config2>", p5.getType().getTypeName()); assertEquals(Config2.class, p5.getElementType().getType()); ConfigMapMetadata p6 = (ConfigMapMetadata) propMap.get("p6"); assertEquals("java.util.Map<java.lang.String, java.math.BigDecimal>", p6.getType().getTypeName()); assertEquals(String.class, p6.getKeysType()); assertEquals(BigDecimal.class, p6.getValuesType().getType()); assertEquals(ConfigValueMetadata.class, p6.getValuesType().getClass()); ConfigMapMetadata p7 = (ConfigMapMetadata) propMap.get("p7"); assertEquals("java.util.Map<java.lang.Integer, io.bootique.meta.config.ConfigMetadataCompilerTest$Config2>", p7.getType().getTypeName()); assertEquals(Integer.class, p7.getKeysType()); assertEquals(Config2.class, p7.getValuesType().getType()); }
@Test public void testCompile_Cycle() { ConfigObjectMetadata md = (ConfigObjectMetadata) createCompiler().compile("prefix", Config3.class); assertNotNull(md); assertEquals("prefix", md.getName()); assertEquals(Config3.class, md.getType()); assertEquals(1, md.getProperties().size()); ConfigListMetadata p1 = (ConfigListMetadata) md.getProperties().iterator().next(); assertEquals("java.util.Collection<io.bootique.meta.config.ConfigMetadataCompilerTest$Config4>", p1.getType().toString()); assertEquals(Config4.class, p1.getElementType().getType()); }
@Test public void testCompile_Inheritance() { ConfigObjectMetadata c5 = (ConfigObjectMetadata) createCompiler(t -> { if (Config5.class.equals(t)) { return Stream.of(Config6.class, Config7.class); } if (Config6.class.equals(t)) { return Stream.of(Config8.class); } return Stream.empty(); }).compile("prefix", Config5.class); assertNotNull(c5); assertEquals("prefix", c5.getName()); assertEquals(Config5.class, c5.getType()); assertTrue(c5.isAbstractType()); Map<Type, ConfigMetadataNode> sc5 = c5.getSubConfigs().stream() .collect(Collectors.toMap(ConfigMetadataNode::getType, Function.identity())); assertEquals(2, sc5.size()); ConfigObjectMetadata c6 = (ConfigObjectMetadata) sc5.get(Config6.class); assertNotNull(c6); assertEquals("c6", c6.getTypeLabel()); assertFalse(c6.isAbstractType()); Map<Type, ConfigMetadataNode> sc6 = c6.getSubConfigs().stream() .collect(Collectors.toMap(ConfigMetadataNode::getType, Function.identity())); assertEquals(1, sc6.size()); ConfigObjectMetadata c8 = (ConfigObjectMetadata) sc6.get(Config8.class); assertNotNull(c8); assertEquals("c8", c8.getTypeLabel()); assertFalse(c8.isAbstractType()); assertEquals(0, c8.getSubConfigs().size()); ConfigObjectMetadata c7 = (ConfigObjectMetadata) sc5.get(Config7.class); assertNotNull(c7); assertEquals("c7", c7.getTypeLabel()); assertFalse(c7.isAbstractType()); assertEquals(0, c7.getSubConfigs().size()); }
@Test public void testCompile_Map() { TypeRef<Map<String, BigDecimal>> tr1 = new TypeRef<Map<String, BigDecimal>>() { }; ConfigMapMetadata md = (ConfigMapMetadata) createCompiler(t -> Stream.empty()) .compile("prefix", tr1.getType()); assertNotNull(md); assertEquals("prefix", md.getName()); assertEquals(tr1.getType(), md.getType()); assertEquals(String.class, md.getKeysType()); assertEquals(BigDecimal.class, md.getValuesType().getType()); assertEquals(ConfigValueMetadata.class, md.getValuesType().getClass()); }
@Test public void testCompile_Map_Inheritance() { TypeRef<Map<String, Config5>> tr1 = new TypeRef<Map<String, Config5>>() { }; ConfigMapMetadata md = (ConfigMapMetadata) createCompiler(t -> { if (Config5.class.equals(t)) { return Stream.of(Config6.class, Config7.class); } if (Config6.class.equals(t)) { return Stream.of(Config8.class); } return Stream.empty(); }).compile("prefix", tr1.getType()); assertNotNull(md); assertEquals("prefix", md.getName()); assertEquals(tr1.getType(), md.getType()); assertEquals(String.class, md.getKeysType()); assertEquals(Config5.class, md.getValuesType().getType()); assertEquals(ConfigObjectMetadata.class, md.getValuesType().getClass()); ConfigObjectMetadata c5 = (ConfigObjectMetadata) md.getValuesType(); assertNotNull(c5); assertEquals(Config5.class, c5.getType()); assertTrue(c5.isAbstractType()); Map<Type, ConfigMetadataNode> sc5 = c5.getSubConfigs().stream() .collect(Collectors.toMap(ConfigMetadataNode::getType, Function.identity())); assertEquals(2, sc5.size()); ConfigObjectMetadata c6 = (ConfigObjectMetadata) sc5.get(Config6.class); assertNotNull(c6); assertEquals("c6", c6.getTypeLabel()); assertFalse(c6.isAbstractType()); Map<Type, ConfigMetadataNode> sc6 = c6.getSubConfigs().stream() .collect(Collectors.toMap(ConfigMetadataNode::getType, Function.identity())); assertEquals(1, sc6.size()); ConfigObjectMetadata c8 = (ConfigObjectMetadata) sc6.get(Config8.class); assertNotNull(c8); assertEquals("c8", c8.getTypeLabel()); assertFalse(c8.isAbstractType()); assertEquals(0, c8.getSubConfigs().size()); ConfigObjectMetadata c7 = (ConfigObjectMetadata) sc5.get(Config7.class); assertNotNull(c7); assertEquals("c7", c7.getTypeLabel()); assertFalse(c7.isAbstractType()); assertEquals(0, c7.getSubConfigs().size()); }