1 package org.apache.jcs.auxiliary.lateral.socket.tcp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.Serializable;
23
24 import junit.framework.TestCase;
25
26 import org.apache.jcs.JCS;
27 import org.apache.jcs.engine.CacheElement;
28 import org.apache.jcs.engine.behavior.ICacheElement;
29
30 /***
31 * @author asmuts
32 */
33 public class LateralTCPFilterRemoveHashCodeUnitTest
34 extends TestCase
35 {
36
37
38
39 private static boolean isSysOut = true;
40
41 /***
42 * Constructor for the TestDiskCache object.
43 *
44 * @param testName
45 */
46 public LateralTCPFilterRemoveHashCodeUnitTest( String testName )
47 {
48 super( testName );
49 }
50
51 /***
52 * Test setup
53 */
54 public void setUp()
55 {
56 JCS.setConfigFilename( "/TestTCPLateralRemoveFilter.ccf" );
57 }
58
59 /***
60 *
61 * @throws Exception
62 */
63 public void test()
64 throws Exception
65 {
66 this.runTestForRegion( "region1", 200, 1 );
67 }
68
69 /***
70 * This tests issues tons of puts. It also check to see that a key that was
71 * put in was removed by the clients remove command.
72 *
73 * @param region
74 * Name of the region to access
75 * @param numOps
76 * @param testNum
77 *
78 * @exception Exception
79 * If an error occurs
80 */
81 public void runTestForRegion( String region, int numOps, int testNum )
82 throws Exception
83 {
84
85
86
87 JCS cache = JCS.getInstance( region );
88
89 Thread.sleep( 100 );
90
91 TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
92 lattr2.setTcpListenerPort( 1102 );
93 lattr2.setTransmissionTypeName( "TCP" );
94 lattr2.setTcpServer( "localhost:1110" );
95 lattr2.setIssueRemoveOnPut( true );
96
97 lattr2.setAllowPut( false );
98
99
100
101
102 LateralTCPService service = new LateralTCPService( lattr2 );
103 service.setListenerId( 123456 );
104
105 String keyToBeRemovedOnPut = "test1";
106
107 String keyToNotBeRemovedOnPut = "test2";
108
109 Serializable dataToPassHashCodeCompare = new Serializable()
110 {
111 private static final long serialVersionUID = 1L;
112
113 public int hashCode()
114 {
115 return 1;
116 }
117 };
118
119
120
121
122
123 cache.put( keyToBeRemovedOnPut, "this should get removed." );
124 ICacheElement element1 = new CacheElement( region, keyToBeRemovedOnPut, region
125 + ":data-this shouldn't get there" );
126 service.update( element1 );
127
128 cache.put( keyToNotBeRemovedOnPut, dataToPassHashCodeCompare );
129 ICacheElement element2 = new CacheElement( region, keyToNotBeRemovedOnPut, dataToPassHashCodeCompare );
130 service.update( element2 );
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 JCS jcs = JCS.getInstance( region );
147 String key = "testKey" + testNum;
148 String data = "testData" + testNum;
149 jcs.put( key, data );
150 String value = (String) jcs.get( key );
151 assertEquals( "Couldn't put normally.", data, value );
152
153
154 for ( int i = 1; i < numOps; i++ )
155 {
156 String keyL = "key" + i;
157 String dataL = (String) jcs.get( keyL );
158 if ( dataL != null )
159 {
160 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
161 }
162
163 }
164
165 Thread.sleep( 200 );
166
167 Object testObj1 = cache.get( keyToBeRemovedOnPut );
168 p( "test object1 = " + testObj1 );
169 assertNull( "The test object should have been remvoed by a put.", testObj1 );
170
171 Object testObj2 = cache.get( keyToNotBeRemovedOnPut );
172 p( "test object2 = " + testObj2 + " hashCode = " );
173 if ( testObj2 != null )
174 {
175 p( "test2 hashcode = " + +testObj2.hashCode() );
176 }
177 assertNotNull( "This should not have been removed, since the hascode were the same.", testObj2 );
178
179 }
180
181 /***
182 * @param s String to print
183 */
184 public static void p( String s )
185 {
186 if ( isSysOut )
187 {
188 System.out.println( s );
189 }
190 }
191 }