Author: asmuts Date: Thu Jun 11 15:29:18 2009 New Revision: 783815 URL: http://svn.apache.org/viewvc?rev=783815&view=rev Log: Some cleanup. Also, adding a compressing serializer.
Added: jakarta/jcs/trunk/src/java/org/apache/jcs/utils/serialization/CompressingSerializer.java jakarta/jcs/trunk/src/java/org/apache/jcs/utils/zip/ jakarta/jcs/trunk/src/java/org/apache/jcs/utils/zip/CompressionUtil.java jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/CompressingSerializerUnitTest.java jakarta/jcs/trunk/src/test/org/apache/jcs/utils/zip/ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/zip/CompressionUtilUnitTest.java Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCache.java jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractDoulbeLinkedListMemoryCache.java jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractMemoryCache.java jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/fifo/FIFOMemoryCache.java jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/mru/MRUMemoryCache.java jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/util/MemoryElementDescriptor.java Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCache.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCache.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCache.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/control/CompositeCache.java Thu Jun 11 15:29:18 2009 @@ -77,6 +77,7 @@ * EventQueue for handling element events. 1 should be enough for all the regions. Else should * create as needed per region. */ + // TODO fix for multi-instance JCS, have the manager pass this in. public static IElementEventQueue elementEventQ = new ElementEventQueue( "AllRegionQueue" ); /** Auxiliary caches. */ Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractDoulbeLinkedListMemoryCache.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractDoulbeLinkedListMemoryCache.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractDoulbeLinkedListMemoryCache.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractDoulbeLinkedListMemoryCache.java Thu Jun 11 15:29:18 2009 @@ -127,7 +127,7 @@ int size = map.size(); // If the element limit is reached, we need to spool - if ( size <= this.cattr.getMaxObjects() ) + if ( size <= this.cacheAttributes.getMaxObjects() ) { return; } @@ -143,7 +143,7 @@ if ( log.isDebugEnabled() ) { log.debug( "About to spool to disk cache, map size: " + size + ", max objects: " - + this.cattr.getMaxObjects() + ", items to spool: " + chunkSizeCorrected ); + + this.cacheAttributes.getMaxObjects() + ", items to spool: " + chunkSizeCorrected ); } // The spool will put them in a disk event queue, so there is no @@ -741,9 +741,6 @@ IStatElement[] ses = (IStatElement[]) elems.toArray( new StatElement[0] ); stats.setStatElements( ses ); - // int rate = ((hitCnt + missCnt) * 100) / (hitCnt * 100) * 100; - // buf.append("\n Hit Rate = " + rate + " %" ); - return stats; } } Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractMemoryCache.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractMemoryCache.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractMemoryCache.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/AbstractMemoryCache.java Thu Jun 11 15:29:18 2009 @@ -45,7 +45,7 @@ import EDU.oswego.cs.dl.util.concurrent.ThreadFactory; /** - * Some common code for the LRU and MRU caches. + * This base includes some common code for memory caches. * <p> * This keeps a static reference to a memory shrinker clock daemon. If this region is configured to * use the shrinker, the clock daemon will be setup to run the shrinker on this region. @@ -56,20 +56,20 @@ /** Don't change. */ private static final long serialVersionUID = -4494626991630099575L; - /** log instance */ + /** Log instance */ private final static Log log = LogFactory.getLog( AbstractMemoryCache.class ); /** The region name. This defines a namespace of sorts. */ protected String cacheName; - /** Map where items are stored by key */ + /** Map where items are stored by key. This is created by the concrete child class. */ protected Map map; - /** Region Elemental Attributes, used as a default. */ - public IElementAttributes attr; + /** Region Elemental Attributes, used as a default and copied for each item. */ + public IElementAttributes elementAttributes; - /** Cache Attributes */ - public ICompositeCacheAttributes cattr; + /** Cache Attributes. Regions settings. */ + public ICompositeCacheAttributes cacheAttributes; /** The cache region this store is associated with */ protected CompositeCache cache; @@ -81,6 +81,7 @@ protected int chunkSize; /** The background memory shrinker, one for all regions. */ + // TODO fix for multi-instance JCS private static ClockDaemon shrinkerDaemon; /** @@ -91,21 +92,21 @@ public synchronized void initialize( CompositeCache hub ) { this.cacheName = hub.getCacheName(); - this.cattr = hub.getCacheAttributes(); + this.cacheAttributes = hub.getCacheAttributes(); this.cache = hub; map = createMap(); - chunkSize = cattr.getSpoolChunkSize(); + chunkSize = cacheAttributes.getSpoolChunkSize(); status = CacheConstants.STATUS_ALIVE; - if ( cattr.getUseMemoryShrinker() ) + if ( cacheAttributes.getUseMemoryShrinker() ) { if ( shrinkerDaemon == null ) { shrinkerDaemon = new ClockDaemon(); shrinkerDaemon.setThreadFactory( new MyThreadFactory() ); } - shrinkerDaemon.executePeriodically( cattr.getShrinkerIntervalSeconds() * 1000, new ShrinkerThread( this ), + shrinkerDaemon.executePeriodically( cacheAttributes.getShrinkerIntervalSeconds() * 1000, new ShrinkerThread( this ), false ); } } @@ -114,7 +115,7 @@ * Children must implement this method. A FIFO implementation may use a tree map. An LRU might * use a hashtable. The map returned should be threadsafe. * <p> - * @return Map + * @return a threadsafe Map */ public abstract Map createMap(); @@ -172,7 +173,8 @@ } /** - * Get an item from the cache without affecting its last access time or position. + * Get an item from the cache without affecting its last access time or position. Not all memory + * cache implementations can get quietly. * <p> * @param key Identifies item to find * @return Element matching key if found, or null @@ -274,13 +276,13 @@ } /** - * Returns the cache name. + * Returns the cache (aka "region") name. * <p> * @return The cacheName value */ public String getCacheName() { - return this.cattr.getCacheName(); + return this.cacheAttributes.getCacheName(); } /** @@ -312,7 +314,7 @@ */ public ICompositeCacheAttributes getCacheAttributes() { - return this.cattr; + return this.cacheAttributes; } /** @@ -322,7 +324,7 @@ */ public void setCacheAttributes( ICompositeCacheAttributes cattr ) { - this.cattr = cattr; + this.cacheAttributes = cattr; } /** @@ -335,7 +337,6 @@ return this.cache; } - /** * @param groupName * @return group keys @@ -374,7 +375,7 @@ { Thread t = new Thread( runner ); String oldName = t.getName(); - t.setName( "JCS-AbstractMemoryCache-" + oldName ); + t.setName( "JCS-AbstractMemoryCache-" + oldName ); t.setDaemon( true ); t.setPriority( Thread.MIN_PRIORITY ); return t; Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/fifo/FIFOMemoryCache.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/fifo/FIFOMemoryCache.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/fifo/FIFOMemoryCache.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/fifo/FIFOMemoryCache.java Thu Jun 11 15:29:18 2009 @@ -26,7 +26,7 @@ import org.apache.jcs.engine.memory.util.MemoryElementDescriptor; /** - * The items are spooled in the order they are added. No adjustments to the list are make on get. + * The items are spooled in the order they are added. No adjustments to the list are made on get. */ public class FIFOMemoryCache extends AbstractDoulbeLinkedListMemoryCache Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java Thu Jun 11 15:29:18 2009 @@ -42,8 +42,7 @@ import org.apache.jcs.engine.stats.behavior.IStats; /** - * This is a test memory manager using the jdk1.4 LinkedHashMap. There may be some thread safety - * issues. + * This is a test memory manager using the jdk1.4 LinkedHashMap. */ public class LHMLRUMemoryCache extends AbstractMemoryCache Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/mru/MRUMemoryCache.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/mru/MRUMemoryCache.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/mru/MRUMemoryCache.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/mru/MRUMemoryCache.java Thu Jun 11 15:29:18 2009 @@ -38,7 +38,7 @@ /** * Adds the item to the front of the list. A put doesn't count as a usage. * <p> - * It's not clear if the put operation sould be different. Perhaps this should remove the oldest + * It's not clear if the put operation should be different. Perhaps this should remove the oldest * if full, and then put. * <p> * @param ce Modified: jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/util/MemoryElementDescriptor.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/util/MemoryElementDescriptor.java?rev=783815&r1=783814&r2=783815&view=diff ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/util/MemoryElementDescriptor.java (original) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/engine/memory/util/MemoryElementDescriptor.java Thu Jun 11 15:29:18 2009 @@ -35,7 +35,7 @@ public ICacheElement ce; /** - * Constructor for the MemoryElementDescriptor object + * Constructs a usable MemoryElementDescriptor. * <p> * @param ce */ Added: jakarta/jcs/trunk/src/java/org/apache/jcs/utils/serialization/CompressingSerializer.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/serialization/CompressingSerializer.java?rev=783815&view=auto ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/utils/serialization/CompressingSerializer.java (added) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/utils/serialization/CompressingSerializer.java Thu Jun 11 15:29:18 2009 @@ -0,0 +1,133 @@ +package org.apache.jcs.utils.serialization; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import org.apache.jcs.engine.behavior.IElementSerializer; +import org.apache.jcs.utils.zip.CompressionUtil; + +/** + * Performs default serialization and de-serialization. It gzips the value. + */ +public class CompressingSerializer + implements IElementSerializer +{ + /** + * Serializes an object using default serilaization. Compresses the byte array. + * <p> + * @param obj object + * @return byte[] + * @throws IOException on i/o problem + */ + public byte[] serialize( Serializable obj ) + throws IOException + { + byte[] uncompressed = serializeObject( obj ); + byte[] compressed = CompressionUtil.compressByteArray( uncompressed ); + return compressed; + } + + /** + * Does the basic serialization. + * <p> + * @param obj object + * @return byte[] + * @throws IOException on i/o problem + */ + protected byte[] serializeObject( Serializable obj ) + throws IOException + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream( baos ); + try + { + oos.writeObject( obj ); + } + finally + { + oos.close(); + } + byte[] uncompressed = baos.toByteArray(); + return uncompressed; + } + + /** + * Uses default de-serialization to turn a byte array into an object. Decompresses the value + * first. All exceptions are converted into IOExceptions. + * <p> + * @param data bytes of data + * @return Object + * @throws IOException on i/o problem + * @throws ClassNotFoundException if class is not found during deserialization + */ + public Object deSerialize( byte[] data ) + throws IOException, ClassNotFoundException + { + if ( data == null ) + { + return null; + } + byte[] decompressedByteArray = CompressionUtil.decompressByteArray( data ); + return deserializeObject( decompressedByteArray ); + } + + /** + * Does the standard deserialization. + * <p> + * @param decompressedByteArray array of decompressed bytes + * @return Object + * @throws IOException on i/o error + * @throws ClassNotFoundException if class is not found during deserialization + */ + protected Object deserializeObject( byte[] decompressedByteArray ) + throws IOException, ClassNotFoundException + { + ByteArrayInputStream bais = new ByteArrayInputStream( decompressedByteArray ); + BufferedInputStream bis = new BufferedInputStream( bais ); + ObjectInputStream ois = new ObjectInputStream( bis ); + + try + { + try + { + return ois.readObject(); + } + catch ( IOException e ) + { + throw e; + } + catch ( ClassNotFoundException e ) + { + throw e; + } + } + finally + { + ois.close(); + } + } +} Added: jakarta/jcs/trunk/src/java/org/apache/jcs/utils/zip/CompressionUtil.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/java/org/apache/jcs/utils/zip/CompressionUtil.java?rev=783815&view=auto ============================================================================== --- jakarta/jcs/trunk/src/java/org/apache/jcs/utils/zip/CompressionUtil.java (added) +++ jakarta/jcs/trunk/src/java/org/apache/jcs/utils/zip/CompressionUtil.java Thu Jun 11 15:29:18 2009 @@ -0,0 +1,199 @@ +package org.apache.jcs.utils.zip; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.DataFormatException; +import java.util.zip.Deflater; +import java.util.zip.GZIPInputStream; +import java.util.zip.Inflater; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** Compress / Decompress. */ +public final class CompressionUtil +{ + /** The logger */ + private final static Log log = LogFactory.getLog( CompressionUtil.class ); + + /** + * no instances. + */ + private CompressionUtil() + { + // NO OP + } + + /** + * Decompress the byte array passed using a default buffer length of 1024. + * <p> + * @param input compressed byte array webservice response + * @return uncompressed byte array + */ + public static byte[] decompressByteArray( final byte[] input ) + { + return decompressByteArray( input, 1024 ); + } + + /** + * Decompress the byte array passed + * <p> + * @param input compressed byte array webservice response + * @param bufferLength buffer length + * @return uncompressed byte array + */ + public static byte[] decompressByteArray( final byte[] input, final int bufferLength ) + { + if ( null == input ) + { + throw new IllegalArgumentException( "Input was null" ); + } + + // Create the decompressor and give it the data to compress + final Inflater decompressor = new Inflater(); + + decompressor.setInput( input ); + + // Create an expandable byte array to hold the decompressed data + final ByteArrayOutputStream baos = new ByteArrayOutputStream( input.length ); + + // Decompress the data + final byte[] buf = new byte[bufferLength]; + + try + { + while ( !decompressor.finished() ) + { + int count = decompressor.inflate( buf ); + baos.write( buf, 0, count ); + } + } + catch ( DataFormatException ex ) + { + log.error( "Problem decompressing.", ex ); + } + + try + { + baos.close(); + } + catch ( IOException ex ) + { + log.error( "Problem closing stream.", ex ); + } + + return baos.toByteArray(); + } + + /** + * Compress the byte array passed + * <p> + * @param input byte array + * @return compressed byte array + * @exception IOException thrown if we can't close the output stream + */ + public static byte[] compressByteArray( byte[] input ) + throws IOException + { + return compressByteArray( input, 1024 ); + } + + /** + * Compress the byte array passed + * <p> + * @param input byte array + * @param bufferLength buffer length + * @return compressed byte array + * @exception IOException thrown if we can't close the output stream + */ + public static byte[] compressByteArray( byte[] input, int bufferLength ) + throws IOException + { + // Compressor with highest level of compression + Deflater compressor = new Deflater(); + compressor.setLevel( Deflater.BEST_COMPRESSION ); + + // Give the compressor the data to compress + compressor.setInput( input ); + compressor.finish(); + + // Create an expandable byte array to hold the compressed data. + // It is not necessary that the compressed data will be smaller than + // the uncompressed data. + ByteArrayOutputStream bos = new ByteArrayOutputStream( input.length ); + + // Compress the data + byte[] buf = new byte[bufferLength]; + while ( !compressor.finished() ) + { + int count = compressor.deflate( buf ); + bos.write( buf, 0, count ); + } + + bos.close(); + + // Get the compressed data + return bos.toByteArray(); + + } + + /** + * decompress a gzip byte array, using a default buffer length of 1024 + * <p> + * @param compressedByteArray gzip-compressed byte array + * @return decompressed byte array + * @throws IOException thrown if there was a failure to construct the GzipInputStream + */ + public static byte[] decompressGzipByteArray( byte[] compressedByteArray ) + throws IOException + { + return decompressGzipByteArray( compressedByteArray, 1024 ); + } + + /** + * decompress a gzip byte array, using a default buffer length of 1024 + * <p> + * @param compressedByteArray gzip-compressed byte array + * @param bufferlength size of the buffer in bytes + * @return decompressed byte array + * @throws IOException thrown if there was a failure to construct the GzipInputStream + */ + public static byte[] decompressGzipByteArray( byte[] compressedByteArray, int bufferlength ) + throws IOException + { + ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream(); + + GZIPInputStream compressedStream = new GZIPInputStream( new ByteArrayInputStream( compressedByteArray ) ); + + byte[] buffer = new byte[bufferlength]; + + int index = -1; + + while ( ( index = compressedStream.read( buffer ) ) != -1 ) + { + uncompressedStream.write( buffer, 0, index ); + } + + return uncompressedStream.toByteArray(); + } +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/CompressingSerializerUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/CompressingSerializerUnitTest.java?rev=783815&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/CompressingSerializerUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/serialization/CompressingSerializerUnitTest.java Thu Jun 11 15:29:18 2009 @@ -0,0 +1,119 @@ +package org.apache.jcs.utils.serialization; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; + +import junit.framework.TestCase; + +/** + * Tests the compressing serializer. + */ +public class CompressingSerializerUnitTest + extends TestCase +{ + /** + * Verify that we don't get any erorrs for null input. + * <p> + * @throws ClassNotFoundException + * @throws IOException + */ + public void testDeserialize_NullInput() + throws IOException, ClassNotFoundException + { + // SETUP + CompressingSerializer serializer = new CompressingSerializer(); + + // DO WORK + Object result = serializer.deSerialize( null ); + + // VERIFY + assertNull( "Should have nothing.", result ); + } + + /** + * Test simple back and forth with a string. + * <p> + * ))<=>(( + * <p> + * @throws Exception on error + */ + public void testSimpleBackAndForth() + throws Exception + { + // SETUP + CompressingSerializer serializer = new CompressingSerializer(); + + // DO WORK + String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdsfdsafdsafsa333 31231"; + String after = (String) serializer.deSerialize( serializer.serialize( before ) ); + + // VERIFY + assertEquals( "Before and after should be the same.", before, after ); + } + + /** + * Test serialization with a null object. Verify that we don't get an error. + * <p> + * @throws Exception on error + */ + public void testSerialize_NullInput() + throws Exception + { + // SETUP + CompressingSerializer serializer = new CompressingSerializer(); + + String before = null; + + // DO WORK + byte[] serialized = serializer.serialize( before ); + String after = (String) serializer.deSerialize( serialized ); + + // VERIFY + assertNull( "Should have nothing. after =" + after, after ); + } + + /** + * Verify that the compressed is smaller. + * <p> + * @throws Exception on error + */ + public void testSerialize_CompareCompressedAndUncompressed() + throws Exception + { + // SETUP + CompressingSerializer serializer = new CompressingSerializer(); + + // I hate for loops. + String before = "adsfdsafdsafdsafdsafdsafdsafdsagfdsafdsafdssaf dsaf sadf dsaf dsaf dsaf " + + "dsafdsa fdsaf dsaf dsafdsa dsaf dsaf dsaf dsaf dsafdsa76f dsa798f dsa6fdsa 087f " + + "gh 987dsahb dsahbuhbfnui nufdsa hbv87 f8vhdsgbnfv h8fdg8dfjvn8fdwgj fdsgjb9fdsjbv" + + "jvhjv hg98f-dsaghj j9fdsb gfsb 9fdshjbgb987fdsbfdwgh ujbhjbhb hbfdsgh fdshb " + + "Ofdsgyfesgyfdsafdsafsa333 31231"; + + // DO WORK + byte[] compressed = serializer.serialize( before ); + byte[] nonCompressed = serializer.serializeObject( before ); + + // VERIFY + assertTrue( "Compressed should be smaller. compressed size = " + compressed.length + "nonCompressed size = " + + nonCompressed.length, compressed.length < nonCompressed.length ); + } +} Added: jakarta/jcs/trunk/src/test/org/apache/jcs/utils/zip/CompressionUtilUnitTest.java URL: http://svn.apache.org/viewvc/jakarta/jcs/trunk/src/test/org/apache/jcs/utils/zip/CompressionUtilUnitTest.java?rev=783815&view=auto ============================================================================== --- jakarta/jcs/trunk/src/test/org/apache/jcs/utils/zip/CompressionUtilUnitTest.java (added) +++ jakarta/jcs/trunk/src/test/org/apache/jcs/utils/zip/CompressionUtilUnitTest.java Thu Jun 11 15:29:18 2009 @@ -0,0 +1,97 @@ +package org.apache.jcs.utils.zip; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.GZIPOutputStream; + +import junit.framework.TestCase; + +/** Unit tests for the compression util */ +public class CompressionUtilUnitTest + extends TestCase +{ + /** Test method for decompressByteArray. */ + public final void testDecompressByteArray_failure() + { + try + { + // DO WORK + CompressionUtil.decompressByteArray( null ); + + // VERIFY + fail( "excepted an IllegalArgumentException" ); + } + catch ( IllegalArgumentException exception ) + { + // expected + return; + } + } + + /** + * Test method for decompressByteArray. + * <p> + * @throws IOException + */ + public final void testCompressDecompressByteArray_success() + throws IOException + { + // SETUP + String text = "This is some text to compress, not a lot, just a bit "; + + // DO WORK + byte[] compressedText = CompressionUtil.compressByteArray( text.getBytes() ); + byte[] output = CompressionUtil.decompressByteArray( compressedText ); + + // VERIFY + String result = new String( output ); + assertNotNull( "decompressed output stream shouldn't have been null ", output ); + assertEquals( text, result ); + } + + /** + * Test method for decompressByteArray. + * <p> + * @throws IOException + */ + public final void testCompressDecompressGzipByteArray_success() + throws IOException + { + // SETUP + String text = " This is some text to compress, not a lot, just a bit "; + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream os = new GZIPOutputStream( baos ); + + os.write( text.getBytes() ); + os.flush(); + os.close(); + + // DO WORK + byte[] output = CompressionUtil.decompressGzipByteArray( baos.toByteArray() ); + + // VERIFY + String result = new String( output ); + assertNotNull( "decompressed output stream shouldn't have been null ", output ); + assertEquals( text, result ); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: jcs-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: jcs-dev-h...@jakarta.apache.org