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