xref: /aosp_15_r20/external/snakeyaml/src/test/java/org/yaml/snakeyaml/issues/issue348/MultiLevelImmutableTest.java (revision ac2a7c1bf4e14d82f3bd566dcc2d76d5b42faf34)
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 org.yaml.snakeyaml.issues.issue348;
15 
16 import static org.hamcrest.CoreMatchers.instanceOf;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertThat;
19 
20 import java.util.List;
21 import java.util.Map;
22 import org.junit.Test;
23 import org.yaml.snakeyaml.Util;
24 import org.yaml.snakeyaml.Yaml;
25 import org.yaml.snakeyaml.issues.issue348.model.Bar;
26 import org.yaml.snakeyaml.issues.issue348.model.Baz;
27 import org.yaml.snakeyaml.issues.issue348.model.Foo;
28 
29 public class MultiLevelImmutableTest {
30 
31   @Test
testUnexpectedRecursive()32   public void testUnexpectedRecursive() {
33     Yaml yaml = new Yaml();
34     String data = Util.getLocalResource("issues/issue348.yaml");
35     Map<?, ?> loadedMap = yaml.loadAs(data, Map.class);
36 
37     for (Map.Entry<?, ?> entry : loadedMap.entrySet()) {
38       assertThat(entry.getValue(), instanceOf(List.class));
39     }
40 
41     Object foo = ((List) loadedMap.get("foo")).get(0);
42     Object bar = ((List) loadedMap.get("bar")).get(0);
43     Object baz = ((List) loadedMap.get("baz")).get(0);
44     assertThat(foo, instanceOf(Foo.class));
45     assertThat(bar, instanceOf(Bar.class));
46     assertThat(baz, instanceOf(Baz.class));
47 
48     assertEquals(foo, ((Bar) bar).getFoo());
49     assertEquals(bar, ((Baz) baz).getBar());
50     assertEquals("foo", ((Foo) foo).getFoo());
51   }
52 }
53