1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package examples; 15 16 import junit.framework.TestCase; 17 import org.yaml.snakeyaml.Yaml; 18 import org.yaml.snakeyaml.constructor.AbstractConstruct; 19 import org.yaml.snakeyaml.constructor.SafeConstructor; 20 import org.yaml.snakeyaml.nodes.Node; 21 import org.yaml.snakeyaml.nodes.Tag; 22 23 /** 24 * Issue 1 for snakeyaml-engine 25 * https://bitbucket.org/snakeyaml/snakeyaml-engine/issues/1/null-tag-constructor-not-called-when 26 */ 27 public class CustomNullConstructorTest extends TestCase { 28 testEmpty()29 public void testEmpty() { 30 Yaml yaml = new Yaml(new NullConstructor()); 31 assertEquals(Integer.valueOf(1), yaml.load("")); 32 } 33 testNull()34 public void testNull() { 35 Yaml yaml = new Yaml(new NullConstructor()); 36 assertEquals(Integer.valueOf(1), yaml.load("null")); 37 } 38 testNullTag()39 public void testNullTag() { 40 Yaml yaml = new Yaml(new NullConstructor()); 41 assertEquals(Integer.valueOf(1), yaml.load("!!null null")); 42 } 43 44 class NullConstructor extends SafeConstructor { 45 NullConstructor()46 public NullConstructor() { 47 this.yamlConstructors.put(Tag.NULL, new ConstructNull()); 48 } 49 50 private class ConstructNull extends AbstractConstruct { 51 construct(Node node)52 public Object construct(Node node) { 53 return 1; 54 } 55 } 56 } 57 } 58