You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/02/25 17:33:22 UTC

[isis] branch master updated: ISIS-1841 Internal API: add tests for compression and base64 encoding

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 8d9afe8  ISIS-1841 Internal API: add tests for compression and base64 encoding
8d9afe8 is described below

commit 8d9afe85cb58d6a3f76f78624dd1bfe4488c4c68
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sun Feb 25 18:33:20 2018 +0100

    ISIS-1841 Internal API: add tests for compression and base64 encoding
---
 .../isis/applib/internal/base/BytesTest.java       | 125 +++++++++++++++++++++
 .../isis/applib/internal/base/StringsTest.java     |  62 ++++++++++
 2 files changed, 187 insertions(+)

diff --git a/core/applib/src/test/java/org/apache/isis/applib/internal/base/BytesTest.java b/core/applib/src/test/java/org/apache/isis/applib/internal/base/BytesTest.java
new file mode 100644
index 0000000..b16163d
--- /dev/null
+++ b/core/applib/src/test/java/org/apache/isis/applib/internal/base/BytesTest.java
@@ -0,0 +1,125 @@
+/*
+ *  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.
+ */
+
+package org.apache.isis.applib.internal.base;
+
+import static org.hamcrest.Matchers.lessThan;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class BytesTest {
+
+	final int n = 256;
+	private final byte[] allBytes = new byte[n];
+	
+	private final byte[] testimonal = 
+			_Strings.toBytes(
+					"https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html#basic?"+
+					"0-theme-entityPageContainer-entity-rows-2-rowContents-1-col-tabGroups-1-panel-"
+					+ "tabPanel-rows-1-rowContents-1-col-fieldSets-1-memberGroup-properties-1-property-"
+					+ "scalarTypeContainer-scalarIfRegular-associatedActionLinksBelow-additionalLinkList-"
+					+ "additionalLinkItem-0-additionalLink", 
+					StandardCharsets.UTF_8);
+	
+	@Before
+	public void before() {
+		for(int i=0; i<n; ++i) {
+			allBytes[i] = (byte)i;
+		}
+	}
+	
+	// -- COMPRESSION
+	
+	@Test
+	public void compressIdentityWithNull() throws Exception {
+		Assert.assertNull(_Bytes.decompress(_Bytes.compress(null)));
+	}
+	
+	@Test
+	public void compressIdentityWithByteRange() throws Exception {
+		Assert.assertArrayEquals(allBytes,
+				_Bytes.decompress(_Bytes.compress(allBytes)));
+	}
+	
+	@Test
+	public void compressIdentityWithTestimonial() throws Exception {
+		Assert.assertArrayEquals(testimonal,
+				_Bytes.decompress(_Bytes.compress(testimonal)));
+	}
+	
+	@Test
+	public void compressionRatio() throws Exception {
+		// lower is better
+		final double compressionRatio = (double)_Bytes.compress(testimonal).length / testimonal.length;
+		Assert.assertThat(compressionRatio, lessThan(0.7));
+	}
+	
+	// -- BASE-64
+	
+	@Test
+	public void base64IdentityWithNull() throws Exception {
+		Assert.assertNull(_Bytes.decodeBase64(
+				Base64.getUrlDecoder(), 
+				_Bytes.encodeToBase64(Base64.getUrlEncoder(), null)));
+	}
+	
+	@Test
+	public void base64IdentityWithByteRange() throws Exception {
+		Assert.assertArrayEquals(allBytes,
+				_Bytes.decodeBase64(
+						Base64.getUrlDecoder(), 
+						_Bytes.encodeToBase64(Base64.getUrlEncoder(), allBytes)));
+	}
+	
+	@Test
+	public void base64IdentityWithTestimonial() throws Exception {
+		Assert.assertArrayEquals(testimonal,
+				_Bytes.decodeBase64(
+						Base64.getUrlDecoder(), 
+						_Bytes.encodeToBase64(Base64.getUrlEncoder(), testimonal)));
+	}
+	
+	// -- OPERATOR COMPOSITION
+	
+	@Test
+	public void composedOperatorWithNull() throws Exception {
+		Assert.assertNull(_Bytes.asCompressedUrlBase64.apply(null));
+		Assert.assertNull(_Bytes.asDecompressedUrlBase64.apply(null));
+	}
+	
+	@Test
+	public void composedIdentityWithByteRange() throws Exception {
+		Assert.assertArrayEquals(allBytes,
+				_Bytes.asDecompressedUrlBase64.apply(
+						_Bytes.asCompressedUrlBase64.apply(allBytes)));
+	}
+	
+	@Test
+	public void composedIdentityWithTestimonial() throws Exception {
+		Assert.assertArrayEquals(testimonal,
+				_Bytes.asDecompressedUrlBase64.apply(
+						_Bytes.asCompressedUrlBase64.apply(testimonal)));
+	}
+	
+}
diff --git a/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java b/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java
index 8d8c963..f9cf466 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java
+++ b/core/applib/src/test/java/org/apache/isis/applib/internal/base/StringsTest.java
@@ -22,8 +22,10 @@ package org.apache.isis.applib.internal.base;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 
+import java.nio.charset.StandardCharsets;
 import java.util.stream.Collectors;
 
+import org.apache.isis.applib.internal._Constants;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -169,6 +171,66 @@ public class StringsTest {
 				is("|12|aBc"));
 	}
 	
+	// -- TO BYTE CONVERSION
+	
+	@Test
+	public void toByteConvertWithNull() throws Exception {
+		Assert.assertThat(
+				_Strings.toBytes(null, StandardCharsets.UTF_8), 
+				nullValue());
+	}
+	
+	@Test
+	public void toByteConvertWithEmpty() throws Exception {
+		Assert.assertArrayEquals(
+				_Constants.emptyBytes,
+				_Strings.toBytes("", StandardCharsets.UTF_8));
+	}
+	
+	@Test
+	public void toByteConvert() throws Exception {
+		Assert.assertArrayEquals(
+				new byte[] {48,49,50,51},
+				_Strings.toBytes("0123", StandardCharsets.UTF_8));
+	}
+	
+	// -- FROM BYTE CONVERSION
+	
+	@Test
+	public void fromByteConvertWithNull() throws Exception {
+		Assert.assertThat(
+				_Strings.ofBytes(null, StandardCharsets.UTF_8), 
+				nullValue());
+	}
+	
+	@Test
+	public void fromByteConvertWithEmpty() throws Exception {
+		Assert.assertThat(
+				_Strings.ofBytes(_Constants.emptyBytes, StandardCharsets.UTF_8), 
+				is(""));
+	}
+	
+	@Test
+	public void fromByteConvert() throws Exception {
+		Assert.assertThat(
+				_Strings.ofBytes(new byte[] {48,49,50,51}, StandardCharsets.UTF_8), 
+				is("0123"));
+	}
+	
+	// -- BYTE MANIPULATION
+	
+	@Test
+	public void convertIdentity() throws Exception {
+		
+		Assert.assertThat(
+				_Strings.convert(null, _Bytes.operator(), StandardCharsets.UTF_8), 
+				nullValue());
+		
+		Assert.assertThat(
+				_Strings.convert("0123", _Bytes.operator(), StandardCharsets.UTF_8), 
+				is("0123"));
+	}
+	
 	// -- OPERATOR COMPOSITION
 	
 	@Test

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.