001    package org.apache.jcs.auxiliary.disk.block;
002    
003    import java.io.File;
004    import java.io.Serializable;
005    import java.util.Map;
006    
007    import junit.framework.TestCase;
008    
009    import org.apache.jcs.engine.CacheElement;
010    import org.apache.jcs.engine.behavior.ICacheElement;
011    import org.apache.jcs.utils.serialization.StandardSerializer;
012    
013    /** Unit tests for the Block Disk Cache */
014    public class BlockDiskCacheUnitTest
015        extends TestCase
016    {
017        /**
018         * Test the basic get matching.
019         * <p>
020         * @throws Exception
021         */
022        public void testPutGetMatching_SmallWait()
023            throws Exception
024        {
025            // SETUP
026            int items = 200;
027    
028            String cacheName = "testPutGetMatching_SmallWait";
029            BlockDiskCacheAttributes cattr = new BlockDiskCacheAttributes();
030            cattr.setCacheName( cacheName );
031            cattr.setMaxKeySize( 100 );
032            cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" );
033            BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>( cattr );
034    
035            // DO WORK
036            for ( int i = 0; i <= items; i++ )
037            {
038                diskCache.update( new CacheElement<String, String>( cacheName, i + ":key", cacheName + " data " + i ) );
039            }
040            Thread.sleep( 500 );
041    
042            Map<String, ICacheElement<String, String>> matchingResults = diskCache.getMatching( "1.8.+" );
043    
044            // VERIFY
045            assertEquals( "Wrong number returned", 10, matchingResults.size() );
046            //System.out.println( "matchingResults.keySet() " + matchingResults.keySet() );
047            //System.out.println( "\nAFTER TEST \n" + diskCache.getStats() );
048        }
049    
050        /**
051         * Test the basic get matching. With no wait this will all come from purgatory.
052         * <p>
053         * @throws Exception
054         */
055        public void testPutGetMatching_NoWait()
056            throws Exception
057        {
058            // SETUP
059            int items = 200;
060    
061            String cacheName = "testPutGetMatching_NoWait";
062            BlockDiskCacheAttributes cattr = new BlockDiskCacheAttributes();
063            cattr.setCacheName( cacheName );
064            cattr.setMaxKeySize( 100 );
065            cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" );
066            BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>( cattr );
067    
068            // DO WORK
069            for ( int i = 0; i <= items; i++ )
070            {
071                diskCache.update( new CacheElement<String, String>( cacheName, i + ":key", cacheName + " data " + i ) );
072            }
073    
074            Map<String, ICacheElement<String, String>> matchingResults = diskCache.getMatching( "1.8.+" );
075    
076            // VERIFY
077            assertEquals( "Wrong number returned", 10, matchingResults.size() );
078            //System.out.println( "matchingResults.keySet() " + matchingResults.keySet() );
079            //System.out.println( "\nAFTER TEST \n" + diskCache.getStats() );
080        }
081    
082        /**
083         * Verify that the block disk cache can handle a big string.
084         * <p>
085         * @throws Exception
086         */
087        public void testChunk_BigString()
088            throws Exception
089        {
090            String string = "This is my big string ABCDEFGH";
091            StringBuffer sb = new StringBuffer();
092            sb.append( string );
093            for ( int i = 0; i < 4; i++ )
094            {
095                sb.append( "|" + i + ":" + sb.toString() ); // big string
096            }
097            string = sb.toString();
098    
099            StandardSerializer elementSerializer = new StandardSerializer();
100            byte[] data = elementSerializer.serialize( string );
101    
102            File file = new File( "target/test-sandbox/BlockDiskCacheUnitTest/testChunk_BigString.data" );
103    
104            BlockDisk blockDisk = new BlockDisk( file, 200, elementSerializer );
105    
106            int numBlocksNeeded = blockDisk.calculateTheNumberOfBlocksNeeded( data );
107            System.out.println( numBlocksNeeded );
108    
109            // get the individual sub arrays.
110            byte[][] chunks = blockDisk.getBlockChunks( data, numBlocksNeeded );
111    
112            byte[] resultData = new byte[0];
113    
114            for ( short i = 0; i < chunks.length; i++ )
115            {
116                byte[] chunk = chunks[i];
117                byte[] newTotal = new byte[data.length + chunk.length];
118                // copy data into the new array
119                System.arraycopy( data, 0, newTotal, 0, data.length );
120                // copy the chunk into the new array
121                System.arraycopy( chunk, 0, newTotal, data.length, chunk.length );
122                // swap the new and old.
123                resultData = newTotal;
124            }
125    
126            Serializable result = elementSerializer.deSerialize( resultData );
127            System.out.println( result );
128            assertEquals( "wrong string after retrieval", string, result );
129        }
130    
131        /**
132         * Verify that the block disk cache can handle a big string.
133         * <p>
134         * @throws Exception
135         */
136        public void testPutGet_BigString()
137            throws Exception
138        {
139            String string = "This is my big string ABCDEFGH";
140            StringBuffer sb = new StringBuffer();
141            sb.append( string );
142            for ( int i = 0; i < 4; i++ )
143            {
144                sb.append( " " + i + sb.toString() ); // big string
145            }
146            string = sb.toString();
147    
148            String cacheName = "testPutGet_BigString";
149    
150            BlockDiskCacheAttributes cattr = new BlockDiskCacheAttributes();
151            cattr.setCacheName( cacheName );
152            cattr.setMaxKeySize( 100 );
153            cattr.setBlockSizeBytes( 200 );
154            cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" );
155            BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>( cattr );
156    
157            // DO WORK
158            diskCache.update( new CacheElement<String, String>( cacheName, "x", string ) );
159    
160            // VERIFY
161            assertNotNull( diskCache.get( "x" ) );
162            Thread.sleep( 1000 );
163            ICacheElement<String, String> afterElement = diskCache.get( "x" );
164            assertNotNull( afterElement );
165            System.out.println( "afterElement = " + afterElement );
166            String after = afterElement.getVal();
167    
168            assertNotNull( after );
169            assertEquals( "wrong string after retrieval", string, after );
170        }
171    
172        /**
173         * Verify that the block disk cache can handle utf encoded strings.
174         * <p>
175         * @throws Exception
176         */
177        public void testUTF8String()
178            throws Exception
179        {
180            String string = "IÒtÎrn‚tiÙn‡lizÊti¯n";
181            StringBuffer sb = new StringBuffer();
182            sb.append( string );
183            for ( int i = 0; i < 4; i++ )
184            {
185                sb.append( sb.toString() ); // big string
186            }
187            string = sb.toString();
188    
189            System.out.println( "The string contains " + string.length() + " characters" );
190    
191            String cacheName = "testUTF8String";
192    
193            BlockDiskCacheAttributes cattr = new BlockDiskCacheAttributes();
194            cattr.setCacheName( cacheName );
195            cattr.setMaxKeySize( 100 );
196            cattr.setBlockSizeBytes( 200 );
197            cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" );
198            BlockDiskCache<String, String> diskCache = new BlockDiskCache<String, String>( cattr );
199    
200            // DO WORK
201            diskCache.update( new CacheElement<String, String>( cacheName, "x", string ) );
202    
203            // VERIFY
204            assertNotNull( diskCache.get( "x" ) );
205            Thread.sleep( 1000 );
206            ICacheElement<String, String> afterElement = diskCache.get( "x" );
207            assertNotNull( afterElement );
208            System.out.println( "afterElement = " + afterElement );
209            String after = afterElement.getVal();
210    
211            assertNotNull( after );
212            assertEquals( "wrong string after retrieval", string, after );
213        }
214    
215        /**
216         * Verify that the block disk cache can handle utf encoded strings.
217         * <p>
218         * @throws Exception
219         */
220        public void testUTF8ByteArray()
221            throws Exception
222        {
223            String string = "IÒtÎrn‚tiÙn‡lizÊti¯n";
224            StringBuffer sb = new StringBuffer();
225            sb.append( string );
226            for ( int i = 0; i < 4; i++ )
227            {
228                sb.append( sb.toString() ); // big string
229            }
230            string = sb.toString();
231            //System.out.println( "The string contains " + string.length() + " characters" );
232            String UTF8 = "UTF-8";
233            byte[] bytes = string.getBytes( UTF8 );
234    
235            String cacheName = "testUTF8ByteArray";
236    
237            BlockDiskCacheAttributes cattr = new BlockDiskCacheAttributes();
238            cattr.setCacheName( cacheName );
239            cattr.setMaxKeySize( 100 );
240            cattr.setBlockSizeBytes( 200 );
241            cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" );
242            BlockDiskCache<String, byte[]> diskCache = new BlockDiskCache<String, byte[]>( cattr );
243    
244            // DO WORK
245            diskCache.update( new CacheElement<String, byte[]>( cacheName, "x", bytes ) );
246    
247            // VERIFY
248            assertNotNull( diskCache.get( "x" ) );
249            Thread.sleep( 1000 );
250            ICacheElement<String, byte[]> afterElement = diskCache.get( "x" );
251            assertNotNull( afterElement );
252            //System.out.println( "afterElement = " + afterElement );
253            byte[] after = afterElement.getVal();
254    
255            assertNotNull( after );
256            assertEquals( "wrong bytes after retrieval", bytes.length, after.length );
257            //assertEquals( "wrong bytes after retrieval", bytes, after );
258            //assertEquals( "wrong bytes after retrieval", string, new String( after, UTF8 ) );
259    
260        }
261    
262        /**
263         * Verify that the block disk cache can handle utf encoded strings.
264         * <p>
265         * @throws Exception
266         */
267        public void testUTF8StringAndBytes()
268            throws Exception
269        {
270            X before = new X();
271            String string = "IÒtÎrn‚tiÙn‡lizÊti¯n";
272            StringBuffer sb = new StringBuffer();
273            sb.append( string );
274            for ( int i = 0; i < 4; i++ )
275            {
276                sb.append( sb.toString() ); // big string
277            }
278            string = sb.toString();
279            //System.out.println( "The string contains " + string.length() + " characters" );
280            String UTF8 = "UTF-8";
281            before.string = string;
282            before.bytes = string.getBytes( UTF8 );
283    
284            String cacheName = "testUTF8StringAndBytes";
285    
286            BlockDiskCacheAttributes cattr = new BlockDiskCacheAttributes();
287            cattr.setCacheName( cacheName );
288            cattr.setMaxKeySize( 100 );
289            cattr.setBlockSizeBytes( 500 );
290            cattr.setDiskPath( "target/test-sandbox/BlockDiskCacheUnitTest" );
291            BlockDiskCache<String, X> diskCache = new BlockDiskCache<String, X>( cattr );
292    
293            // DO WORK
294            diskCache.update( new CacheElement<String, X>( cacheName, "x", before ) );
295    
296            // VERIFY
297            assertNotNull( diskCache.get( "x" ) );
298            Thread.sleep( 1000 );
299            ICacheElement<String, X> afterElement = diskCache.get( "x" );
300            System.out.println( "afterElement = " + afterElement );
301            X after = ( afterElement.getVal() );
302    
303            assertNotNull( after );
304            assertEquals( "wrong string after retrieval", string, after.string );
305            assertEquals( "wrong bytes after retrieval", string, new String( after.bytes, UTF8 ) );
306    
307        }
308    
309        /** Holder for a string and byte array. */
310        static class X
311            implements Serializable
312        {
313            /** ignore */
314            private static final long serialVersionUID = 1L;
315    
316            /** Test string */
317            String string;
318    
319            /*** test byte array. */
320            byte[] bytes;
321        }
322    }