view src/pathfinder/mergetest/TestMerger.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 TestMerger {
	static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
	protected NetworkSimulator<REPCommand> ns=null;
	protected LinkedList<EditorSimulator> editors;
	protected SeMaSimulator<REPCommand> sema;
	protected UsersSimulator users;

	public TestMerger(){
		editors = new LinkedList<EditorSimulator>();
	}

	public static void main(String[] args){
		TestMerger tm;
		/* get the number of Editors.  */
		int i = (args.length>0) ? Integer.parseInt(args[0]) : 3;
		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 TestMerger();
		//tm.init(false, i, j, 1);  // init( boolean SM?, int max_client, int max_packet, int loglevel);
		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 (EditorSimulator ee: editors){
			ee.start();
		}
		/* start SessionManager if it exist.  */
		if (sema!=null) sema.init();
		if (sema!=null) sema.start();
		users.init();
		users.start();

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

	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 SeMaSimulator<REPCommand>(ns, ne);
		} else {
			ns = new NetworkSimulatorwithoutSeMa<REPCommand>();
			sema = null;
		}
		ns.setLogLevel(ll);

		/* create UsersSimulator.  */
		//users = new UsersSimulator(ns, ne, np*ne);

		/* create ne Editors and np commands.  */
		for (int i=0; i<ne; i++){
			LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
			// 各エディタが送信するコマンド列を生成

			/* create command list.  */
			/*
			for (int j=0; j<np; j++){
				String str = "created by Editor"+i+":"+j;
				REPCommand cmd = new REPCommand(REP.REPCMD_INSERT,
					0, i, j,
					10, //Verify.random(text.size()-1), //size-1?
					str.length(), str);
				cmds.add( cmd);
			}
			*/

			/* create a Editor, and pass command list to it.  */
			EditorSimulator ee = null; //new EditorSimulator(i, ns, cmds, "Editor"+i);
			if(i==0) ee.setOwner(true);
			editors.add(ee);
		}
	}

	protected void printAllTexts(){
		for(EditorSimulator 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(EditorSimulator ee: editors){
			//System.out.println(" ");
			//ee.getText().printAllText();
			if (!text0.equals(ee.getText())) return false;
		}
		return true;
	}
}