/*

BSD license

Copyright (c) 2005, Jean Lazarou
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer. 
Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution. 
Neither the name of the @home Company! nor the names of its contributors may be 
used to endorse or promote products derived from this software without specific 
prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/*
 * Created on 27-juil.-2005
 */
package net.sf.omap;

import net.sf.omap.model.talk.Talk;
import net.sf.omap.model.talk.User;
import junit.framework.TestCase;

public class TestMapperResolution extends TestCase {

    Talk talk;
    User james;
    
    MappingExpert expert;
    
    protected void setUp() throws Exception {
        
        james = new User();
        james.setName("James Bond");
        james.setBirthDate(TestingService.createDate(19621225));
        james.setCivilId("007-Top-Secret");
    
        talk = new Talk();
        talk.setData("user", james);
        
        expert = new ObjectToXMLMappingExpert();
        
    }

    public void testUseDefaultCategory() {
        
        String expected = 
            "<talk>\n" +
            "<talker><user>\n" +
            "<id><![CDATA[007-Top-Secret]]></id>\n" +
            "<name><![CDATA[James Bond]]></name>\n" +
            "</user>\n" 
            "</talker>\n" 
            "</talk>\n";
        
        expert.register(Talk.class, getTalkMapping());
        expert.register(User.class, getUserMapping());
        
        String xml = (Stringexpert.map(talk);
        
        assertEquals(expected, xml);
        
    }

    public void testFallBackToDefaultCategory() {
        
        String expected = 
            "<talk>\n" +
            "<talker><user>\n" +
            "<id><![CDATA[007-Top-Secret]]></id>\n" +
            "<name><![CDATA[James Bond]]></name>\n" +
            "</user>\n" 
            "</talker>\n" 
            "</talk>\n";
        
        expert.register("talk", Talk.class, getTalkMapping());
        expert.register(User.class, getUserMapping());
        
        String xml = (Stringexpert.map("talk", talk);
        
        assertEquals(expected, xml);
        
    }

    public void testCategoryDefinitionHidesDefault() {
        
        String expected = 
            "<talk>\n" +
            "<talker><james>\n" +
            "<id><![CDATA[007-Top-Secret]]></id>\n" +
            "</james>\n" 
            "</talker>\n" 
            "</talk>\n";
        
        expert.register(User.class, getUserMapping());
        expert.register("talk", Talk.class, getTalkMapping());
        expert.register("talk", User.class, getShortUserMapping());
        
        String xml = (Stringexpert.map("talk", talk);
        
        assertEquals(expected, xml);
        
    }

    public void testAlwaysSearchFirstForCategoryDefinition() {
        
        String expected = 
            "<talk>\n" +
            "<talker><james>\n" +
            "<id><![CDATA[007-Top-Secret]]></id>\n" +
            "</james>\n" 
            "</talker>\n" 
            "</talk>\n";
        
        expert.register(User.class, getUserMapping());
        expert.register(Talk.class, getTalkMapping());
        expert.register("talk", User.class, getShortUserMapping());
        
        String xml = (Stringexpert.map("talk", talk);
        
        assertEquals(expected, xml);
        
    }

    public void testLocalDefinitionReplacesCategoryDefinition() {
        
        String expected = 
            "<talk>\n" +
            "<talker><user>\n" +
            "<id><![CDATA[007-Top-Secret]]></id>\n" +
            "<name><![CDATA[James Bond]]></name>\n" +
            "<date>1962-12-25</date>\n" +
            "</user>\n" 
            "</talker>\n" 
            "</talk>\n";
        
        expert.register(Talk.class, getTalkMapping());
        expert.register("talk", User.class, getUserMapping());
        expert.register("talk", getTalkAndUserMapping());
        
        String xml = (Stringexpert.map("talk", talk);
        
        assertEquals(expected, xml);
                
    }
    
    String getTalkAndUserMapping() {
    
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("alias ");
        buffer.append(Talk.class.getName());
        buffer.append(" Talk\n");
        
        buffer.append("alias ");
        buffer.append(User.class.getName());
        buffer.append(" User\n");

        buffer.append("\n\n");
        
        buffer.append(getTalkMapping());
        
        buffer.append("\n\n");
        
        buffer.append(getFullUserMapping());
        
        return buffer.toString();
        
    }
    
    String getTalkMapping() {
        
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("Talk -> talk\n");
        buffer.append("   data.user -> talker\n");
        
        return buffer.toString();
        
    }

    String getFullUserMapping() {
        
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("User -> user\n");
        buffer.append("  civil-id -> id\n");
        buffer.append("  name\n");
        buffer.append("  birth-date -> date as 'yyyy-MM-dd'\n");
        
        return buffer.toString();
        
    }

    String getUserMapping() {
        
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("User -> user\n");
        buffer.append("  civil-id -> id\n");
        buffer.append("  name\n");
        
        return buffer.toString();
        
    }

    String getShortUserMapping() {
        
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("User -> james\n");
        buffer.append("  civil-id -> id\n");
        
        return buffer.toString();
        
    }

}