package com.thoughtworks.acceptance;

import com.thoughtworks.acceptance.someobjects.X;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Paul Hammant
 */
public class AliasTest extends AbstractAcceptanceTest {

    public void testBarfsIfItDoesNotExist() {

        String xml = "" +
                "<X-array>\n" +
                "  <X>\n" +
                "    <anInt>0</anInt>\n" +
                "  </X>\n" +
                "</X-array>";

        // now change the alias
        xstream.alias("Xxxxxxxx", X.class);
        try {
            xstream.fromXML(xml);
            fail("ShouldCannotResolveClassException expected");
        } catch (CannotResolveClassException expectedException) {
            // expected
        }
    }
    
    //FIXME underscore is mapped to __ 
    public void FIXMEtestWithUnderscore() {
        String xml = "" +
                "<X_alias>\n" +
                "  <anInt>0</anInt>\n" +
                "</X_alias>";

        // now change the alias
        xstream.alias("X_alias", X.class);
        X x = new X(0);
        assertBothWays(x, xml);
    }

    final static class Software {
        String vendor;
        String name;
        
        public Software(String vendor, String name) {
            this.name = name;
            this.vendor = vendor;
        }
        
        protected Software() {
            // for JDK 1.3
        }
    }
    
    public void testForFieldAsAttribute() {
        Software software = new Software("walness", "xstream");
        
        xstream.alias("software", Software.class);
        xstream.useAttributeFor(String.class);
        xstream.aliasAttribute("id", "name");
        
        String xml = "<software vendor=\"walness\" id=\"xstream\"/>";
        
        assertBothWays(software, xml);
    }
    
    public void testForReferenceAttribute() {
        List list = new ArrayList();
        Software software = new Software("walness", "xstream");
        list.add(software);
        list.add(software);
        
        xstream.alias("software", Software.class);
        xstream.useAttributeFor(String.class);
        xstream.aliasAttribute("refid", "reference");
        
        String xml = "" + 
            "<list>\n" +
            "  <software vendor=\"walness\" name=\"xstream\"/>\n" +
            "  <software refid=\"../software\"/>\n" +
            "</list>";
        
        assertBothWays(list, xml);
    }
    
}

@@ -2505,10 +2505,792 @@
  %7D%0A    %0A
+    private static class FieldsWithInternalNames %7B%0A        String clazz;%0A        String ref;%0A    %7D%0A%0A    public void testCanUseInternalNameAsFieldAlias() %7B%0A        FieldsWithInternalNames object = new FieldsWithInternalNames();%0A        object.clazz = %22TestIt%22;%0A        object.ref = %22MyReference%22;%0A%0A        xstream.alias(%22internalNames%22, FieldsWithInternalNames.class);%0A        xstream.aliasField(%22class%22, FieldsWithInternalNames.class, %22clazz%22);%0A        xstream.aliasField(%22reference%22, FieldsWithInternalNames.class, %22ref%22);%0A        %0A        String xml = %22%22%0A                + %22%3CinternalNames%3E%5Cn%22%0A                + %22  %3Cclass%3ETestIt%3C/class%3E%5Cn%22%0A                + %22  %3Creference%3EMyReference%3C/reference%3E%5Cn%22%0A                + %22%3C/internalNames%3E%22;%0A%0A        assertBothWays(object, xml);%0A    %7D%0A
 %7D%0A

Test field aliases for internal attribute names.