This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git
The following commit(s) were added to refs/heads/master by this push:
new c8cca217 Further test cleanup
c8cca217 is described below
commit c8cca2172ff888f269f1aa8f0f7056518b4fca62
Author: Thomas Vandahl <[email protected]>
AuthorDate: Sun Oct 25 19:49:11 2026 +0100
Further test cleanup
---
.../utils/serialization/CompressingSerializer.java | 100 +++++++++-
.../commons/jcs4/utils/zip/CompressionUtil.java | 202 ---------------------
.../lru/LHMLRUMemoryCacheConcurrentUnitTest.java | 31 +---
.../lru/LRUMemoryCacheConcurrentUnitTest.java | 28 +--
.../CompressingSerializerUnitTest.java | 46 +++++
.../jcs4/utils/zip/CompressionUtilUnitTest.java | 92 ----------
6 files changed, 154 insertions(+), 345 deletions(-)
diff --git
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializer.java
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializer.java
index f3866552..eb3f910e 100644
---
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializer.java
+++
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializer.java
@@ -1,5 +1,7 @@
package org.apache.commons.jcs4.utils.serialization;
+import java.io.ByteArrayOutputStream;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -20,9 +22,11 @@ package org.apache.commons.jcs4.utils.serialization;
*/
import java.io.IOException;
+import java.util.zip.DataFormatException;
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
import org.apache.commons.jcs4.engine.behavior.IElementSerializer;
-import org.apache.commons.jcs4.utils.zip.CompressionUtil;
/**
* Performs default serialization and de-serialization. It gzips the value.
@@ -70,7 +74,7 @@ public class CompressingSerializer extends StandardSerializer
return null;
}
- final byte[] decompressedByteArray =
CompressionUtil.decompressByteArray( data );
+ final byte[] decompressedByteArray = decompressByteArray( data, 1024 );
return serializer.deSerialize(decompressedByteArray, loader);
}
@@ -86,6 +90,96 @@ public class CompressingSerializer extends StandardSerializer
throws IOException
{
final byte[] uncompressed = serializer.serialize(obj);
- return CompressionUtil.compressByteArray( uncompressed );
+ return compressByteArray( uncompressed, 1024 );
+ }
+
+ /**
+ * Compress the byte array passed
+ *
+ * @param input byte array
+ * @param bufferLength buffer length
+ * @return compressed byte array
+ * @throws IOException thrown if we can't close the output stream
+ */
+ protected static byte[] compressByteArray( final byte[] input, final int
bufferLength )
+ throws IOException
+ {
+ // Compressor with highest level of compression
+ final 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.
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream(
input.length ))
+ {
+ // Compress the data
+ final byte[] buf = new byte[bufferLength];
+ while ( !compressor.finished() )
+ {
+ final int count = compressor.deflate( buf );
+ bos.write( buf, 0, count );
+ }
+
+ // Get the compressed data
+ return bos.toByteArray();
+ }
+ finally
+ {
+ // JCS-136 ( Details here :
http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
+ compressor.end();
+ }
+ }
+
+ /**
+ * Decompress the byte array passed
+ *
+ * @param input compressed byte array webservice response
+ * @param bufferLength buffer length
+ * @return uncompressed byte array
+ * @throws IOException thrown if we can't close the output stream
+ */
+ public static byte[] decompressByteArray( final byte[] input, final int
bufferLength )
+ throws IOException
+ {
+ if ( input == null )
+ {
+ 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
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream(
input.length ))
+ {
+ // Decompress the data
+ final byte[] buf = new byte[bufferLength];
+
+ try
+ {
+ while ( !decompressor.finished() )
+ {
+ final int count = decompressor.inflate( buf );
+ baos.write( buf, 0, count );
+ }
+ }
+ catch ( final DataFormatException ex )
+ {
+ throw new IOException("Problem decompressing.", ex);
+ }
+
+ return baos.toByteArray();
+ }
+ finally
+ {
+ decompressor.end();
+ }
}
}
diff --git
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/zip/CompressionUtil.java
b/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/zip/CompressionUtil.java
deleted file mode 100644
index 7c7a8986..00000000
---
a/commons-jcs4-core/src/main/java/org/apache/commons/jcs4/utils/zip/CompressionUtil.java
+++ /dev/null
@@ -1,202 +0,0 @@
-package org.apache.commons.jcs4.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
- *
- * https://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.jcs4.log.Log;
-
-/** Compress / Decompress. */
-public final class CompressionUtil
-{
- /** The logger */
- private static final Log log = Log.getLog( CompressionUtil.class );
-
- /**
- * Compress the byte array passed
- *
- * @param input byte array
- * @return compressed byte array
- * @throws IOException thrown if we can't close the output stream
- */
- public static byte[] compressByteArray( final byte[] input )
- throws IOException
- {
- return compressByteArray( input, 1024 );
- }
-
- /**
- * Compress the byte array passed
- *
- * @param input byte array
- * @param bufferLength buffer length
- * @return compressed byte array
- * @throws IOException thrown if we can't close the output stream
- */
- public static byte[] compressByteArray( final byte[] input, final int
bufferLength )
- throws IOException
- {
- // Compressor with highest level of compression
- final 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.
- final ByteArrayOutputStream bos = new ByteArrayOutputStream(
input.length );
-
- // Compress the data
- final byte[] buf = new byte[bufferLength];
- while ( !compressor.finished() )
- {
- final int count = compressor.deflate( buf );
- bos.write( buf, 0, count );
- }
-
- // JCS-136 ( Details here :
http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
- compressor.end();
- bos.close();
-
- // Get the compressed data
- return bos.toByteArray();
-
- }
-
- /**
- * Decompress the byte array passed using a default buffer length of 1024.
- *
- * @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
- *
- * @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 ( input == null )
- {
- 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() )
- {
- final int count = decompressor.inflate( buf );
- baos.write( buf, 0, count );
- }
- }
- catch ( final DataFormatException ex )
- {
- log.error( "Problem decompressing.", ex );
- }
-
- decompressor.end();
-
- try
- {
- baos.close();
- }
- catch ( final IOException ex )
- {
- log.error( "Problem closing stream.", ex );
- }
-
- return baos.toByteArray();
- }
-
- /**
- * decompress a gzip byte array, using a default buffer length of 1024
- *
- * @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( final byte[]
compressedByteArray )
- throws IOException
- {
- return decompressGzipByteArray( compressedByteArray, 1024 );
- }
-
- /**
- * decompress a gzip byte array, using a default buffer length of 1024
- *
- * @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( final byte[]
compressedByteArray, final int bufferlength )
- throws IOException
- {
- final ByteArrayOutputStream uncompressedStream = new
ByteArrayOutputStream();
-
- final GZIPInputStream compressedStream = new GZIPInputStream( new
ByteArrayInputStream( compressedByteArray ) );
-
- final byte[] buffer = new byte[bufferlength];
-
- int index = -1;
-
- while ( ( index = compressedStream.read( buffer ) ) != -1 )
- {
- uncompressedStream.write( buffer, 0, index );
- }
-
- return uncompressedStream.toByteArray();
- }
-
- /**
- * no instances.
- */
- private CompressionUtil()
- {
- // NO OP
- }
-}
diff --git
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LHMLRUMemoryCacheConcurrentUnitTest.java
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LHMLRUMemoryCacheConcurrentUnitTest.java
index 500ea24c..49c5b3eb 100644
---
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LHMLRUMemoryCacheConcurrentUnitTest.java
+++
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LHMLRUMemoryCacheConcurrentUnitTest.java
@@ -31,8 +31,8 @@ import org.apache.commons.jcs4.engine.CacheElement;
import org.apache.commons.jcs4.engine.behavior.ICacheElement;
import org.apache.commons.jcs4.engine.control.CompositeCache;
import org.apache.commons.jcs4.engine.control.CompositeCacheManager;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Test which exercises the LRUMemory cache. This one uses three different
@@ -40,7 +40,6 @@ import org.junit.jupiter.api.Test;
*/
class LHMLRUMemoryCacheConcurrentUnitTest
{
-
// Number of items to cache, twice the configured maxObjects for the
memory cache regions.
private static final int items = 200;
@@ -50,14 +49,16 @@ class LHMLRUMemoryCacheConcurrentUnitTest
* @param region Name of the region to access
* @throws Exception If an error occurs
*/
- public static void runTestForRegion( final String region )
+ @ParameterizedTest
+ @ValueSource(strings = {"indexedRegion1"})
+ public void testLHMLRUMemoryCache( final String region )
throws Exception
{
final CompositeCacheManager cacheMgr =
CompositeCacheManager.getUnconfiguredInstance();
cacheMgr.configure( "/TestLHMLRUCache.ccf" );
final CompositeCache<String, String> cache = cacheMgr.getCache( region
);
- final LRUMemoryCache<String, String> lru = new LRUMemoryCache<>();
+ final LHMLRUMemoryCache<String, String> lru = new
LHMLRUMemoryCache<>();
lru.initialize( cache );
// Add items to cache
@@ -112,24 +113,4 @@ class LHMLRUMemoryCacheConcurrentUnitTest
assertNull( lru.get( i + ":key" ), "Removed key should be null: "
+ i + ":key" );
}
}
-
- /**
- * Test setup
- */
- @BeforeEach
- void setUp()
- {
- // Any setup logic can go here, e.g., configuring caches
- // JCS.setConfigFilename("/TestLHMLRUCache.ccf");
- }
-
- /**
- * Run test for region 'indexedRegion1'
- */
- @Test
- void testLHMLRUMemoryCache()
- throws Exception
- {
- runTestForRegion( "indexedRegion1" );
- }
}
diff --git
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LRUMemoryCacheConcurrentUnitTest.java
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LRUMemoryCacheConcurrentUnitTest.java
index e82801d1..3d34280c 100644
---
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LRUMemoryCacheConcurrentUnitTest.java
+++
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/engine/memory/lru/LRUMemoryCacheConcurrentUnitTest.java
@@ -31,8 +31,8 @@ import org.apache.commons.jcs4.engine.CacheElement;
import org.apache.commons.jcs4.engine.behavior.ICacheElement;
import org.apache.commons.jcs4.engine.control.CompositeCache;
import org.apache.commons.jcs4.engine.control.CompositeCacheManager;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
/**
* Test which exercises the LRUMemory cache. This one uses three different
@@ -50,7 +50,9 @@ class LRUMemoryCacheConcurrentUnitTest
* @param region Name of the region to access
* @throws Exception If an error occurs
*/
- public static void runTestForRegion( final String region )
+ @ParameterizedTest
+ @ValueSource(strings = {"testRegion1"})
+ public void testLRUMemoryCache( final String region )
throws Exception
{
final CompositeCacheManager cacheMgr =
CompositeCacheManager.getUnconfiguredInstance();
@@ -112,24 +114,4 @@ class LRUMemoryCacheConcurrentUnitTest
assertNull( lru.get( i + ":key" ), "Removed key should be null: "
+ i + ":key" );
}
}
-
- /**
- * Test setup
- */
- @BeforeEach
- void setUp()
- {
- // Any setup logic can go here, e.g., configuring caches
- // JCS.setConfigFilename("/TestDiskCache.ccf");
- }
-
- /**
- * Run test for region 'testRegion1'
- */
- @Test
- void testLRUMemoryCache()
- throws Exception
- {
- runTestForRegion( "testRegion1" );
- }
}
diff --git
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializerUnitTest.java
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializerUnitTest.java
index 9de78c84..7a724bdf 100644
---
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializerUnitTest.java
+++
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/serialization/CompressingSerializerUnitTest.java
@@ -1,5 +1,9 @@
package org.apache.commons.jcs4.utils.serialization;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -21,6 +25,8 @@ package org.apache.commons.jcs4.utils.serialization;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.IOException;
+
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -64,4 +70,44 @@ class CompressingSerializerUnitTest
"Compressed should be smaller. compressed size = " +
compressed.length + "nonCompressed size = "
+ nonCompressed.length );
}
+
+ /**
+ * Test method for decompressByteArray.
+ *
+ * @throws IOException
+ */
+ @Test
+ public final void testCompressDecompressByteArray_success()
+ throws IOException
+ {
+ // SETUP
+ final String text = "This is some text to compress, not a lot, just a
bit ";
+
+ // DO WORK
+ final byte[] compressedText = CompressingSerializer.compressByteArray(
text.getBytes(), 1024 );
+ final byte[] output = CompressingSerializer.decompressByteArray(
compressedText, 1024 );
+
+ // VERIFY
+ final String result = new String( output );
+ assertNotNull( output, "decompressed output stream shouldn't have been
null " );
+ assertEquals( text, result );
+ }
+
+ /** Test method for decompressByteArray. */
+ @Test
+ public final void testDecompressByteArray_failure()
+ {
+ assertThrows(IllegalArgumentException.class, () ->
CompressingSerializer.decompressByteArray( null, 0 ),
+ "excepted an IllegalArgumentException");
+ }
+
+ /** Test method for decompressByteArray. */
+ @Test
+ public final void testDecompressByteArray_gibberish()
+ {
+ final String text = "This is some text to decompress, not a lot, just
a bit ";
+
+ assertThrows(IOException.class, () ->
CompressingSerializer.decompressByteArray( text.getBytes(), 0 ),
+ "excepted an IOException");
+ }
}
diff --git
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java
b/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java
deleted file mode 100644
index 120c5f2d..00000000
---
a/commons-jcs4-core/src/test/java/org/apache/commons/jcs4/utils/zip/CompressionUtilUnitTest.java
+++ /dev/null
@@ -1,92 +0,0 @@
-package org.apache.commons.jcs4.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
- *
- * https://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 static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.util.zip.GZIPOutputStream;
-
-import org.junit.jupiter.api.Test;
-
-/** Tests for the compression util */
-public class CompressionUtilUnitTest
-{
- /**
- * Test method for decompressByteArray.
- *
- * @throws IOException
- */
- @Test
- public final void testCompressDecompressByteArray_success()
- throws IOException
- {
- // SETUP
- final String text = "This is some text to compress, not a lot, just a
bit ";
-
- // DO WORK
- final byte[] compressedText = CompressionUtil.compressByteArray(
text.getBytes() );
- final byte[] output = CompressionUtil.decompressByteArray(
compressedText );
-
- // VERIFY
- final String result = new String( output );
- assertNotNull( output, "decompressed output stream shouldn't have been
null " );
- assertEquals( text, result );
- }
-
- /**
- * Test method for decompressByteArray.
- *
- * @throws IOException
- */
- @Test
- public final void testCompressDecompressGzipByteArray_success()
- throws IOException
- {
- // SETUP
- final String text = " This is some text to compress, not a lot, just a
bit ";
-
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- final GZIPOutputStream os = new GZIPOutputStream( baos );
-
- os.write( text.getBytes() );
- os.flush();
- os.close();
-
- // DO WORK
- final byte[] output = CompressionUtil.decompressGzipByteArray(
baos.toByteArray() );
-
- // VERIFY
- final String result = new String( output );
- assertNotNull( output, "decompressed output stream shouldn't have been
null " );
- assertEquals( text, result );
- }
-
- /** Test method for decompressByteArray. */
- @Test
- public final void testDecompressByteArray_failure()
- {
- assertThrows(IllegalArgumentException.class, () ->
CompressionUtil.decompressByteArray( null ),
- "excepted an IllegalArgumentException");
- }
-}