package test.org.itscool.commons.document2;

import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;

import org.itscool.commons.document.BeanToXmlDocument;
import org.itscool.commons.document.DomParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class BeanToXmlDocumentTest extends TestCase{
    
    /**
     * クラスの継承に対するXML⇒JavaBeansマッピングテスト
     */
    public void testParseList(){
        SampleSubBean2 bean = new SampleSubBean2();
        bean.setId(1);
        bean.setName("ほげ　たろう");
        bean.setAddr("大阪府 茨木市...");
        bean.setTel("090-0000-1234");
        
        try{
            BeanToXmlDocument beanToXml = new BeanToXmlDocument("root");
            
            //マッピング定義
            beanToXml.replaceClassToTagName(SampleSubBean2.class.getName(), "sample_sub_bean");
            
            //オブジェクト追加
            beanToXml.addElement(bean);
            
            Document document = beanToXml.getDocument();
            
            String text = beanToXml.parseTextIndent("utf-8");
            System.out.println("---- test2 start ----");
            System.out.println(text);
            System.out.println("---- test2 end ----");
            
            DomParser parser = new DomParser(document);
            
            NodeList beanNodes = parser.getElementsByTagNames("root.sample_sub_bean" );
            
            assertEquals("bean num", beanNodes.getLength(), 1);
            Node beanNode = beanNodes.item(0);
            String beanTagName = beanNode.getNodeName();
            String id = parser.getFirstTextElementByTagNames(beanNode.getChildNodes(), "id");
            String name = parser.getFirstTextElementByTagNames(beanNode.getChildNodes(), "name");
            String addr = parser.getFirstTextElementByTagNames(beanNode.getChildNodes(), "addr");
            String tel = parser.getFirstTextElementByTagNames(beanNode.getChildNodes(), "tel");
            
            assertEquals("beanName", beanTagName, "sample_sub_bean");
            assertEquals("id", id, "1");
            assertEquals("name", name, "ほげ　たろう");
            assertEquals("addr", addr, "大阪府 茨木市...");
            assertEquals("tel", tel, "090-0000-1234");
            
        }catch(Exception e){
            e.printStackTrace();
            fail(e.getMessage());
        }
    }
    
}
