From 416fa21047df6f67140cbfb29f45a4e87bab58ab Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Wed, 4 Jul 2018 17:09:40 +0200 Subject: [PATCH] #198: Adds prefab values for nio buffer --- .../prefabvalues/JavaApiPrefabValues.java | 21 ++++++++++++++ .../extended_contract/JavaApiClassesTest.java | 29 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java b/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java index 3b6b40643..d187abfc5 100644 --- a/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java +++ b/src/main/java/nl/jqno/equalsverifier/internal/prefabvalues/JavaApiPrefabValues.java @@ -7,6 +7,7 @@ import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; +import java.nio.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; @@ -65,6 +66,7 @@ private void addJavaClasses() { addMaps(); addSets(); addQueues(); + addNioBuffers(); addAwtClasses(); addJava8ApiClasses(); addJavaFxClasses(); @@ -281,6 +283,25 @@ public Tuple createValues(TypeTag tag, PrefabValues pf, LinkedHashSe addValues(SynchronousQueue.class, new SynchronousQueue<>(), new SynchronousQueue<>(), new SynchronousQueue<>()); } + private void addNioBuffers() { + addValues(Buffer.class, + ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 }), ByteBuffer.wrap(new byte[] { 0 })); + addValues(ByteBuffer.class, + ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 }), ByteBuffer.wrap(new byte[] { 0 })); + addValues(CharBuffer.class, + CharBuffer.wrap("a"), CharBuffer.wrap("b"), CharBuffer.wrap("a")); + addValues(DoubleBuffer.class, + DoubleBuffer.wrap(new double[] { 0.0 }), DoubleBuffer.wrap(new double[] { 1.0 }), DoubleBuffer.wrap(new double[] { 0.0 })); + addValues(FloatBuffer.class, + FloatBuffer.wrap(new float[] { 0.0f }), FloatBuffer.wrap(new float[] { 1.0f }), FloatBuffer.wrap(new float[] { 0.0f })); + addValues(IntBuffer.class, + IntBuffer.wrap(new int[] { 0 }), IntBuffer.wrap(new int[] { 1 }), IntBuffer.wrap(new int[] { 0 })); + addValues(LongBuffer.class, + LongBuffer.wrap(new long[] { 0 }), LongBuffer.wrap(new long[] { 1 }), LongBuffer.wrap(new long[] { 0 })); + addValues(ShortBuffer.class, + ShortBuffer.wrap(new short[] { 0 }), ShortBuffer.wrap(new short[] { 1 }), ShortBuffer.wrap(new short[] { 0 })); + } + private void addAwtClasses() { prefabValues.addLazyFactory("java.awt.Color", new ReflectiveLazyConstantFactory<>("java.awt.Color", "RED", "BLACK")); prefabValues.addLazyFactory("java.awt.color.ColorSpace", new ReflectiveLazyAwtFactory<>("java.awt.color.ColorSpace")); diff --git a/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java b/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java index 1d313eb10..7e42fb8e7 100644 --- a/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java +++ b/src/test/java/nl/jqno/equalsverifier/integration/extended_contract/JavaApiClassesTest.java @@ -7,6 +7,7 @@ import java.io.File; import java.net.InetAddress; +import java.nio.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.*; @@ -46,6 +47,12 @@ public void succeed_whenClassContainsAMap() { .verify(); } + @Test + public void succeed_whenClassContainsANioBuffer() { + EqualsVerifier.forClass(NioBufferContainer.class) + .verify(); + } + @Test public void succeed_whenClassContainsACommonJavaApiType() { EqualsVerifier.forClass(CommonClassesContainer.class) @@ -232,6 +239,28 @@ protected void callAbstractMethodsOnInterface() { } } + @SuppressWarnings("unused") // because of the use of defaultEquals and defaultHashCode + static final class NioBufferContainer { + private final Buffer buffer; + private final ByteBuffer byteBuffer; + private final CharBuffer charBuffer; + private final DoubleBuffer doubleBuffer; + private final FloatBuffer floatBuffer; + private final IntBuffer intBuffer; + private final LongBuffer longBuffer; + private final ShortBuffer shortBuffer; + + // CHECKSTYLE: ignore ParameterNumber for 1 line. + public NioBufferContainer(Buffer buffer, ByteBuffer byteBuffer, CharBuffer charBuffer, DoubleBuffer doubleBuffer, + FloatBuffer floatBuffer, IntBuffer intBuffer, LongBuffer longBuffer, ShortBuffer shortBuffer) { + this.buffer = buffer; this.byteBuffer = byteBuffer; this.charBuffer = charBuffer; this.doubleBuffer = doubleBuffer; + this.floatBuffer = floatBuffer; this.intBuffer = intBuffer; this.longBuffer = longBuffer; this.shortBuffer = shortBuffer; + } + + @Override public boolean equals(Object obj) { return defaultEquals(this, obj); } + @Override public int hashCode() { return defaultHashCode(this); } + } + @SuppressWarnings("unused") // because of the use of defaultEquals and defaultHashCode static final class CommonClassesContainer { private final String string;