1 /** 2 * Copyright (c) 2008, http://www.snakeyaml.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.yaml.snakeyaml.issues.issue73; 17 18 import java.util.Set; 19 import java.util.SortedSet; 20 import java.util.TreeSet; 21 22 import junit.framework.TestCase; 23 24 import org.yaml.snakeyaml.Util; 25 import org.yaml.snakeyaml.Yaml; 26 27 public class RecursiveSortedSetTest extends TestCase { testDumpException()28 public void testDumpException() { 29 SortedSet<Object> set = new TreeSet<Object>(); 30 Bean11 bean = new Bean11(); 31 bean.setId("ID555"); 32 bean.setSet(set); 33 set.add("ggg"); 34 set.add("hhh"); 35 set.add(bean); 36 Yaml yaml = new Yaml(); 37 String doc = yaml.dump(bean); 38 // System.out.println(doc); 39 assertEquals(Util.getLocalResource("issues/issue73-recursive9.txt"), doc); 40 } 41 testLoadException()42 public void testLoadException() { 43 String doc = Util.getLocalResource("issues/issue73-recursive10.txt"); 44 // System.out.println(doc); 45 Yaml yaml = new Yaml(); 46 try { 47 yaml.load(doc); 48 fail("Recursive sets are not supported."); 49 } catch (Exception e) { 50 assertTrue(e.getMessage(), e.getMessage().contains("Set cannot be recursive.")); 51 } 52 } 53 54 /** 55 * set and JavaBean refer to each other 56 */ testLoadRecursiveTest()57 public void testLoadRecursiveTest() { 58 String doc = Util.getLocalResource("issues/issue73-recursive9.txt"); 59 // System.out.println(doc); 60 Yaml yaml = new Yaml(); 61 Bean11 beanWithSet = (Bean11) yaml.load(doc); 62 Set<Object> set = beanWithSet.getSet(); 63 assertEquals(TreeSet.class, set.getClass()); 64 assertEquals("ID555", beanWithSet.getId()); 65 assertEquals(3, set.size()); 66 assertTrue(set.remove("ggg")); 67 // assertFalse(set.remove("ggg"));??? 68 assertTrue(set.remove("hhh")); 69 assertEquals(1, set.size()); 70 // 71 Bean11 beanRef = (Bean11) set.iterator().next(); 72 assertEquals(beanWithSet, beanRef); 73 assertSame(beanWithSet, beanRef); 74 // 75 assertFalse(set.isEmpty()); 76 assertTrue(set.contains(beanWithSet)); 77 assertFalse(set.add(beanWithSet)); 78 assertTrue(set.remove(beanWithSet)); 79 assertFalse(set.remove(beanWithSet)); 80 assertTrue(set.isEmpty()); 81 } 82 83 public static class Bean11 implements Comparable<Object> { 84 private SortedSet<Object> set; 85 private String id; 86 getSet()87 public SortedSet<Object> getSet() { 88 return set; 89 } 90 setSet(SortedSet<Object> set)91 public void setSet(SortedSet<Object> set) { 92 this.set = set; 93 } 94 getId()95 public String getId() { 96 return id; 97 } 98 setId(String id)99 public void setId(String id) { 100 this.id = id; 101 } 102 compareTo(Object o)103 public int compareTo(Object o) { 104 return toString().compareTo(o.toString()); 105 } 106 107 @Override equals(Object obj)108 public boolean equals(Object obj) { 109 if (obj instanceof Bean11) { 110 Bean11 b = (Bean11) obj; 111 return id.equals(b.id); 112 } else { 113 return false; 114 } 115 } 116 117 @Override hashCode()118 public int hashCode() { 119 return toString().hashCode(); 120 } 121 122 @Override toString()123 public String toString() { 124 return "Bean id=" + id + "set=" + System.identityHashCode(set); 125 } 126 } 127 } 128