view src/treecms/proto/test/EditorTest.java @ 38:e647bb37b7ad

added treecms.proto.merge
author suika6039
date Thu, 30 Dec 2010 00:50:47 +0900
parents
children ea7f0a4eacaf
line wrap: on
line source

package treecms.proto.test;

import java.util.LinkedList;

import junit.framework.Assert;

import org.junit.Test;

import treecms.proto.api.*;
import treecms.proto.util.PreOrderTreeWalker;

public class EditorTest
{
	private Editor m_editor;
	private Node m_root,m_child1,m_child11,m_child12,m_child2;
	
	public EditorTest(Editor _editor)
	{
		m_editor = _editor;
		m_root = _editor.useContents();
		
		//create tree
		m_root.setTitle("root");
		
		m_child1 = m_root.addChild(m_root.createNode());
		m_child1.setTitle("child1");
		m_child2 = m_root.addChild(m_root.createNode());
		m_child2.setTitle("child2");
		
		m_child11 = m_child1.addChild(m_root.createNode());
		m_child11.setTitle("child11");
		m_child12 = m_child1.addChild(m_root.createNode());
		m_child12.setTitle("child12");
	}
	
	@Test
	public void testClone()
	{
		Node target = m_editor.edit(m_child11);
		Assert.assertEquals(true,target.getID().isOrderThen(m_child11.getID()));
	}
	
	public LinkedList<Node> findPath(Node _root,Node _target)
	{
		LinkedList<Node> path = new LinkedList<Node>();
		
		if(findPath(_root,_target,path)){
			path.addFirst(_root);
		}
		
		return path;
	}
	
	private boolean findPath(Node _parent,Node _target,LinkedList<Node> _path)
	{
		if(_target.getID().isFamily(_parent.getID())){
			return true;
		}
		
		for(Node child : _parent.getChildren()){
			if(findPath(child,_target,_path)){
				_path.addFirst(child);
				return true;
			}
		}
		
		return false;
	}
	
	public void print(Node _root)
	{
		print(_root,0);
	}
	
	private void print(Node _root,int _indent)
	{
		for(int i = 0;i < _indent;i ++){
			System.out.print("\t");
		}
		System.out.println(_root.getTitle()+"["+_root.getID()+"]");
		for(Node child : _root.getChildren()){
			print(child,_indent+1);
		}
	}
}