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