/*
 * Copyright 2007 Kickmogu Co. Ltd. All rights reserved.
 *
 * 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.
 */



/*
 * 作成日: 2006/12/19
 *
 */
package com.kickmogu.rhythm.core.test;

import com.kickmogu.rhythm.core.*;
import com.thoughtworks.xstream.XStream;
import java.io.*;
import jp.co.dgic.testing.framework.DJUnitTestCase;

public class RhythmTreeTest extends DJUnitTestCase {
	RhythmTree root;
	RhythmTree firstChild;
	RhythmTree secondChild;
    XStream xstream = new XStream();
    Writer writer = null;
    Reader reader = null;

	protected void setUp() throws Exception {
		super.setUp();
		// XMLデータを出力するライタを作成
        root = new RhythmTree();
        root.setName("一番親です");
        firstChild = new RhythmTree();
        firstChild.setName("最初の子供です");
        secondChild = new RhythmTree();
        secondChild.setName("次男です");
        RhythmTree mago1Child = new RhythmTree();
        mago1Child.setName("最初の子供の孫１");
        RhythmTree mago2Child = new RhythmTree();
        mago2Child.setName("最初の子供の孫2");
        root.addToChild(firstChild);
        firstChild.addToChild(mago1Child);
        firstChild.addToChild(mago2Child);
        secondChild.addToParent(root);

        writer = new FileWriter("c:/RhythmTree.xml");
        reader = new FileReader("c:/RhythmTree.xml");

        // XStreamインスタンスを生成し、XMLデータをライタへ出力
        xstream.toXML(root, writer);
        System.out.println("------------初期のTree------------");
        System.out.println(root.toString());
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void test001() throws Exception {
        // 削除してみる
        firstChild.remove();
        System.out.println("------------削除後のTree------------");
        System.out.println(root.toString());
        // XMLから読み込み
        //      XMLデータを取得するリーダを作成

        // XStreamインスタンスを生成し、リーダからインスタンスを復元
        RhythmTree newRoot = (RhythmTree)xstream.fromXML(reader);
        System.out.println("------------XMLから読み込んだTree------------");
        System.out.println(newRoot.toString());
	}
	
	public void test002() throws Exception {
        // 長男を次男の子供にする
        firstChild.changeParent(secondChild);
        System.out.println("------------長男を次男の子供にしたTree------------");
        System.out.println(root.toString());
	}

	public void test003() throws Exception {
        // 長男を次男の子供にする
		secondChild.moveSeq(0);
        System.out.println("------------次男を長男にしたTree------------");
        System.out.println(root.toString());
	}

	public void test004() throws Exception {
        System.out.println("------------XML表現Tree------------");
        System.out.println(root.toXmlAll());
	}
}
