113
|
1 package pathfinder.mergetest;
|
|
2
|
|
3 import java.util.LinkedList;
|
|
4 import remoteeditor.command.REPCommand;
|
|
5 import remoteeditor.network.REP;
|
|
6
|
|
7 public class TestMerger {
|
|
8 static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
|
|
9 private NetworkSimulator<REPCommand> ns=null;
|
|
10 private LinkedList<EditorSimulator> editors;
|
|
11 private SeMaSimulator<REPCommand> sema;
|
125
|
12 private UsersSimulator users;
|
113
|
13
|
|
14 public TestMerger(){
|
|
15 editors = new LinkedList<EditorSimulator>();
|
|
16 }
|
|
17
|
|
18 public static void main(String[] args){
|
|
19 TestMerger tm;
|
124
|
20 /* get the number of Editors. */
|
113
|
21 int i = (args.length>0) ? Integer.parseInt(args[0]) : 2;
|
|
22 System.out.println("number of Editor = "+i);
|
124
|
23 /* get the number of Command. */
|
113
|
24 int j = (args.length>1) ? Integer.parseInt(args[1]) : 3;
|
124
|
25 System.out.println("number of Packet = "+j);
|
|
26
|
|
27 /* create, initialize and start test. */
|
113
|
28 tm = new TestMerger();
|
125
|
29 tm.init(true, i, j);
|
113
|
30 tm.startTest();
|
|
31
|
125
|
32 //tm.printAllTexts();
|
113
|
33 //if (!tm.checkCS())
|
|
34 // System.out.println("Error!! :some ChannelSimulator still have packet!");
|
|
35 if (!tm.checkEquality())
|
|
36 System.out.println("Error!! :all Editor's text is NOT mutch!");
|
|
37 assert tm.checkEquality();
|
|
38 }
|
|
39
|
|
40 private void startTest() {
|
124
|
41 /* start all Editors. */
|
113
|
42 for (EditorSimulator ee: editors){
|
|
43 ee.start();
|
|
44 }
|
124
|
45 /* start SessionManager if it exist. */
|
125
|
46 if (sema!=null) sema.init();
|
113
|
47 if (sema!=null) sema.start();
|
125
|
48 users.init();
|
|
49 users.start();
|
113
|
50
|
124
|
51 /* wait Editors finish. */
|
113
|
52 for (EditorSimulator ee: editors){
|
|
53 try {
|
|
54 ee.join();
|
|
55 } catch (InterruptedException e) {
|
|
56 e.printStackTrace();
|
|
57 }
|
|
58 }
|
124
|
59 /* inform SessionManager to finish. */
|
113
|
60 if (sema!=null) sema.finish();
|
|
61 }
|
|
62
|
|
63 private void init(boolean sm, int ne, int np){
|
124
|
64 /* create NetworkSimulator, and SessionManager if it's required. */
|
113
|
65 if (sm){
|
|
66 ns = new NetworkSimulatorwithSeMa<REPCommand>();
|
|
67 sema = new SeMaSimulator<REPCommand>(ns, ne);
|
|
68 } else {
|
|
69 ns = new NetworkSimulatorwithoutSeMa<REPCommand>();
|
|
70 sema = null;
|
|
71 }
|
|
72
|
125
|
73 /* create UsersSimulator. */
|
|
74 users = new UsersSimulator(ns, ne, np*ne);
|
|
75
|
124
|
76 /* create ne Editors and np commands. */
|
113
|
77 for (int i=0; i<ne; i++){
|
|
78 LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
|
|
79 // 各エディタが送信するコマンド列を生成
|
|
80
|
124
|
81 /* create command list. */
|
125
|
82 /*
|
113
|
83 for (int j=0; j<np; j++){
|
|
84 String str = "created by Editor"+i+":"+j;
|
|
85 REPCommand cmd = new REPCommand(REP.REPCMD_INSERT,
|
|
86 0, i, j,
|
|
87 10, //Verify.random(text.size()-1), //size-1?
|
|
88 str.length(), str);
|
|
89 cmds.add( cmd);
|
|
90 }
|
125
|
91 */
|
113
|
92
|
124
|
93 /* create a Editor, and pass command list to it. */
|
113
|
94 EditorSimulator ee = new EditorSimulator(i, ns, cmds, "Editor"+i);
|
|
95 if(i==0) ee.setOwner(true);
|
|
96 editors.add(ee);
|
|
97 }
|
|
98 }
|
|
99
|
|
100 private void printAllTexts(){
|
|
101 for(EditorSimulator ee: editors){
|
|
102 System.out.println("--"+ee.getName()+"------------------------");
|
|
103 ee.getText().printAllText();
|
|
104 }
|
|
105 }
|
|
106 /*
|
|
107 private boolean checkCS(){
|
|
108 return ns.checkAllCS();
|
|
109 }
|
|
110 */
|
|
111 private boolean checkEquality(){
|
|
112 /*
|
|
113 Text ee0 = editors.remove().getText();
|
|
114 return editors.remove().getText().equals(ee0);
|
|
115 */
|
|
116 Text text0 = editors.element().getText();
|
|
117 for(EditorSimulator ee: editors){
|
|
118 if (!text0.equals(ee.getText())) return false;
|
|
119 }
|
|
120 return true;
|
|
121 }
|
|
122 }
|