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.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 //issue 59 35 public class CustomOrderTest extends TestCase { 36 testReversedOrder()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 // System.out.println(output); 43 assertEquals(Util.getLocalResource("issues/issue59-1.yaml"), output); 44 } 45 46 private class ReversedPropertyUtils extends PropertyUtils { 47 @Override createPropertySet(Class<? extends Object> type, BeanAccess bAccess)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 testUnsorted()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 // System.out.println(output); 62 assertEquals(Util.getLocalResource("issues/issue59-2.yaml"), output); 63 } 64 65 private class UnsortedPropertyUtils extends PropertyUtils { 66 @Override createPropertySet(Class<? extends Object> type, BeanAccess bAccess)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());// drop 'listInt' property 72 return result; 73 } 74 } 75 getBean()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 } 85