1   /*
2    * Copyright (c) 2005, The K-Wf Grid Consortium
3    * Fraunhofer Institute for Computer Architecture and Software Technology
4    * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5    */
6   package net.kwfgrid.gworkflowdl.protocol.server;
7   
8   import net.kwfgrid.gworkflowdl.protocol.Protocol;
9   
10  import junit.framework.TestCase;
11  
12  import org.apache.log4j.Logger;
13  
14  /***
15     Test for the ModificationBuffer.
16   */
17  public class TestModificationBuffer extends TestCase {
18      public void testModificationBuffer() throws Exception {
19  	int SIZE = 10;
20  	ModificationBuffer b = new ModificationBuffer(0, SIZE);
21  	try {
22  	    b.write(0, "v0");
23  	    fail("Write version 0 should throw exception.");
24  	} catch (IndexOutOfBoundsException x) {
25  	    //
26  	}
27  	try {
28  	    b.write(2, "v0");
29  	    fail("Write version 2 should throw exception.");
30  	} catch (IndexOutOfBoundsException x) {
31  	    //
32  	}
33  	try {
34  	    b.write(5, "v0");
35  	    fail("Write version 5 should throw exception.");
36  	} catch (IndexOutOfBoundsException x) {
37  	    //
38  	}
39  	try {
40  	    b.read(-1);
41  	    fail("Read version -1 should throw exception.");
42  	} catch (IndexOutOfBoundsException x) {
43  	    //
44  	}
45  
46  	for (int i=0; i<SIZE*2; i++) {
47  	    b.write(i+1, "v"+(i+1));
48  	}
49  
50  	try {
51  	    b.read(SIZE-1);
52  	    fail("Read version "+(SIZE-1)+" should throw exception.");
53  	} catch (IndexOutOfBoundsException x) {
54  	    //
55  	}
56  
57  	String[][] m = b.read(SIZE);
58  
59  	assertEquals("Number of messages wrong.", SIZE, m.length);
60  	for (int i=0; i<m.length; i++) {
61  	    assertEquals("Wrong version number.", m[i][0], ""+(i+SIZE+1));
62  	    assertEquals("Wrong identifier.", m[i][1], Protocol.IDENTIFIER_MODIFICATION);
63  	    assertEquals("Wrong modification.", m[i][2], "v"+(i+SIZE+1));
64  	}
65  
66  	m = b.read(SIZE+1);
67  
68  	assertEquals("Number of messages wrong.", SIZE-1, m.length);
69  	for (int i=0; i<m.length; i++) {
70  	    assertEquals("Wrong version number.", m[i][0], ""+(i+SIZE+2));
71  	    assertEquals("Wrong identifier.", m[i][1], Protocol.IDENTIFIER_MODIFICATION);
72  	    assertEquals("Wrong modification.", m[i][2], "v"+(i+SIZE+2));
73  	}
74  
75  	m = b.read(SIZE+5);
76  
77  	assertEquals("Number of messages wrong.", SIZE-5, m.length);
78  	for (int i=0; i<m.length; i++) {
79  	    assertEquals("Wrong version number.", m[i][0], ""+(i+SIZE+6));
80  	    assertEquals("Wrong identifier.", m[i][1], Protocol.IDENTIFIER_MODIFICATION);
81  	    assertEquals("Wrong modification.", m[i][2], "v"+(i+SIZE+6));
82  	}
83  
84  	m = b.read(2*SIZE);
85  
86  	assertEquals("Number of messages wrong.", 0, m.length);
87      }
88  }