view src/pathfinder/mergetest/TestMerger2.java @ 154:6a3c982bd72a

*** empty log message ***
author pin
date Sun, 24 Aug 2008 13:43:50 +0900
parents 6326e5ea4595
children 1a2269c820df
line wrap: on
line source

package pathfinder.mergetest;

import java.util.LinkedList;
import remoteeditor.command.REPCommand;
import remoteeditor.network.REP;

public class TestMerger2 {
	static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
	protected NetworkSimulator<REPCommand> ns=null;
	protected LinkedList<EditorSimulatorWithoutMerger> editors;
	protected SessionManagerSimulatorWithMerger<REPCommand> sema;
	protected LinkedList<UsersSimulator> users = new LinkedList<UsersSimulator>();
	
	static private String[] text0 = {
		"aaa", "bbb", "ccc", "ddd", "eee",
		"fff", "ggg", "hhh", "iii", "jjj",
		"kkk", "lll", "mmm", "nnn", "ooo",
		"ppp", "qqq", "rrr", "sss", "ttt",
		"uuu", "vvv", "www", "xxx", "yyy", "zzz"
	};	
	static private String[] text1 = {""};

	public TestMerger2(){
		editors = new LinkedList<EditorSimulatorWithoutMerger>();
	}

	public static void main(String[] args){
		TestMerger2 tm;
		/* get the number of Editors.  */
		int i = (args.length>0) ? Integer.parseInt(args[0]) : 4;
		System.out.println("number of Editor = "+i);
		/* get the number of Command.  */
		int j = (args.length>1) ? Integer.parseInt(args[1]) : 3;
		System.out.println("number of Packet = "+j);

		/* create, initialize and start test.  */
		tm = new TestMerger2();
		tm.init(true, i, j, 1);
		tm.startTest();

		tm.printAllTexts();
		//if (!tm.checkCS())
		//	System.out.println("Error!! :some ChannelSimulator still have packet!");
		if (!tm.checkEquality())
			System.out.println("Error!! :all Editor's text is NOT mutch!");
		assert tm.checkEquality();
	}

	protected void startTest() {
		/* start all Editors.  */
		for (EditorSimulatorWithoutMerger ee: editors){
			ee.start();
		}
		/* start SessionManager if it exist.  */
		if (sema!=null) sema.init();
		if (sema!=null) sema.start();
		
		for(UsersSimulator u: users){
			u.start();
		}
		for(UsersSimulator u: users){
			try {
				u.join();
			} catch (InterruptedException e) { }
		}


		/* wait Editors finish.  */
		for (EditorSimulatorWithoutMerger ee: editors){
			try {
				ee.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		/* inform SessionManager to finish.  */
		if (sema!=null) {
			sema.finish();
			try {
				sema.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	 LinkedList<REPCommand> userCommand(int eid){
		LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
		int sid = 0; int eid2 = -1; int seq = 0; int line = 0;
		cmds .add(new REPCommand(REP.REPCMD_INSERT, sid, eid2, seq++, line, 0, "inserted by editor:" + eid + ":0"));
		cmds .add(new REPCommand(REP.REPCMD_INSERT, sid, eid2, seq++, line, 0, "inserted by editor:" + eid + ":1"));
		cmds .add(new REPCommand(REP.REPCMD_INSERT, sid, eid2, seq++, line, 0, "inserted by editor:" + eid + ":2"));
		if(eid == 0) cmds.add(new REPCommand(REP.SMCMD_QUIT, sid, eid, seq++, line, 0, "sent by editor:" + eid + ":3"));
		return cmds;
	}
	
	protected void init(boolean sm, int ne, int np, int ll){
		/* create NetworkSimulator, and SessionManager if it's required.   */
		if (sm){
			ns = new NetworkSimulatorwithSeMa<REPCommand>();
			sema = new SessionManagerSimulatorWithMerger<REPCommand>(ns, ne, 0);
		} else {
			ns = new NetworkSimulatorwithoutSeMa<REPCommand>();
			sema = null;
		}
		ns.setLogLevel(ll);
		
		/* create UsersSimulator.  */
		for(int i = 0; i < ne; i++){
			users.add(new UsersSimulator(ns, i, userCommand(i)));
		}
		
		/* create ne Editors and np commands.  */
		for (int i=0; i<ne; i++){
			/* create a Editor, and pass command list to it.  */
			EditorSimulatorWithoutMerger ee;
			if(i == 0) {
				ee = new EditorSimulatorWithoutMerger(i, ns, text0, "Editor:"+i);
			}else {
				ee = new EditorSimulatorWithoutMerger(i, ns, text0, "Editor:"+i);
			}
			
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
		
		
	}

	protected void printAllTexts(){
		for(EditorSimulatorWithoutMerger ee: editors){
			System.out.println("--"+ee.getName()+"------------------------");
			ee.getText().printAllText();
		}
	}
/*
	private boolean checkCS(){
		return ns.checkAllCS();
	}
*/
	protected boolean checkEquality(){
		/*
		Text ee0 = editors.remove().getText();
		return editors.remove().getText().equals(ee0);
		*/
		Text text0 = editors.element().getText();
		//System.out.println("------------------- 結果 --------------------");
		for(EditorSimulatorWithoutMerger ee: editors){
			//System.out.println(" ");
			//ee.getText().printAllText();
			if (!text0.equals(ee.getText())) return false;
		}
		return true;
	}
}