@NonNull @Override public ConfigurationNode createEmptyNode(@NonNull ConfigurationOptions options) { return SimpleConfigurationNode.root(options); } }
@NonNull @Override public ConfigurationNode createEmptyNode(@NonNull ConfigurationOptions options) { options = options.setAcceptedTypes(ImmutableSet.of(Map.class, List.class, Double.class, Float.class, Long.class, Integer.class, Boolean.class, String.class)); return SimpleConfigurationNode.root(options); }
/** * Return an empty node of the most appropriate type for this loader * * @param options The options to use with this node. Must not be null (take a look at {@link ConfigurationOptions#defaults()}) * @return The appropriate node type */ @NonNull @Override public ConfigurationNode createEmptyNode(@NonNull ConfigurationOptions options) { return SimpleConfigurationNode.root(options); } }
@Test public void testGetSetValueSerialized() throws ObjectMappingException { SimpleConfigurationNode subject = SimpleConfigurationNode.root(); subject.setValue("48"); assertEquals((Object) 48, subject.getValue(TypeToken.of(Integer.class))); UUID testId = UUID.randomUUID(); subject.setValue(TypeToken.of(UUID.class), testId); assertEquals(testId.toString(), subject.getValue()); }
@Test public void testUUIDSerializer() throws ObjectMappingException { final TypeToken<UUID> uuidType = TypeToken.of(UUID.class); final TypeSerializer<UUID> uuidSerializer = SERIALIZERS.get(uuidType); final UUID testUuid = UUID.randomUUID(); SimpleConfigurationNode serializeTo = SimpleConfigurationNode.root(); uuidSerializer.serialize(uuidType, testUuid, serializeTo); assertEquals(testUuid.toString(), serializeTo.getValue()); assertEquals(testUuid, uuidSerializer.deserialize(uuidType, serializeTo)); }
@Test public void testMapSerializerPreservesEmptyMap() throws ObjectMappingException { final TypeToken<Map<String, Integer>> mapStringIntType = new TypeToken<Map<String, Integer>>() {}; final TypeSerializer<Map<String, Integer>> mapStringIntSerializer = SERIALIZERS.get(mapStringIntType); final ConfigurationNode value = SimpleConfigurationNode.root(); mapStringIntSerializer.serialize(mapStringIntType, ImmutableMap.of(), value); assertTrue(value.hasMapChildren()); }
@Test public void testGetPath() { ConfigurationNode root = SimpleConfigurationNode.root(); assertArrayEquals(new Object[]{"a", "b", "c"}, root.getNode("a", "b", "c").getPath()); }
@Test public void testURLSerializer() throws ObjectMappingException, MalformedURLException { final TypeToken<URL> urlType = TypeToken.of(URL.class); final TypeSerializer<URL> urlSerializer = SERIALIZERS.get(urlType); final String urlString = "http://google.com"; final URL testUrl = new URL(urlString); SimpleConfigurationNode node = SimpleConfigurationNode.root().setValue(urlString); assertEquals(testUrl, urlSerializer.deserialize(urlType, node)); urlSerializer.serialize(urlType, testUrl, node); assertEquals(urlString, node.getValue()); }
@Test public void testURISerializer() throws ObjectMappingException { final TypeToken<URI> uriType = TypeToken.of(URI.class); final TypeSerializer<URI> uriSerializer = SERIALIZERS.get(uriType); final String uriString = "http://google.com"; final URI testUri = URI.create(uriString); SimpleConfigurationNode node = SimpleConfigurationNode.root().setValue(uriString); assertEquals(testUri, uriSerializer.deserialize(uriType, node)); uriSerializer.serialize(uriType, testUri, node); assertEquals(uriString, node.getValue()); }
@Test public void testGetChildrenMap() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode a = root.getNode("a").setValue("one"); ConfigurationNode b = root.getNode("b").setValue("two"); assertEquals(ImmutableMap.<Object, ConfigurationNode>of("a", a, "b", b), root.getChildrenMap()); }
@Test public void testListRawTypes() throws ObjectMappingException { final TypeToken<List> rawType = TypeToken.of(List.class); final TypeSerializer<List> serial = SERIALIZERS.get(rawType); final ConfigurationNode value = SimpleConfigurationNode.root(); value.getAppendedNode().setValue(1); value.getAppendedNode().setValue("dog"); value.getAppendedNode().setValue(2.4); Assertions.assertTrue(Assertions.assertThrows(Exception.class, () -> { serial.deserialize(rawType, value); }).getMessage().startsWith("Raw types")); }
@Test public void testGetChildrenList() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode a = root.getAppendedNode().setValue("one"); ConfigurationNode b = root.getAppendedNode().setValue("two"); assertEquals(ImmutableList.of(a, b), root.getChildrenList()); }
@Test public void testStringSerializer() throws ObjectMappingException { final TypeToken<String> stringType = TypeToken.of(String.class); final TypeSerializer<String> stringSerializer = SERIALIZERS.get(stringType); final ConfigurationNode node = SimpleConfigurationNode.root().setValue("foobar"); assertEquals("foobar", stringSerializer.deserialize(stringType, node)); stringSerializer.serialize(stringType, "foobarbaz", node); assertEquals("foobarbaz", node.getString()); }
@Test public void testCreateFromNode() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); source.getNode("test-key").setValue("some are born great, some achieve greatness, and some have greatness thrust upon them"); final TestObject obj = mapper.bindToNew().populate(source); assertEquals("some are born great, some achieve greatness, and some have greatness thrust upon them", obj.stringVal); }
@Test public void testListPacking() { ConfigurationNode root = SimpleConfigurationNode.root(); root.getAppendedNode().setValue("test1"); root.getAppendedNode().setValue("test2"); assertEquals(TEST_LIST, root.getValue()); }
@Test public void testLoadExistingObject() throws ObjectMappingException { final ObjectMapper<TestObject> mapper = ObjectMapper.forClass(TestObject.class); final ConfigurationNode source = SimpleConfigurationNode.root(); final TestObject instance = new TestObject(); source.getNode("test-key").setValue("boom"); mapper.bind(instance).populate(source); assertEquals("boom", instance.stringVal); }
@Test public void testListUnpacking() { ConfigurationNode root = SimpleConfigurationNode.root(); root.setValue(TEST_LIST); assertEquals("test1", root.getNode(0).getValue()); assertEquals("test2", root.getNode(1).getValue()); }
@Test public void testBooleanSerializer() throws ObjectMappingException { final TypeToken<Boolean> booleanType = TypeToken.of(Boolean.class); final TypeSerializer<Boolean> booleanSerializer = SERIALIZERS.get(booleanType); SimpleConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("direct").setValue(true); node.getNode("fromstring").setValue("true"); assertEquals(true, booleanSerializer.deserialize(booleanType, node.getNode("direct"))); assertEquals(true, booleanSerializer.deserialize(booleanType, node.getNode("fromstring"))); }
@Test public void testDefaultsCopied() { SimpleConfigurationNode subject = SimpleConfigurationNode.root(ConfigurationOptions.defaults().setShouldCopyDefaults(true)); assertNull(subject.getValue()); assertEquals("default value", subject.getValue("default value")); assertEquals("default value", subject.getValue()); }
@Test public void testSettingNullRemoves() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode child = root.getNode("child").setValue("a"); assertFalse(child.isVirtual()); assertSame(child, root.getNode("child")); child.setValue(null); assertTrue(child.isVirtual()); assertNotSame(child, root.getNode("child")); }