From 4905fcfbb3a8d47fb02482ab79a5c2666535a377 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sebastian=20Ho=C3=9F?= Date: Fri, 12 Aug 2016 01:36:00 +0200 Subject: [PATCH] fix #6 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Sebastian Hoß --- README.asciidoc | 1 + pom.xml | 1 + storage-units-eclipselink/bnd.bnd | 10 ++ storage-units-eclipselink/pom.xml | 66 +++++++++ .../eclipselink/AbstractStorageUnitConverter.java | 60 +++++++++ .../eclipselink/BinaryStorageUnitConverter.java | 28 ++++ .../eclipselink/CommonStorageUnitConverter.java | 28 ++++ .../eclipselink/DecimalStorageUnitConverter.java | 28 ++++ .../storage_unit/eclipselink/package-info.java | 12 ++ .../eclipselink/StorageUnitConverterTest.java | 148 +++++++++++++++++++++ 10 files changed, 382 insertions(+) create mode 100644 storage-units-eclipselink/bnd.bnd create mode 100644 storage-units-eclipselink/pom.xml create mode 100644 storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/AbstractStorageUnitConverter.java create mode 100644 storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/BinaryStorageUnitConverter.java create mode 100644 storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/CommonStorageUnitConverter.java create mode 100644 storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/DecimalStorageUnitConverter.java create mode 100644 storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/package-info.java create mode 100644 storage-units-eclipselink/src/test/java/de/xn__ho_hia/storage_unit/eclipselink/StorageUnitConverterTest.java diff --git a/README.asciidoc b/README.asciidoc index 479ad95..5c77249 100755 --- a/README.asciidoc +++ b/README.asciidoc @@ -33,6 +33,7 @@ https://www.java.com[Java] library for storage-/byte-units. All units defined in * Lossless conversion between all units * Human readable text format, including custom formats * Compatible with any `java.lang.Number` +* Custom serializers for Jackson, MongoDB & EclipseLink ==== Available Units diff --git a/pom.xml b/pom.xml index 3d18632..b57bb4f 100755 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ storage-units + storage-units-eclipselink storage-units-jackson storage-units-mongodb diff --git a/storage-units-eclipselink/bnd.bnd b/storage-units-eclipselink/bnd.bnd new file mode 100644 index 0000000..837dc2a --- /dev/null +++ b/storage-units-eclipselink/bnd.bnd @@ -0,0 +1,10 @@ +Bundle-License: Creative Commons Zero +Bundle-Name: ${project.artifactId} +Bundle-Description: ${project.description} +Bundle-DocURL: ${project.url} +Bundle-Version: ${project.version} +Export-Package: de.xn__ho_hia.storage_unit.eclipselink +Import-Package: \ + de.xn__ho_hia.quality.null_analysis,\ + de.xn__ho_hia.quality.suppression,\ + * diff --git a/storage-units-eclipselink/pom.xml b/storage-units-eclipselink/pom.xml new file mode 100644 index 0000000..2cf8557 --- /dev/null +++ b/storage-units-eclipselink/pom.xml @@ -0,0 +1,66 @@ + + + + 4.0.0 + + + + + + + de.xn--ho-hia.storage_units + storage-units.java + 4.0.0-${revision} + + + + + + + storage-units-eclipselink + + + + + + Storage Units :: Serialization :: EclipseLink + + + + de.xn--ho-hia.quality + suppress-warnings + + + de.xn--ho-hia.quality + null-analysis + + + de.xn--ho-hia.storage_units + storage-units + ${project.version} + + + org.eclipse.persistence + eclipselink + 2.6.3 + + + org.mockito + mockito-core + test + + + org.jooq + jool + 0.9.11 + test + + + \ No newline at end of file diff --git a/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/AbstractStorageUnitConverter.java b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/AbstractStorageUnitConverter.java new file mode 100644 index 0000000..3466892 --- /dev/null +++ b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/AbstractStorageUnitConverter.java @@ -0,0 +1,60 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.eclipselink; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.persistence.internal.helper.DatabaseField; +import org.eclipse.persistence.mappings.DatabaseMapping; +import org.eclipse.persistence.mappings.DirectCollectionMapping; +import org.eclipse.persistence.mappings.converters.Converter; +import org.eclipse.persistence.sessions.Session; + +import de.xn__ho_hia.storage_unit.StorageUnit; + +/** + * Abstract implementation of a EclipseLink {@link Converter} for {@link StorageUnit StorageUnits}. + * + * @see EclipseLink + * documentation + */ +abstract class AbstractStorageUnitConverter implements Converter { + + private static final long serialVersionUID = 1696764872656233871L; + + @Override + public Object convertObjectValueToDataValue(final Object objectValue, final Session session) { + return ((StorageUnit) objectValue).inByte(); + } + + @Override + public Object convertDataValueToObjectValue(final Object dataValue, final Session session) { + final BigInteger value = new BigInteger(dataValue.toString()); + return convertToStorageUnit(value); + } + + protected abstract StorageUnit convertToStorageUnit(@NonNull BigInteger value); + + @Override + public boolean isMutable() { + return false; + } + + @Override + public void initialize(final DatabaseMapping mapping, final Session session) { + final DatabaseField field; + if (mapping instanceof DirectCollectionMapping) { + field = ((DirectCollectionMapping) mapping).getDirectField(); + } else { + field = mapping.getField(); + } + field.setSqlType(java.sql.Types.BIGINT); + } + +} diff --git a/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/BinaryStorageUnitConverter.java b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/BinaryStorageUnitConverter.java new file mode 100644 index 0000000..f619d9c --- /dev/null +++ b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/BinaryStorageUnitConverter.java @@ -0,0 +1,28 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.eclipselink; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * Converts database values to binary storage units. + */ +public final class BinaryStorageUnitConverter extends AbstractStorageUnitConverter { + + private static final long serialVersionUID = 7476654237380243377L; + + @Override + protected StorageUnit convertToStorageUnit(@NonNull final BigInteger value) { + return StorageUnits.binaryValueOf(value); + } + +} diff --git a/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/CommonStorageUnitConverter.java b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/CommonStorageUnitConverter.java new file mode 100644 index 0000000..c5327cf --- /dev/null +++ b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/CommonStorageUnitConverter.java @@ -0,0 +1,28 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.eclipselink; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * Converts database values to common storage units. + */ +public final class CommonStorageUnitConverter extends AbstractStorageUnitConverter { + + private static final long serialVersionUID = -3710617517690757565L; + + @Override + protected StorageUnit convertToStorageUnit(@NonNull final BigInteger value) { + return StorageUnits.commonValueOf(value); + } + +} diff --git a/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/DecimalStorageUnitConverter.java b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/DecimalStorageUnitConverter.java new file mode 100644 index 0000000..deb52c3 --- /dev/null +++ b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/DecimalStorageUnitConverter.java @@ -0,0 +1,28 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.eclipselink; + +import java.math.BigInteger; + +import org.eclipse.jdt.annotation.NonNull; + +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * Converts database values to decimal storage units. + */ +public final class DecimalStorageUnitConverter extends AbstractStorageUnitConverter { + + private static final long serialVersionUID = -6919304587763247036L; + + @Override + protected StorageUnit convertToStorageUnit(@NonNull final BigInteger value) { + return StorageUnits.decimalValueOf(value); + } + +} diff --git a/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/package-info.java b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/package-info.java new file mode 100644 index 0000000..278fb1c --- /dev/null +++ b/storage-units-eclipselink/src/main/java/de/xn__ho_hia/storage_unit/eclipselink/package-info.java @@ -0,0 +1,12 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +/** + * Serialization support for EclipseLink. + * + * @see EclipseLink Homepage + */ +package de.xn__ho_hia.storage_unit.eclipselink; diff --git a/storage-units-eclipselink/src/test/java/de/xn__ho_hia/storage_unit/eclipselink/StorageUnitConverterTest.java b/storage-units-eclipselink/src/test/java/de/xn__ho_hia/storage_unit/eclipselink/StorageUnitConverterTest.java new file mode 100644 index 0000000..fb39183 --- /dev/null +++ b/storage-units-eclipselink/src/test/java/de/xn__ho_hia/storage_unit/eclipselink/StorageUnitConverterTest.java @@ -0,0 +1,148 @@ +/* + * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units, + * including this file, may be copied, modified, propagated, or distributed except according to the terms contained + * in the LICENSE file. + */ +package de.xn__ho_hia.storage_unit.eclipselink; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Supplier; + +import org.eclipse.persistence.internal.helper.DatabaseField; +import org.eclipse.persistence.mappings.DatabaseMapping; +import org.eclipse.persistence.mappings.DirectCollectionMapping; +import org.jooq.lambda.tuple.Tuple2; +import org.junit.Assert; +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.mockito.Mockito; + +import de.xn__ho_hia.quality.suppression.CompilerWarnings; +import de.xn__ho_hia.storage_unit.CommonKilobyte; +import de.xn__ho_hia.storage_unit.Kibibyte; +import de.xn__ho_hia.storage_unit.Kilobyte; +import de.xn__ho_hia.storage_unit.StorageUnit; +import de.xn__ho_hia.storage_unit.StorageUnits; + +/** + * + */ +@RunWith(Theories.class) +@SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD }) +public class StorageUnitConverterTest { + + /** + * @return Suppliers to create a storage-unit converter. + */ + @DataPoints("supplier") + public static final List, Class>> suppliers() { + final List, Class>> suppliers = new ArrayList<>(); + suppliers.add(new Tuple2<>(BinaryStorageUnitConverter::new, Kibibyte.class)); + suppliers.add(new Tuple2<>(CommonStorageUnitConverter::new, CommonKilobyte.class)); + suppliers.add(new Tuple2<>(DecimalStorageUnitConverter::new, Kilobyte.class)); + return suppliers; + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + public void shouldNotBeMutable( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final AbstractStorageUnitConverter converter = supplier.v1.get(); + + // when + final boolean isMutable = converter.isMutable(); + + // then + Assert.assertFalse(isMutable); + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + public void shouldConvertUnitToBigInteger( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final StorageUnit unit = StorageUnits.kilobyte(1); + final AbstractStorageUnitConverter converter = supplier.v1.get(); + + // when + final Object dataValue = converter.convertObjectValueToDataValue(unit, null); + + // then + Assert.assertNotNull(dataValue); + Assert.assertEquals(new BigInteger("1000"), dataValue); + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + public void shouldConvertBigIntegerToUnit( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final BigInteger dataValue = new BigInteger("2000"); + final AbstractStorageUnitConverter converter = supplier.v1.get(); + + // when + final Object objectValue = converter.convertDataValueToObjectValue(dataValue, null); + + // then + Assert.assertNotNull(dataValue); + Assert.assertEquals(supplier.v2, objectValue.getClass()); + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + public void shouldInitializeAsBigInt( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final DatabaseMapping mapping = Mockito.mock(DatabaseMapping.class); + final DatabaseField field = new DatabaseField(); + BDDMockito.given(mapping.getField()).willReturn(field); + final AbstractStorageUnitConverter converter = supplier.v1.get(); + + // when + converter.initialize(mapping, null); + + // then + Assert.assertEquals(java.sql.Types.BIGINT, field.getSqlType()); + } + + /** + * @param supplier + * The supplier to use. + */ + @Theory + public void shouldInitializeAsBigIntWithDirectCollectionMapping( + @FromDataPoints("supplier") final Tuple2, Class> supplier) { + // given + final DirectCollectionMapping mapping = new DirectCollectionMapping(); + final DatabaseField field = new DatabaseField(); + mapping.setDirectField(field); + final AbstractStorageUnitConverter converter = supplier.v1.get(); + + // when + converter.initialize(mapping, null); + + // then + Assert.assertEquals(java.sql.Types.BIGINT, field.getSqlType()); + } + +} -- 2.11.4.GIT