1 package org.apache.commons.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 junit.framework.TestCase;
23 import org.apache.commons.jcs.JCS;
24 import org.apache.commons.jcs.access.CacheAccess;
25 import org.apache.commons.jcs.engine.CacheElement;
26 import org.apache.commons.jcs.engine.behavior.ICacheElement;
27
28 import java.io.Serializable;
29
30
31
32
33 public class LateralTCPFilterRemoveHashCodeUnitTest
34 extends TestCase
35 {
36
37 private static boolean isSysOut = false;
38
39
40 private final int serverPort = 2001;
41
42
43
44
45
46
47 public LateralTCPFilterRemoveHashCodeUnitTest( String testName )
48 {
49 super( testName );
50 }
51
52
53
54
55 @Override
56 public void setUp()
57 {
58 System.setProperty( "jcs.auxiliary.LTCP.attributes.TcpServers", "localhost:" + serverPort );
59 JCS.setConfigFilename( "/TestTCPLateralRemoveFilter.ccf" );
60 }
61
62
63
64
65
66 public void test()
67 throws Exception
68 {
69 this.runTestForRegion( "region1", 200, 1 );
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public void runTestForRegion( String region, int numOps, int testNum )
85 throws Exception
86 {
87 CacheAccess<String, Serializable> 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:" + serverPort );
95 lattr2.setIssueRemoveOnPut( true );
96
97 lattr2.setAllowPut( false );
98
99
100
101
102 LateralTCPService<String, Serializable> service = new LateralTCPService<String, Serializable>( 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 @Override
114 public int hashCode()
115 {
116 return 1;
117 }
118 };
119
120
121
122
123
124 cache.put( keyToBeRemovedOnPut, "this should get removed." );
125 ICacheElement<String, Serializable> element1 = new CacheElement<String, Serializable>( region, keyToBeRemovedOnPut, region
126 + ":data-this shouldn't get there" );
127 service.update( element1 );
128
129 cache.put( keyToNotBeRemovedOnPut, dataToPassHashCodeCompare );
130 ICacheElement<String, Serializable> element2 = new CacheElement<String, Serializable>( region, keyToNotBeRemovedOnPut, dataToPassHashCodeCompare );
131 service.update( element2 );
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 CacheAccess<String, String> jcs = JCS.getInstance( region );
148 String key = "testKey" + testNum;
149 String data = "testData" + testNum;
150 jcs.put( key, data );
151 String value = jcs.get( key );
152 assertEquals( "Couldn't put normally.", data, value );
153
154
155 for ( int i = 1; i < numOps; i++ )
156 {
157 String keyL = "key" + i;
158 String dataL = jcs.get( keyL );
159 if ( dataL != null )
160 {
161 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
162 }
163
164 }
165
166 Thread.sleep( 200 );
167
168 Object testObj1 = cache.get( keyToBeRemovedOnPut );
169 p( "test object1 = " + testObj1 );
170 assertNull( "The test object should have been remvoed by a put.", testObj1 );
171
172 Object testObj2 = cache.get( keyToNotBeRemovedOnPut );
173 p( "test object2 = " + testObj2 + " hashCode = " );
174 if ( testObj2 != null )
175 {
176 p( "test2 hashcode = " + +testObj2.hashCode() );
177 }
178 assertNotNull( "This should not have been removed, since the hascode were the same.", testObj2 );
179
180 }
181
182
183
184
185 public static void p( String s )
186 {
187 if ( isSysOut )
188 {
189 System.out.println( s );
190 }
191 }
192 }