1   package org.apache.jcs.auxiliary.lateral.socket.tcp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      //private static boolean isSysOut = false;
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          //boolean show = true;//false;
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          // should still try to remove
97          lattr2.setAllowPut( false );
98  
99          // this service will put and remove using the lateral to
100         // the cache instance above
101         // the cache thinks it is different since the listenerid is different
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         //String dataToPassHashCodeCompare = "this should be the same and not
119         // get removed.";
120         //p( "dataToPassHashCodeCompare hashcode = " + +
121         // dataToPassHashCodeCompare.hashCode() );
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          * try { for ( int i = 1; i < numOps; i++ ) { Random ran = new Random( i );
134          * int n = ran.nextInt( 4 ); int kn = ran.nextInt( range ); String key =
135          * "key" + kn;
136          *
137          * ICacheElement element = new CacheElement( region, key, region +
138          * ":data" + i + " junk asdfffffffadfasdfasf " + kn + ":" + n );
139          * service.update( element ); if ( show ) { p( "put " + key ); }
140          *
141          * if ( i % 100 == 0 ) { System.out.println( cache.getStats() ); }
142          *  } p( "Finished cycle of " + numOps ); } catch ( Exception e ) { p(
143          * e.toString() ); e.printStackTrace( System.out ); throw e; }
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         // make sure the items we can find are in the correct region.
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 }