View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    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  //issue 59
    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          // System.out.println(output);
    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          // System.out.println(output);
    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());// drop 'listInt' property
    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  }