• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.issue60;
15 
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.LinkedHashSet;
19 import java.util.Set;
20 import java.util.TreeSet;
21 import junit.framework.TestCase;
22 import org.yaml.snakeyaml.Util;
23 import org.yaml.snakeyaml.Yaml;
24 import org.yaml.snakeyaml.introspector.BeanAccess;
25 import org.yaml.snakeyaml.introspector.Property;
26 import org.yaml.snakeyaml.introspector.PropertyUtils;
27 import org.yaml.snakeyaml.representer.Representer;
28 
29 // issue 59
30 public class CustomOrderTest extends TestCase {
31 
testReversedOrder()32   public void testReversedOrder() {
33     Representer repr = new Representer();
34     repr.setPropertyUtils(new ReversedPropertyUtils());
35     Yaml yaml = new Yaml(repr);
36     String output = yaml.dump(getBean());
37     // System.out.println(output);
38     assertEquals(Util.getLocalResource("issues/issue59-1.yaml"), output);
39   }
40 
41   private class ReversedPropertyUtils extends PropertyUtils {
42 
43     @Override
createPropertySet(Class<? extends Object> type, BeanAccess bAccess)44     protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) {
45       Set<Property> result = new TreeSet<Property>(Collections.reverseOrder());
46       result.addAll(super.createPropertySet(type, bAccess));
47       return result;
48     }
49   }
50 
testUnsorted()51   public void testUnsorted() {
52     Representer repr = new Representer();
53     repr.setPropertyUtils(new UnsortedPropertyUtils());
54     Yaml yaml = new Yaml(repr);
55     String output = yaml.dump(getBean());
56     // System.out.println(output);
57     assertEquals(Util.getLocalResource("issues/issue59-2.yaml"), output);
58   }
59 
60   private class UnsortedPropertyUtils extends PropertyUtils {
61 
62     @Override
createPropertySet(Class<? extends Object> type, BeanAccess bAccess)63     protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) {
64       Set<Property> result =
65           new LinkedHashSet<Property>(getPropertiesMap(type, BeanAccess.FIELD).values());
66       result.remove(result.iterator().next());// drop 'listInt' property
67       return result;
68     }
69   }
70 
getBean()71   private SkipBean getBean() {
72     SkipBean bean = new SkipBean();
73     bean.setText("foo");
74     bean.setListDate(null);
75     bean.setListInt(Arrays.asList(null, 1, 2, 3));
76     bean.setListStr(Arrays.asList("bar", null, "foo", null));
77     return bean;
78   }
79 }
80