1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.issues.issue60;
18
19 import java.beans.IntrospectionException;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.LinkedHashSet;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import junit.framework.TestCase;
27
28 import org.yaml.snakeyaml.Util;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.introspector.BeanAccess;
31 import org.yaml.snakeyaml.introspector.Property;
32 import org.yaml.snakeyaml.introspector.PropertyUtils;
33 import org.yaml.snakeyaml.representer.Representer;
34
35
36 public class CustomOrderTest extends TestCase {
37
38 public void testReversedOrder() {
39 Representer repr = new Representer();
40 repr.setPropertyUtils(new ReversedPropertyUtils());
41 Yaml yaml = new Yaml(repr);
42 String output = yaml.dump(getBean());
43
44 assertEquals(Util.getLocalResource("issues/issue59-1.yaml"), output);
45 }
46
47 private class ReversedPropertyUtils extends PropertyUtils {
48 @Override
49 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess)
50 throws IntrospectionException {
51 Set<Property> result = new TreeSet<Property>(Collections.reverseOrder());
52 result.addAll(super.createPropertySet(type, bAccess));
53 return result;
54 }
55 }
56
57 public void testUnsorted() {
58 Representer repr = new Representer();
59 repr.setPropertyUtils(new UnsortedPropertyUtils());
60 Yaml yaml = new Yaml(repr);
61 String output = yaml.dump(getBean());
62
63 assertEquals(Util.getLocalResource("issues/issue59-2.yaml"), output);
64 }
65
66 private class UnsortedPropertyUtils extends PropertyUtils {
67 @Override
68 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess)
69 throws IntrospectionException {
70 Set<Property> result = new LinkedHashSet<Property>(getPropertiesMap(type,
71 BeanAccess.FIELD).values());
72 result.remove(result.iterator().next());
73 return result;
74 }
75 }
76
77 private SkipBean getBean() {
78 SkipBean bean = new SkipBean();
79 bean.setText("foo");
80 bean.setListDate(null);
81 bean.setListInt(Arrays.asList(new Integer[] { null, 1, 2, 3 }));
82 bean.setListStr(Arrays.asList(new String[] { "bar", null, "foo", null }));
83 return bean;
84 }
85 }