aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/examples/IgnoreTagsExampleTest.java
blob: 36e308d0a1581180eb865ec1a8a1e684d01eb689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * 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 examples;

import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import org.yaml.snakeyaml.Util;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.AbstractConstruct;
import org.yaml.snakeyaml.constructor.Construct;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;

public class IgnoreTagsExampleTest extends TestCase {

  @SuppressWarnings("unchecked")
  public void testLoad() {
    String input = Util.getLocalResource("examples/unknown-tags-example.yaml");
    // System.out.println(input);
    Yaml yaml = new Yaml(new MyConstructor());
    Map<String, Object> result = yaml.load(input);
    // Check the result
    assertNotNull(result);
    assertEquals(3, result.size());
    assertEquals("123", result.get("aaa"));
    //
    List<Object> bbb = (List<Object>) result.get("bbb");
    assertEquals(2, bbb.size());
    assertEquals(Integer.valueOf(111), bbb.get(0));
    assertEquals("ddd", bbb.get(1));
    //
    Map<String, Object> ccc = (Map<String, Object>) result.get("ccc");
    assertEquals(2, ccc.size());
    assertEquals(1.0, ccc.get("x"));
    assertEquals(3.1416, ccc.get("y"));
  }

  private class MyConstructor extends Constructor {

    private final Construct original;

    public MyConstructor() {
      original = this.yamlConstructors.get(null);
      this.yamlConstructors.put(null, new IgnoringConstruct());
    }

    private class IgnoringConstruct extends AbstractConstruct {

      public Object construct(Node node) {
        if (node.getTag().startsWith("!KnownTag")) {
          return original.construct(node);
        } else {
          switch (node.getNodeId()) {
            case scalar:
              return yamlConstructors.get(Tag.STR).construct(node);
            case sequence:
              return yamlConstructors.get(Tag.SEQ).construct(node);
            case mapping:
              return yamlConstructors.get(Tag.MAP).construct(node);
            default:
              throw new YAMLException("Unexpected node");
          }
        }
      }
    }
  }
}