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.issue82;
    18
    19  import java.util.ArrayList;
    20  import java.util.Collection;
    21  import java.util.HashMap;
    22  import java.util.HashSet;
    23  import java.util.Map;
    24  import java.util.Set;
    25
    26  import junit.framework.TestCase;
    27
    28  import org.yaml.snakeyaml.TypeDescription;
    29  import org.yaml.snakeyaml.Yaml;
    30  import org.yaml.snakeyaml.constructor.Constructor;
    31  import org.yaml.snakeyaml.nodes.Tag;
    32  import org.yaml.snakeyaml.representer.Representer;
    33
    34  /**
    35   * @see issue 82: property order influence when aliased in generic collection
    36   */
    37  public class PropOrderInfluenceWhenAliasedInGenericCollectionTest extends TestCase {
    38
    39      public static interface Account {
    40      }
    41
    42      public static class GeneralAccount implements Account {
    43          public String name = "General";
    44      }
    45
    46      public static class SuperSaverAccount extends GeneralAccount {
    47
    48          public SuperSaverAccount() {
    49              name = "SuperSaver";
    50          }
    51      }
    52
    53      public static class CustomerAB {
    54          public Collection<Account> aAll;
    55          public Collection<GeneralAccount> bGeneral;
    56
    57          @Override
    58          public String toString() {
    59              return "CustomerAB";
    60          }
    61      }
    62
    63      public static class CustomerBA {
    64          public Collection<GeneralAccount> aGeneral;
    65          public Collection<Account> bAll;
    66      }
    67
    68      public static class CustomerAB_MapValue {
    69          public Collection<Account> aAll;
    70          public Map<String, GeneralAccount> bGeneralMap;
    71
    72          @Override
    73          public String toString() {
    74              return "CustomerAB_MapValue";
    75          }
    76      }
    77
    78      public static class CustomerAB_MapKey {
    79          public Collection<Account> aAll;
    80          public Map<GeneralAccount, String> bGeneralMap;
    81
    82          @Override
    83          public String toString() {
    84              return "CustomerAB_MapKey";
    85          }
    86      }
    87
    88      public static class CustomerAB_Property {
    89          public Account acc;
    90          public Collection<GeneralAccount> bGeneral;
    91
    92          @Override
    93          public String toString() {
    94              return "CustomerAB_Property";
    95          }
    96      }
    97
    98      public void testAB() {
    99          SuperSaverAccount supersaver = new SuperSaverAccount();
    100         GeneralAccount generalAccount = new GeneralAccount();
    101
    102         CustomerAB customerAB = new CustomerAB();
    103         ArrayList<Account> all = new ArrayList<Account>();
    104         all.add(supersaver);
    105         all.add(generalAccount);
    106         ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    107         general.add(generalAccount);
    108         general.add(supersaver);
    109
    110         customerAB.aAll = all;
    111         customerAB.bGeneral = general;
    112
    113         Yaml yaml = new Yaml();
    114         String dump = yaml.dump(customerAB);
    115         // System.out.println(dump);
    116         CustomerAB parsed = (CustomerAB) yaml.load(dump);
    117         assertNotNull(parsed);
    118     }
    119
    120     public void testAB_Set() {
    121         SuperSaverAccount supersaver = new SuperSaverAccount();
    122         GeneralAccount generalAccount = new GeneralAccount();
    123
    124         CustomerAB customerAB = new CustomerAB();
    125         ArrayList<Account> all = new ArrayList<Account>();
    126         all.add(supersaver);
    127         all.add(generalAccount);
    128         Set<GeneralAccount> general = new HashSet<GeneralAccount>();
    129         general.add(generalAccount);
    130         general.add(supersaver);
    131
    132         customerAB.aAll = all;
    133         customerAB.bGeneral = general;
    134
    135         Yaml yaml = new Yaml();
    136         String dump = yaml.dump(customerAB);
    137         // System.out.println(dump);
    138         CustomerAB parsed = (CustomerAB) yaml.load(dump);
    139         assertNotNull(parsed);
    140     }
    141
    142     public void testABWithCustomTag() {
    143         SuperSaverAccount supersaver = new SuperSaverAccount();
    144         GeneralAccount generalAccount = new GeneralAccount();
    145
    146         CustomerAB customerAB = new CustomerAB();
    147         ArrayList<Account> all = new ArrayList<Account>();
    148         all.add(supersaver);
    149         all.add(generalAccount);
    150         ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    151         general.add(generalAccount);
    152         general.add(supersaver);
    153
    154         customerAB.aAll = all;
    155         customerAB.bGeneral = general;
    156
    157         Constructor constructor = new Constructor();
    158         Representer representer = new Representer();
    159         Tag generalAccountTag = new Tag("!GA");
    160         constructor
    161                 .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
    162         representer.addClassTag(GeneralAccount.class, generalAccountTag);
    163
    164         Yaml yaml = new Yaml(constructor, representer);
    165         String dump = yaml.dump(customerAB);
    166         // System.out.println(dump);
    167         CustomerAB parsed = (CustomerAB) yaml.load(dump);
    168         assertNotNull(parsed);
    169     }
    170
    171     public void testABProperty() {
    172         SuperSaverAccount supersaver = new SuperSaverAccount();
    173         GeneralAccount generalAccount = new GeneralAccount();
    174
    175         CustomerAB_Property customerAB_property = new CustomerAB_Property();
    176         ArrayList<Account> all = new ArrayList<Account>();
    177         all.add(supersaver);
    178         all.add(generalAccount);
    179         ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    180         general.add(generalAccount);
    181         general.add(supersaver);
    182
    183         customerAB_property.acc = generalAccount;
    184         customerAB_property.bGeneral = general;
    185
    186         Constructor constructor = new Constructor();
    187         Representer representer = new Representer();
    188
    189         Yaml yaml = new Yaml(constructor, representer);
    190         String dump = yaml.dump(customerAB_property);
    191         // System.out.println(dump);
    192         CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
    193         assertNotNull(parsed);
    194     }
    195
    196     public void testABPropertyWithCustomTag() {
    197         SuperSaverAccount supersaver = new SuperSaverAccount();
    198         GeneralAccount generalAccount = new GeneralAccount();
    199
    200         CustomerAB_Property customerAB_property = new CustomerAB_Property();
    201         ArrayList<Account> all = new ArrayList<Account>();
    202         all.add(supersaver);
    203         all.add(generalAccount);
    204         ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    205         general.add(generalAccount);
    206         general.add(supersaver);
    207
    208         customerAB_property.acc = generalAccount;
    209         customerAB_property.bGeneral = general;
    210
    211         Constructor constructor = new Constructor();
    212         Representer representer = new Representer();
    213
    214         Tag generalAccountTag = new Tag("!GA");
    215         constructor
    216                 .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
    217         representer.addClassTag(GeneralAccount.class, generalAccountTag);
    218
    219         Yaml yaml = new Yaml(constructor, representer);
    220         String dump = yaml.dump(customerAB_property);
    221         // System.out.println(dump);
    222         CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
    223         assertNotNull(parsed);
    224     }
    225
    226     public void testABwithJavaBeanHelpers() {
    227         SuperSaverAccount supersaver = new SuperSaverAccount();
    228         GeneralAccount generalAccount = new GeneralAccount();
    229
    230         CustomerAB customerAB = new CustomerAB();
    231         ArrayList<Account> all = new ArrayList<Account>();
    232         all.add(supersaver);
    233         all.add(generalAccount);
    234         ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    235         general.add(generalAccount);
    236         general.add(supersaver);
    237
    238         customerAB.aAll = all;
    239         customerAB.bGeneral = general;
    240
    241         Yaml yaml = new Yaml();
    242         String dump2 = yaml.dumpAsMap(customerAB);
    243         // System.out.println(dump2);
    244         Yaml loader = new Yaml();
    245         CustomerAB parsed = loader.loadAs(dump2, CustomerAB.class);
    246         assertNotNull(parsed);
    247     }
    248
    249     public void testAB_asMapValue() {
    250         SuperSaverAccount supersaver = new SuperSaverAccount();
    251         GeneralAccount generalAccount = new GeneralAccount();
    252
    253         CustomerAB_MapValue customerAB_mapValue = new CustomerAB_MapValue();
    254         ArrayList<Account> all = new ArrayList<Account>();
    255         all.add(supersaver);
    256         all.add(generalAccount);
    257         Map<String, GeneralAccount> generalMap = new HashMap<String, GeneralAccount>();
    258         generalMap.put(generalAccount.name, generalAccount);
    259         generalMap.put(supersaver.name, supersaver);
    260
    261         customerAB_mapValue.aAll = all;
    262         customerAB_mapValue.bGeneralMap = generalMap;
    263
    264         Yaml yaml = new Yaml();
    265         String dump = yaml.dump(customerAB_mapValue);
    266         // System.out.println(dump);
    267         CustomerAB_MapValue parsed = (CustomerAB_MapValue) yaml.load(dump);
    268         assertNotNull(parsed);
    269     }
    270
    271     public void testAB_asMapKey() {
    272         SuperSaverAccount supersaver = new SuperSaverAccount();
    273         GeneralAccount generalAccount = new GeneralAccount();
    274
    275         CustomerAB_MapKey customerAB_mapKey = new CustomerAB_MapKey();
    276         ArrayList<Account> all = new ArrayList<Account>();
    277         all.add(supersaver);
    278         all.add(generalAccount);
    279         Map<GeneralAccount, String> generalMap = new HashMap<GeneralAccount, String>();
    280         generalMap.put(generalAccount, generalAccount.name);
    281         generalMap.put(supersaver, supersaver.name);
    282
    283         customerAB_mapKey.aAll = all;
    284         customerAB_mapKey.bGeneralMap = generalMap;
    285
    286         Yaml yaml = new Yaml();
    287         String dump = yaml.dump(customerAB_mapKey);
    288         // System.out.println(dump);
    289         CustomerAB_MapKey parsed = (CustomerAB_MapKey) yaml.load(dump);
    290         assertNotNull(parsed);
    291     }
    292
    293     public void testBA() {
    294         SuperSaverAccount supersaver = new SuperSaverAccount();
    295         GeneralAccount generalAccount = new GeneralAccount();
    296
    297         CustomerBA customerBA = new CustomerBA();
    298         ArrayList<Account> all = new ArrayList<Account>();
    299         all.add(supersaver);
    300         all.add(generalAccount);
    301         ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    302         general.add(generalAccount);
    303         general.add(supersaver);
    304
    305         customerBA.aGeneral = general;
    306         customerBA.bAll = all;
    307
    308         Yaml yaml = new Yaml();
    309         String dump = yaml.dump(customerBA);
    310         // System.out.println(dump);
    311         //
    312         CustomerBA parsed = (CustomerBA) yaml.load(dump);
    313         assertEquals(2, parsed.bAll.size());
    314         assertEquals(2, parsed.aGeneral.size());
    315         assertFalse(parsed.bAll.equals(parsed.aGeneral));
    316         GeneralAccount[] array = parsed.aGeneral.toArray(new GeneralAccount[2]);
    317         assertEquals(GeneralAccount.class, array[0].getClass());
    318         assertEquals(SuperSaverAccount.class, array[1].getClass());
    319         assertEquals("SuperSaver", array[1].name);
    320     }
    321 }