/**
 * Copyright (c) 2008-2010, http://code.google.com/p/snakeyaml/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yaml.snakeyaml.issues.issue82;

import java.util.ArrayList;
import java.util.Collection;

import junit.framework.TestCase;

import org.yaml.snakeyaml.JavaBeanDumper;
import org.yaml.snakeyaml.JavaBeanLoader;
import org.yaml.snakeyaml.Yaml;

public class PropOrderInfluenceWhenAliasedInGenericCollectionTest extends TestCase {

    public static interface Account {
    }

    public static class GeneralAccount implements Account {
        public String name = "General";
    }

    public static class SuperSaverAccount extends GeneralAccount {
        public String name = "SuperSaver";
    }

    public static class SecretAccount implements Account {
        public String name = "Secret";
    }

    public static class CustomerAB {
        public Collection<Account> aAll;
        public Collection<GeneralAccount> bGeneral;

        @Override
        public String toString() {
            return "CustomerAB";
        }
    }

    public static class CustomerBA {
        public Collection<GeneralAccount> aGeneral;
        public Collection<Account> bAll;
    }

    public void testAB() {
        SuperSaverAccount supersaver = new SuperSaverAccount();
        SecretAccount secret = new SecretAccount();
        GeneralAccount generalAccount = new GeneralAccount();

        CustomerAB customerAB = new CustomerAB();
        ArrayList<Account> all = new ArrayList<Account>();
        all.add(supersaver);
        all.add(secret);
        all.add(generalAccount);
        ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
        general.add(generalAccount);
        general.add(supersaver);

        customerAB.aAll = all;
        customerAB.bGeneral = general;

        Yaml yaml = new Yaml();
        String dump = yaml.dump(customerAB);
        // System.out.println(dump);
        CustomerAB parsed = (CustomerAB) yaml.load(dump);

    }

    public void testAB2() {
        SuperSaverAccount supersaver = new SuperSaverAccount();
        SecretAccount secret = new SecretAccount();
        GeneralAccount generalAccount = new GeneralAccount();

        CustomerAB customerAB = new CustomerAB();
        ArrayList<Account> all = new ArrayList<Account>();
        all.add(supersaver);
        all.add(secret);
        all.add(generalAccount);
        ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
        general.add(generalAccount);
        general.add(supersaver);

        customerAB.aAll = all;
        customerAB.bGeneral = general;

        JavaBeanDumper dumper = new JavaBeanDumper();
        String dump2 = dumper.dump(customerAB);
        // System.out.println(dump2);
        JavaBeanLoader<CustomerAB> loader = new JavaBeanLoader<CustomerAB>(CustomerAB.class);
        CustomerAB parsed = loader.load(dump2);

    }

    public void testBA() {
        SuperSaverAccount supersaver = new SuperSaverAccount();
        SecretAccount secret = new SecretAccount();
        GeneralAccount generalAccount = new GeneralAccount();

        CustomerBA customerBA = new CustomerBA();
        ArrayList<Account> all = new ArrayList<Account>();
        all.add(supersaver);
        all.add(secret);
        all.add(generalAccount);
        ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
        general.add(generalAccount);
        general.add(supersaver);

        customerBA.aGeneral = general;
        customerBA.bAll = all;

        Yaml yaml = new Yaml();
        String dump = yaml.dump(customerBA);
        // System.out.println(dump);
        //
        CustomerBA parsed = (CustomerBA) yaml.load(dump);
        assertEquals(3, parsed.bAll.size());
        assertEquals(2, parsed.aGeneral.size());
        assertEquals(GeneralAccount.class, parsed.aGeneral.toArray()[0].getClass());
        assertEquals(SuperSaverAccount.class, parsed.aGeneral.toArray()[1].getClass());
    }
}

@@ -877,16 +877,102 @@
 .Yaml;%0A%0A
+/**%0A * @see issue 82: property order influence when aliased in generic collection%0A */%0A
 public c
@@ -2491,35 +2491,32 @@
 omerAB);%0A       
- //
  System.out.prin
@@ -2614,17 +2614,35 @@
 d testAB
-2
+withJavaBeanHelpers
 () %7B%0A   
@@ -4219,35 +4219,32 @@
 omerBA);%0A       
- //
  System.out.prin
@@ -4425,29 +4425,16 @@
         
-assertEquals(
 GeneralA
@@ -4435,31 +4435,34 @@
 neralAccount
-.class,
+%5B%5D array =
  parsed.aGen
@@ -4474,17 +4474,88 @@
 toArray(
-)
+new GeneralAccount%5B2%5D);%0A        assertEquals(GeneralAccount.class, array
 %5B0%5D.getC
@@ -4613,33 +4613,13 @@
 ss, 
-p
 ar
-sed.aGeneral.toAr
 ray
-()
 %5B1%5D.
@@ -4627,20 +4627,78 @@
 etClass());%0A
+        assertEquals(%22SuperSaverAccount%22, array%5B1%5D.name);%0A
     %7D%0A%7D%0A

Update PropOrderInfluenceWhenAliasedInGenericCollectionTest (name is wrong)