blob: 399de94cb78e60dbf9bb2347078729aad0b5f1b0 [file] [log] [blame]
/**
* Copyright (c) 2008-2014, http://www.snakeyaml.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.yaml.snakeyaml.issues.issue139;
import java.util.Map;
import junit.framework.TestCase;
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.ConstructorException;
public class MergeValueTest extends TestCase {
public void testNotUniqueSimple() {
String simple = "{k: 1, k: 2}";
Yaml yaml = new Yaml();
try {
yaml.load(simple);
fail("Duplicate key must not be accepted.");
} catch (ConstructorException e) {
System.out.print(e);
assertTrue(simple, e.getMessage().contains("duplicate key"));
}
}
public void testMerge() {
check("issues/issue139-1.yaml");// merge with unique keys
}
public void testMergeWithSameKey() {
try {
check("issues/issue139-2.yaml");// merge with same key
fail("Duplicate key must not be accepted.");
} catch (ConstructorException e) {
assertTrue(e.getMessage().contains("duplicate key"));
}
}
private void check(String name) {
String input = Util.getLocalResource(name);
// System.out.println(input);
Yaml yaml = new Yaml();
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) yaml.load(input);
assertEquals(2, map.size());
assertTrue(map.containsKey("common"));
assertTrue(map.containsKey("production"));
assertEquals(map.get("common"), map.get("production"));
}
/**
* http://yaml.org/type/merge.html: If the value associated with the key is
* a single mapping node, each of its key/value pairs is inserted into the
* current mapping, <b>unless the key already exists in it</b>.
*/
@SuppressWarnings("unchecked")
public void testMergeUnlessAlreadyExists() {
String input = Util.getLocalResource("issues/issue139-3.yaml");
// System.out.println(input);
Yaml yaml = new Yaml();
Map<String, Object> map = (Map<String, Object>) yaml.load(input);
assertEquals(2, map.size());
Map<String, Integer> common = (Map<String, Integer>) map.get("common");
Map<String, Integer> production = (Map<String, Integer>) map.get("production");
assertEquals(new Integer(1), common.get("key"));
assertEquals(new Integer(2), production.get("key"));
}
}