diff --git a/.codestyle/checkstyle.xml b/.codestyle/checkstyle.xml index 4d26d2d..6c122e6 100644 --- a/.codestyle/checkstyle.xml +++ b/.codestyle/checkstyle.xml @@ -4,7 +4,7 @@ "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd"> - - - junit - junit - test - - - org.assertj - assertj-core - test - @@ -85,14 +57,13 @@ limitations under the License. - org.codehaus.mojo - findbugs-maven-plugin + com.github.spotbugs + spotbugs-maven-plugin org.apache.maven.plugins maven-pmd-plugin - org.apache.maven.plugins maven-checkstyle-plugin diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/Erlang.java b/encon-terms/src/main/java/io/appulse/encon/terms/Erlang.java index 00c6b1f..c3e841d 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/Erlang.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/Erlang.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,7 +55,7 @@ public final class Erlang { /** * Cached enpty (0-size string) {@link ErlangAtom} instance. */ - public static final ErlangAtom EMPTY_ATOM = ErlangAtom.cached(""); + public static final ErlangAtom EMPTY_ATOM = new ErlangAtom(""); /** * Creates new {@link ErlangAtom} instance from {@code boolean} (true/false value) . @@ -65,7 +65,9 @@ public final class Erlang { * @return {@link ErlangAtom} new instance */ public static ErlangAtom atom (boolean value) { - return ErlangAtom.cached(value); + return value + ? ErlangAtom.ATOM_TRUE + : ErlangAtom.ATOM_FALSE; } /** @@ -76,7 +78,7 @@ public static ErlangAtom atom (boolean value) { * @return {@link ErlangAtom} new instance */ public static ErlangAtom atom (@NonNull String value) { - return ErlangAtom.cached(value); + return new ErlangAtom(value); } /** diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/ErlangTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/ErlangTerm.java index e697902..c6c176a 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/ErlangTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/ErlangTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -95,7 +95,7 @@ public static T newInstance (@NonNull ByteBuf buffer) { case INTEGER: case SMALL_BIG: case LARGE_BIG: - return (T) ErlangInteger.cached(type, buffer); + return (T) new ErlangInteger(type, buffer); case FLOAT: case NEW_FLOAT: return (T) new ErlangFloat(type, buffer); @@ -108,7 +108,7 @@ public static T newInstance (@NonNull ByteBuf buffer) { return (T) new ErlangPort(type, buffer); case PID: case NEW_PID: - return (T) ErlangPid.cached(type, buffer); + return (T) new ErlangPid(type, buffer); case SMALL_TUPLE: case LARGE_TUPLE: return (T) new ErlangTuple(type, buffer); @@ -133,7 +133,7 @@ public static T newInstance (@NonNull ByteBuf buffer) { case SMALL_ATOM_UTF8: case ATOM: case SMALL_ATOM: - return (T) ErlangAtom.cached(type, buffer); + return (T) new ErlangAtom(type, buffer); default: val message = String.format("Unknown term type %s (%d)", type.name(), typeByte); throw new ErlangTermDecodeException(message); diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/TermType.java b/encon-terms/src/main/java/io/appulse/encon/terms/TermType.java index 85b4c73..4328896 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/TermType.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/TermType.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -487,7 +487,7 @@ public enum TermType { * *

* Encodes a map. The Arity field is an unsigned 4 byte integer in big-endian format that determines the - * number of key-value pairs in the map. Key and value pairs (Ki => Vi) are encoded in section Pairs in the + * number of key-value pairs in the map. Key and value pairs (Ki => Vi) are encoded in section Pairs in the * following order: K1, V1, K2, V2,..., Kn, Vn. *

* Duplicate keys are not allowed within the same map. @@ -661,7 +661,7 @@ public enum TermType { FUNCTION(117, ErlangFunction.class), /** - * This is the new encoding of internal funs: fun F/A and fun(Arg1,..) -> ... end. + * This is the new encoding of internal funs: fun F/A and fun(Arg1,..) -> ... end. *

* Structure: *

diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermDecodeException.java b/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermDecodeException.java index 4d94f03..b6c5af4 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermDecodeException.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermDecodeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermValidationException.java b/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermValidationException.java index 8bdf720..ae7aa2f 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermValidationException.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/exception/ErlangTermValidationException.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/exception/IllegalErlangTermTypeException.java b/encon-terms/src/main/java/io/appulse/encon/terms/exception/IllegalErlangTermTypeException.java index 981360d..6a48aff 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/exception/IllegalErlangTermTypeException.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/exception/IllegalErlangTermTypeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/BooleanTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/BooleanTerm.java index 33509e3..7eee854 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/BooleanTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/BooleanTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/CollectionTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/CollectionTerm.java index 001a946..de152c6 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/CollectionTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/CollectionTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/FloatTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/FloatTerm.java index ed93b61..2359c59 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/FloatTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/FloatTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/IntegerTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/IntegerTerm.java index 3e657d6..038a1ba 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/IntegerTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/IntegerTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/NodeContainerTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/NodeContainerTerm.java index c513c8d..36d42d4 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/NodeContainerTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/NodeContainerTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/NumberTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/NumberTerm.java index d4aa92c..8d9b234 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/NumberTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/NumberTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/StringTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/StringTerm.java index 5804696..8284376 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/StringTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/StringTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/term/ValueTerm.java b/encon-terms/src/main/java/io/appulse/encon/terms/term/ValueTerm.java index 4311554..f9d295c 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/term/ValueTerm.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/term/ValueTerm.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangAtom.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangAtom.java index a0b9232..0a19da4 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangAtom.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangAtom.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,25 +21,20 @@ import static io.appulse.encon.terms.TermType.SMALL_ATOM_UTF8; import static java.nio.charset.StandardCharsets.ISO_8859_1; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.Locale.ENGLISH; import static lombok.AccessLevel.PRIVATE; import java.nio.charset.Charset; -import java.util.Arrays; import io.appulse.encon.terms.ErlangTerm; import io.appulse.encon.terms.TermType; import io.appulse.encon.terms.exception.IllegalErlangTermTypeException; -import io.appulse.utils.cache.LruCache; import io.netty.buffer.ByteBuf; -import io.netty.util.ByteProcessor; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NonNull; import lombok.ToString; import lombok.experimental.FieldDefaults; -import lombok.experimental.NonFinal; /** * An atom is a literal, a constant with name. An atom is to be enclosed in @@ -51,83 +46,48 @@ * @author Artem Labazin */ @ToString +@SuppressWarnings("deprecation") @EqualsAndHashCode(callSuper = true, of = "bytes") @FieldDefaults(level = PRIVATE, makeFinal = true) public class ErlangAtom extends ErlangTerm { private static final long serialVersionUID = -2748345367418129439L; - private static final int MAX_ATOM_CODE_POINTS_LENGTH = 255; - - private static final int MAX_SMALL_ATOM_BYTES_LENGTH = 255; - - private static final LruCache CACHE = new LruCache<>(1000); + public static final ErlangAtom ATOM_TRUE = new ErlangAtom(true); - private static final ErlangAtom ATOM_TRUE = cached(Boolean.TRUE.toString().toLowerCase(ENGLISH)); + public static final ErlangAtom ATOM_FALSE = new ErlangAtom(false); - private static final ErlangAtom ATOM_FALSE = cached(Boolean.FALSE.toString().toLowerCase(ENGLISH)); - - public static ErlangAtom cached (boolean value) { - return value - ? ATOM_TRUE - : ATOM_FALSE; - } - - public static ErlangAtom cached (String value) { - Charset charset = UTF_8; - byte[] bytes = value.getBytes(charset); - int hashCode = Arrays.hashCode(bytes); - return CACHE.computeIfAbsent(hashCode, key -> new ErlangAtom(value, charset, bytes)); - } - - @SuppressWarnings("deprecation") - public static ErlangAtom cached (TermType type, ByteBuf buffer) { - ByteArrayHashCode byteProcessor = new ByteArrayHashCode(); - - int length = type == SMALL_ATOM || type == SMALL_ATOM_UTF8 - ? buffer.readUnsignedByte() - : buffer.readUnsignedShort(); - - buffer.forEachByte(buffer.readerIndex(), length, byteProcessor); + private static final int MAX_ATOM_CODE_POINTS_LENGTH = 255; - return CACHE.compute(byteProcessor.getHashCode(), (key, value) -> { - if (value == null) { - return new ErlangAtom(type, buffer, length); - } else { - buffer.skipBytes(length); - return value; - } - }); - } + private static final int MAX_SMALL_ATOM_BYTES_LENGTH = 255; - @NonFinal - String value; + @Getter(lazy = true, value = PRIVATE) + String value = createString(); byte[] bytes; transient Charset charset; /** - * Constructs Erlang's atom object with specific {@link String} value. + * Constructs Erlang's term object with specific {@link TermType} from {@link ByteBuf}. * - * @param value {@link String} atom's value + * @param type object's type + * + * @param buffer byte buffer */ - public ErlangAtom (@NonNull String value) { - super(); + public ErlangAtom (TermType type, ByteBuf buffer) { + super(type); - this.value = value.codePointCount(0, value.length()) <= MAX_ATOM_CODE_POINTS_LENGTH - ? value - // Throwing an exception would be better I think, but truncation - // seems to be the way it has been done in other parts of OTP... - : new String(value.codePoints().toArray(), 0, MAX_ATOM_CODE_POINTS_LENGTH); + int length = type == SMALL_ATOM || type == SMALL_ATOM_UTF8 + ? buffer.readUnsignedByte() + : buffer.readUnsignedShort(); - charset = UTF_8; - bytes = this.value.getBytes(charset); - if (bytes.length > MAX_SMALL_ATOM_BYTES_LENGTH) { - setType(ATOM_UTF8); - } else { - setType(SMALL_ATOM_UTF8); - } + charset = type == SMALL_ATOM_UTF8 || type == ATOM_UTF8 + ? UTF_8 + : ISO_8859_1; + + bytes = new byte[length]; + buffer.readBytes(bytes); } /** @@ -136,46 +96,31 @@ public ErlangAtom (@NonNull String value) { * @param value {@code boolean} atom's value */ public ErlangAtom (boolean value) { - super(SMALL_ATOM_UTF8); - charset = UTF_8; - this.value = Boolean.toString(value); - bytes = value - ? Boolean.TRUE.toString().getBytes(charset) - : Boolean.FALSE.toString().getBytes(charset); + this(Boolean.toString(value), UTF_8); } /** - * Constructs Erlang's term object with specific {@link TermType} from {@link ByteBuf}. - * - * @param type object's type - * - * @param buffer byte buffer + * Constructs Erlang's atom object with specific {@link String} value. * - * @param length amount of useful bytes + * @param value {@link String} atom's value */ - private ErlangAtom (TermType type, ByteBuf buffer, int length) { - super(type); - - charset = type == SMALL_ATOM_UTF8 || type == ATOM_UTF8 - ? UTF_8 - : ISO_8859_1; - - bytes = new byte[length]; - buffer.readBytes(bytes); + public ErlangAtom (String value) { + this(value, UTF_8); } - @SuppressWarnings("PMD.ArrayIsStoredDirectly") - private ErlangAtom (String value, Charset charset, byte[] bytes) { + public ErlangAtom (@NonNull String value, @NonNull Charset charset) { super(); - this.value = value.codePointCount(0, value.length()) <= MAX_ATOM_CODE_POINTS_LENGTH - ? value - // Throwing an exception would be better I think, but truncation - // seems to be the way it has been done in other parts of OTP... - : new String(value.codePoints().toArray(), 0, MAX_ATOM_CODE_POINTS_LENGTH); + if (value.codePointCount(0, value.length()) <= MAX_ATOM_CODE_POINTS_LENGTH) { + this.value.set(value); + } else { + // Throwing an exception would be better I think, but truncation + // seems to be the way it has been done in other parts of OTP... + this.value.set(new String(value.codePoints().toArray(), 0, MAX_ATOM_CODE_POINTS_LENGTH)); + } this.charset = charset; - this.bytes = bytes; + this.bytes = getValue().getBytes(charset); if (bytes.length > MAX_SMALL_ATOM_BYTES_LENGTH) { setType(ATOM_UTF8); } else { @@ -195,10 +140,7 @@ public boolean asBoolean (boolean defaultValue) { @Override public String asText (String defaultValue) { - if (value == null) { - value = new String(bytes, charset); - } - return value; + return getValue(); } @Override @@ -233,16 +175,7 @@ protected void serialize (ByteBuf buffer) { buffer.writeBytes(bytes); } - @Getter - @FieldDefaults(level = PRIVATE) - private static class ByteArrayHashCode implements ByteProcessor { - - int hashCode = 1; - - @Override - public boolean process (byte value) throws Exception { - hashCode = 31 * hashCode + value; - return true; - } + private String createString () { + return new String(bytes, charset); } } diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBinary.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBinary.java index b757439..bb42bd1 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBinary.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBinary.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBitString.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBitString.java index a3ce36c..d8552b3 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBitString.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangBitString.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangExternalFunction.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangExternalFunction.java index e353bd1..cce687e 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangExternalFunction.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangExternalFunction.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFloat.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFloat.java index 6f78e7b..b4b66ae 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFloat.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFloat.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFunction.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFunction.java index 8e566bb..edbca58 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFunction.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangFunction.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangInteger.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangInteger.java index 1158358..c4b2e39 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangInteger.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangInteger.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,12 +31,9 @@ import io.appulse.encon.terms.TermType; import io.appulse.encon.terms.exception.ErlangTermDecodeException; import io.appulse.encon.terms.exception.IllegalErlangTermTypeException; -import io.appulse.utils.cache.LruCache; import io.netty.buffer.ByteBuf; -import io.netty.util.ByteProcessor; import lombok.EqualsAndHashCode; -import lombok.Getter; import lombok.NonNull; import lombok.ToString; import lombok.experimental.FieldDefaults; @@ -64,8 +61,6 @@ public class ErlangInteger extends ErlangTerm { private static final int MAX_SMALL_BIG_BYTES_LENGTH = 255; - private static final LruCache CACHE = new LruCache<>(1000); - /** * Creates cached {@link ErlangInteger} value. * @@ -74,15 +69,7 @@ public class ErlangInteger extends ErlangTerm { * @return new or cached {@link ErlangInteger} object */ public static ErlangInteger cached (byte value) { - int hashCode = 31 + value; - - ErlangInteger result = CACHE.get(hashCode); - if (result != null) { - return result; - } - result = new ErlangInteger(value); - CACHE.put(hashCode, result); - return result; + return ErlangIntegerCache.CACHE[value + (-ErlangIntegerCache.LOW)]; } /** @@ -93,16 +80,10 @@ public static ErlangInteger cached (byte value) { * @return new or cached {@link ErlangInteger} object */ public static ErlangInteger cached (char value) { - int hashCode = 31 + (byte) (value >> 8); - hashCode = 31 * hashCode + (byte) value; - - ErlangInteger result = CACHE.get(hashCode); - if (result != null) { - return result; + if (value <= ErlangIntegerCache.HIGH) { + return ErlangIntegerCache.CACHE[value + (-ErlangIntegerCache.LOW)]; } - result = new ErlangInteger(value); - CACHE.put(hashCode, result); - return result; + return new ErlangInteger(value); } /** @@ -113,16 +94,10 @@ public static ErlangInteger cached (char value) { * @return new or cached {@link ErlangInteger} object */ public static ErlangInteger cached (short value) { - int hashCode = 31 + (byte) (value >> 8); - hashCode = 31 * hashCode + (byte) value; - - ErlangInteger result = CACHE.get(hashCode); - if (result != null) { - return result; + if (value >= ErlangIntegerCache.LOW && value <= ErlangIntegerCache.HIGH) { + return ErlangIntegerCache.CACHE[value + (-ErlangIntegerCache.LOW)]; } - result = new ErlangInteger(value); - CACHE.put(hashCode, result); - return result; + return new ErlangInteger(value); } /** @@ -133,18 +108,10 @@ public static ErlangInteger cached (short value) { * @return new or cached {@link ErlangInteger} object */ public static ErlangInteger cached (int value) { - int hashCode = 31 + (byte) (value >> 24); - hashCode = 31 * hashCode + (byte) (value >> 16); - hashCode = 31 * hashCode + (byte) (value >> 8); - hashCode = 31 * hashCode + (byte) value; - - ErlangInteger result = CACHE.get(hashCode); - if (result != null) { - return result; + if (value >= ErlangIntegerCache.LOW && value <= ErlangIntegerCache.HIGH) { + return ErlangIntegerCache.CACHE[value + (-ErlangIntegerCache.LOW)]; } - result = new ErlangInteger(value); - CACHE.put(hashCode, result); - return result; + return new ErlangInteger(value); } /** @@ -155,22 +122,10 @@ public static ErlangInteger cached (int value) { * @return new or cached {@link ErlangInteger} object */ public static ErlangInteger cached (long value) { - int hashCode = 31 + (byte) (value >> 56); - hashCode = 31 * hashCode + (byte) (value >> 48); - hashCode = 31 * hashCode + (byte) (value >> 40); - hashCode = 31 * hashCode + (byte) (value >> 32); - hashCode = 31 * hashCode + (byte) (value >> 24); - hashCode = 31 * hashCode + (byte) (value >> 16); - hashCode = 31 * hashCode + (byte) (value >> 8); - hashCode = 31 * hashCode + (byte) value; - - ErlangInteger result = CACHE.get(hashCode); - if (result != null) { - return result; + if (value >= ErlangIntegerCache.LOW && value <= ErlangIntegerCache.HIGH) { + return ErlangIntegerCache.CACHE[(int) value + (-ErlangIntegerCache.LOW)]; } - result = new ErlangInteger(value); - CACHE.put(hashCode, result); - return result; + return new ErlangInteger(value); } /** @@ -187,39 +142,6 @@ public static ErlangInteger cached (BigInteger value) { return new ErlangInteger(value); } - public static ErlangInteger cached (TermType type, @NonNull ByteBuf buffer) { - int index = buffer.readerIndex(); - ByteArrayHashCode byteProcessor = new ByteArrayHashCode(); - - int length; - switch (type) { - case SMALL_INTEGER: - length = Byte.BYTES; - break; - case INTEGER: - length = Integer.BYTES; - break; - case SMALL_BIG: - length = buffer.readByte() + Byte.BYTES; - break; - case LARGE_BIG: - default: - length = buffer.readInt() + Byte.BYTES; - } - - buffer.forEachByte(buffer.readerIndex(), length, byteProcessor); - - return CACHE.compute(byteProcessor.getHashCode(), (key, value) -> { - if (value == null) { - buffer.readerIndex(index); - return new ErlangInteger(type, buffer); - } else { - buffer.skipBytes(length); - return value; - } - }); - } - BigInteger value; @NonFinal @@ -476,16 +398,21 @@ private void reverse (byte[] data) { } } - @Getter - @FieldDefaults(level = PRIVATE) - private static class ByteArrayHashCode implements ByteProcessor { + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + private static class ErlangIntegerCache { - int hashCode = 1; + private static final int LOW = -128; - @Override - public boolean process (byte value) throws Exception { - hashCode = 31 * hashCode + value; - return true; + private static final int HIGH = 127; + + private static final ErlangInteger[] CACHE; + + static { + CACHE = new ErlangInteger[(HIGH - LOW) + 1]; + int value = LOW; + for (int index = 0; index < CACHE.length; index++) { + CACHE[index] = new ErlangInteger(value++); + } } } } diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangList.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangList.java index 26d6533..7748c9b 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangList.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangList.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangMap.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangMap.java index 9b239ca..efb5428 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangMap.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangNil.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangNil.java index 7fe8867..e8fc3a5 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangNil.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangNil.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPid.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPid.java index cd512ee..d16f658 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPid.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPid.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ package io.appulse.encon.terms.type; import static io.appulse.encon.terms.TermType.PID; -import static io.appulse.encon.terms.TermType.SMALL_ATOM; -import static io.appulse.encon.terms.TermType.SMALL_ATOM_UTF8; import static java.util.Optional.ofNullable; import static lombok.AccessLevel.PRIVATE; @@ -27,10 +25,8 @@ import io.appulse.encon.terms.ErlangTerm; import io.appulse.encon.terms.TermType; import io.appulse.encon.terms.exception.IllegalErlangTermTypeException; -import io.appulse.utils.cache.LruCache; import io.netty.buffer.ByteBuf; -import io.netty.util.ByteProcessor; import lombok.Builder; import lombok.EqualsAndHashCode; import lombok.Getter; @@ -51,34 +47,6 @@ public class ErlangPid extends ErlangTerm { private static final long serialVersionUID = 7083159089429831665L; - private static final LruCache CACHE = new LruCache<>(1000); - - @SuppressWarnings("deprecation") - public static ErlangPid cached (TermType type, ByteBuf buffer) { - int index = buffer.readerIndex(); - byte nodeNameType = buffer.readByte(); - int nodeNameLength = nodeNameType == SMALL_ATOM.getCode() || nodeNameType == SMALL_ATOM_UTF8.getCode() - ? buffer.readUnsignedByte() - : buffer.readUnsignedShort(); - - int length = type == PID - ? nodeNameLength + 9 - : nodeNameLength + 12; - - ByteArrayHashCode byteProcessor = new ByteArrayHashCode(); - buffer.forEachByte(buffer.readerIndex(), length, byteProcessor); - - return CACHE.compute(byteProcessor.getHashCode(), (key, value) -> { - if (value == null) { - buffer.readerIndex(index); - return new ErlangPid(type, buffer); - } else { - buffer.skipBytes(length); - return value; - } - }); - } - @NonFinal NodeDescriptor descriptor; @@ -183,17 +151,4 @@ protected void serialize (ByteBuf buffer) { throw new IllegalErlangTermTypeException(getClass(), getType()); } } - - @Getter - @FieldDefaults(level = PRIVATE) - private static class ByteArrayHashCode implements ByteProcessor { - - int hashCode = 1; - - @Override - public boolean process (byte value) throws Exception { - hashCode = 31 * hashCode + value; - return true; - } - } } diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPort.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPort.java index 056e7b0..ff84fe9 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPort.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangPort.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangReference.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangReference.java index 646c671..d1afe82 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangReference.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangReference.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangString.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangString.java index b250284..0f40576 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangString.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangString.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangTuple.java b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangTuple.java index 2acc9c5..c24f52f 100644 --- a/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangTuple.java +++ b/encon-terms/src/main/java/io/appulse/encon/terms/type/ErlangTuple.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangAtomTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangAtomTest.java index 80a4db9..9dd552a 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangAtomTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangAtomTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,11 +24,9 @@ import java.util.stream.IntStream; - import io.appulse.encon.terms.Erlang; import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpInputStream; import erlang.OtpOutputStream; @@ -36,22 +34,27 @@ import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangAtomTest { +@DisplayName("Check Erlang's Atom term type") +class ErlangAtomTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void instantiate () { + @DisplayName("create new instance by constructor") + void instantiate () { assertThat(new ErlangAtom("hello").getType()) .isEqualTo(SMALL_ATOM_UTF8); @@ -63,12 +66,13 @@ public void instantiate () { } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val value = "hello"; - val bytes = Bytes.allocate() - .put1B(SMALL_ATOM_UTF8.getCode()) - .put1B(value.getBytes(UTF_8).length) - .put(value.getBytes(UTF_8)) + val bytes = Bytes.resizableArray() + .write1B(SMALL_ATOM_UTF8.getCode()) + .write1B(value.getBytes(UTF_8).length) + .writeNB(value.getBytes(UTF_8)) .array(); ErlangAtom atom = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -87,12 +91,13 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val value = "hello"; - val expected = Bytes.allocate() - .put1B(SMALL_ATOM_UTF8.getCode()) - .put1B(value.getBytes(UTF_8).length) - .put(value.getBytes(UTF_8)) + val expected = Bytes.resizableArray() + .write1B(SMALL_ATOM_UTF8.getCode()) + .write1B(value.getBytes(UTF_8).length) + .writeNB(value.getBytes(UTF_8)) .array(); assertThat(Erlang.atom(value).toBytes()) @@ -100,7 +105,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { val smallValue = "popa"; val smallAtom = new ErlangAtom(smallValue); @@ -132,12 +138,13 @@ public void encode () { } @Test - public void decode () throws Exception { + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { val value1 = "hello"; - val bytes1 = Bytes.allocate() - .put1B(SMALL_ATOM_UTF8.getCode()) - .put1B(value1.getBytes(UTF_8).length) - .put(value1.getBytes(UTF_8)) + val bytes1 = Bytes.resizableArray() + .write1B(SMALL_ATOM_UTF8.getCode()) + .write1B(value1.getBytes(UTF_8).length) + .writeNB(value1.getBytes(UTF_8)) .array(); try (val input = new OtpInputStream(bytes1)) { @@ -148,10 +155,10 @@ public void decode () throws Exception { val value2 = "попа"; - val bytes2 = Bytes.allocate() - .put1B(ATOM_UTF8.getCode()) - .put2B(value2.getBytes(UTF_8).length) - .put(value2.getBytes(UTF_8)) + val bytes2 = Bytes.resizableArray() + .write1B(ATOM_UTF8.getCode()) + .write2B(value2.getBytes(UTF_8).length) + .writeNB(value2.getBytes(UTF_8)) .array(); try (val input = new OtpInputStream(bytes2)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBinaryTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBinaryTest.java index e8c8346..d41c8a7 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBinaryTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBinaryTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,9 @@ import static io.netty.buffer.Unpooled.wrappedBuffer; import static org.assertj.core.api.Assertions.assertThat; - import io.appulse.encon.terms.Erlang; import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangBinary; import erlang.OtpInputStream; @@ -32,22 +30,27 @@ import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangBinaryTest { +@DisplayName("Check Erlang's Binary term type") +class ErlangBinaryTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void instantiate () { + @DisplayName("create new instance by constructor") + void instantiate () { val value = new byte[] { 1, 2, 3 }; assertThat(new ErlangBinary(value).asBinary()) @@ -55,13 +58,14 @@ public void instantiate () { } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val value = new byte[] { 1, 2, 3 }; - val bytes = Bytes.allocate() - .put1B(BINARY.getCode()) - .put4B(value.length) - .put(value) + val bytes = Bytes.resizableArray() + .write1B(BINARY.getCode()) + .write4B(value.length) + .writeNB(value) .array(); ErlangBinary binary = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -77,13 +81,14 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val value = new byte[] { 1, 2, 3 }; - val expected = Bytes.allocate() - .put1B(BINARY.getCode()) - .put4B(value.length) - .put(value) + val expected = Bytes.resizableArray() + .write1B(BINARY.getCode()) + .write4B(value.length) + .writeNB(value) .array(); assertThat(Erlang.binary(value).toBytes()) @@ -91,20 +96,22 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { val binary = new byte[] { 1, 2, 3, 4, 5 }; assertThat(Erlang.binary(binary).toBytes()) .isEqualTo(bytes(binary)); } @Test - public void decode () throws Exception { + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { val value = new byte[] { 1, 2, 3 }; - val bytes = Bytes.allocate() - .put1B(BINARY.getCode()) - .put4B(value.length) - .put(value) + val bytes = Bytes.resizableArray() + .write1B(BINARY.getCode()) + .write4B(value.length) + .writeNB(value) .array(); try (val input = new OtpInputStream(bytes)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBitStringTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBitStringTest.java index 2b654cf..424f104 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBitStringTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangBitStringTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,12 +21,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; - import io.appulse.encon.terms.Erlang; import io.appulse.encon.terms.exception.ErlangTermValidationException; import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangBitstr; import erlang.OtpInputStream; @@ -34,22 +32,27 @@ import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangBitStringTest { +@DisplayName("Check Erlang's BitString term type") +class ErlangBitStringTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void erlangTermValidationException () { + @DisplayName("checks throwing exceptions in constructor") + void erlangTermValidationException () { assertThatThrownBy(() -> new ErlangBitString(new byte[] { 1 }, -1)) .isInstanceOf(ErlangTermValidationException.class) .hasMessage("Padding must be in range 0..7"); @@ -60,15 +63,16 @@ public void erlangTermValidationException () { } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val value = new byte[] { 1, 2, 3 }; val pad = 3; - val bytes = Bytes.allocate() - .put1B(BIT_BINNARY.getCode()) - .put4B(value.length) - .put1B(8 - pad) - .put(value) + val bytes = Bytes.resizableArray() + .write1B(BIT_BINNARY.getCode()) + .write4B(value.length) + .write1B(8 - pad) + .writeNB(value) .array(); ErlangBitString bitString = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -84,15 +88,16 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val bits = new byte[] { 1, 2, 3 }; val pad = 3; - val bytes = Bytes.allocate() - .put1B(BIT_BINNARY.getCode()) - .put4B(bits.length) - .put1B(8 - pad) - .put(new byte[] { 1, 2, 0 }) + val bytes = Bytes.resizableArray() + .write1B(BIT_BINNARY.getCode()) + .write4B(bits.length) + .write1B(8 - pad) + .writeNB(new byte[] { 1, 2, 0 }) .array(); assertThat(Erlang.bitstr(bits, pad).toBytes()) @@ -100,7 +105,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { val binary = new byte[] { 1, 2, 3 }; assertThat(Erlang.bitstr(binary, 1).toBytes()) @@ -114,15 +120,16 @@ public void encode () { } @Test - public void decode () throws Exception { + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { val bits = new byte[] { 1, 2, 3 }; val pad = 3; - val bytes = Bytes.allocate() - .put1B(BIT_BINNARY.getCode()) - .put4B(bits.length) - .put1B(8 - pad) - .put(new byte[] { 1, 2, 0 }) + val bytes = Bytes.resizableArray() + .write1B(BIT_BINNARY.getCode()) + .write4B(bits.length) + .write1B(8 - pad) + .writeNB(new byte[] { 1, 2, 0 }) .array(); try (val input = new OtpInputStream(bytes)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangFloatTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangFloatTest.java index 3053c49..7cce282 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangFloatTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangFloatTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,28 +26,34 @@ import erlang.OtpErlangFloat; import erlang.OtpInputStream; import erlang.OtpOutputStream; + import io.appulse.encon.terms.Erlang; import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; + import lombok.SneakyThrows; import lombok.val; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangFloatTest { +@DisplayName("Check Erlang's Float term type") +class ErlangFloatTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(Erlang.number(Float.MIN_NORMAL).toBytes()) .isEqualTo(bytes(Float.MIN_NORMAL)); @@ -68,10 +74,11 @@ public void encode () { } @Test - public void decode () throws Exception { - val bytes1 = Bytes.allocate() - .put1B(FLOAT.getCode()) - .put(String.format("%031.20e", Float.MAX_VALUE).getBytes(ISO_8859_1)) + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { + val bytes1 = Bytes.resizableArray() + .write1B(FLOAT.getCode()) + .writeNB(String.format("%031.20e", Float.MAX_VALUE).getBytes(ISO_8859_1)) .array(); try (val input = new OtpInputStream(bytes1)) { @@ -80,9 +87,9 @@ public void decode () throws Exception { .isEqualTo(input.read_float()); } - val bytes2 = Bytes.allocate() - .put1B(NEW_FLOAT.getCode()) - .put8B(Double.doubleToLongBits(Double.MIN_VALUE)) + val bytes2 = Bytes.resizableArray() + .write1B(NEW_FLOAT.getCode()) + .write8B(Double.doubleToLongBits(Double.MIN_VALUE)) .array(); try (val input = new OtpInputStream(bytes2)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangIntegerTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangIntegerTest.java index 3110520..cd8511d 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangIntegerTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangIntegerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,32 +27,35 @@ import java.util.Arrays; import java.util.stream.IntStream; - import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangLong; import erlang.OtpInputStream; import erlang.OtpOutputStream; import lombok.SneakyThrows; import lombok.val; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangIntegerTest { +@DisplayName("Check Erlang's Integer term type") +class ErlangIntegerTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void instantiate () { + @DisplayName("create new instance by constructor") + void instantiate () { assertThat(new ErlangInteger(254).getType()) .isEqualTo(SMALL_INTEGER); @@ -73,7 +76,8 @@ public void instantiate () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(new ErlangInteger(Character.MIN_VALUE).toBytes()) .isEqualTo(bytes(Character.MIN_VALUE)); @@ -119,10 +123,11 @@ public void encode () { } @Test - public void decode () throws Exception { - val bytes1 = Bytes.allocate() - .put1B(SMALL_INTEGER.getCode()) - .put1B(255) + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { + val bytes1 = Bytes.resizableArray() + .write1B(SMALL_INTEGER.getCode()) + .write1B(255) .array(); try (val input = new OtpInputStream(bytes1)) { @@ -131,9 +136,9 @@ public void decode () throws Exception { .isEqualTo(input.read_int()); } - val bytes2 = Bytes.allocate() - .put1B(INTEGER.getCode()) - .put4B(134217726) + val bytes2 = Bytes.resizableArray() + .write1B(INTEGER.getCode()) + .write4B(134217726) .array(); try (val input = new OtpInputStream(bytes2)) { @@ -167,6 +172,14 @@ public void decode () throws Exception { } } + @Test + @DisplayName("checks caching values") + void cached () { + ErlangInteger num1 = ErlangInteger.cached(1273); + ErlangInteger num2 = ErlangInteger.cached(117); + assertThat(num1).isNotEqualTo(num2); + } + @SneakyThrows private byte[] bytes (char value) { try (OtpOutputStream output = new OtpOutputStream()) { @@ -223,12 +236,12 @@ private byte[] bytes (BigInteger value) { } private byte[] bigBytes (BigInteger value) { - Bytes buffer = Bytes.allocate(); + Bytes buffer = Bytes.resizableArray(); if (value.abs().toByteArray().length < 256) { - buffer.put1B(SMALL_BIG.getCode()); + buffer.write1B(SMALL_BIG.getCode()); } else { - buffer.put1B(LARGE_BIG.getCode()); + buffer.write1B(LARGE_BIG.getCode()); } byte[] bytes = value.abs().toByteArray(); @@ -248,15 +261,15 @@ private byte[] bigBytes (BigInteger value) { } if ((length & 0xFF) == length) { - buffer.put1B(length); // length + buffer.write1B(length); // length } else { - buffer.put4B(length); // length + buffer.write4B(length); // length } val sign = value.signum() < 0 ? 1 : 0; - buffer.put1B(sign); - buffer.put(magnitude); + buffer.write1B(sign); + buffer.writeNB(magnitude); return buffer.array(); } } diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangListTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangListTest.java index 356ffd8..e1840ea 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangListTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangListTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,10 +22,8 @@ import java.util.stream.Stream; - import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangAtom; import erlang.OtpErlangList; @@ -33,28 +31,33 @@ import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangListTest { +@DisplayName("Check Erlang's List term type") +class ErlangListTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val value = new ErlangNil(); - val bytes = Bytes.allocate() - .put1B(LIST.getCode()) - .put4B(1) - .put(value.toBytes()) - .put(new ErlangNil().toBytes()) + val bytes = Bytes.resizableArray() + .write1B(LIST.getCode()) + .write4B(1) + .writeNB(value.toBytes()) + .writeNB(new ErlangNil().toBytes()) .array(); ErlangList list = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -80,13 +83,14 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val value = new ErlangNil(); - val expected = Bytes.allocate() - .put1B(LIST.getCode()) - .put4B(1) - .put(value.toBytes()) - .put(new ErlangNil().toBytes()) + val expected = Bytes.resizableArray() + .write1B(LIST.getCode()) + .write4B(1) + .writeNB(value.toBytes()) + .writeNB(new ErlangNil().toBytes()) .array(); assertThat(new ErlangList(value).toBytes()) @@ -94,7 +98,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { String[] values = new String[] { "one", "two", diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangMapTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangMapTest.java index 4124052..993c891 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangMapTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangMapTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,31 +22,34 @@ import java.util.LinkedHashMap; import java.util.List; - import io.appulse.encon.terms.ErlangTerm; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangMap; import erlang.OtpErlangObject; import erlang.OtpErlangString; import erlang.OtpOutputStream; import lombok.SneakyThrows; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangMapTest { +@DisplayName("Check Erlang's Map term type") +class ErlangMapTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { LinkedHashMap value = new LinkedHashMap<>(3); value.put("one", "1"); value.put("two", "2"); diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangNilTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangNilTest.java index 4aafb86..90c8b73 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangNilTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangNilTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,29 +22,33 @@ import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpOutputStream; import lombok.SneakyThrows; import lombok.val; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangNilTest { +@DisplayName("Check Erlang's NIL term type") +class ErlangNilTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void newInstance () { - val bytes = Bytes.allocate() - .put1B(NIL.getCode()) + @DisplayName("create new instance from bytes") + void newInstance () { + val bytes = Bytes.resizableArray() + .write1B(NIL.getCode()) .array(); ErlangNil nil = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -54,9 +58,10 @@ public void newInstance () { } @Test - public void toBytes () { - val expected = Bytes.allocate() - .put1B(NIL.getCode()) + @DisplayName("convert instance to byte array") + void toBytes () { + val expected = Bytes.resizableArray() + .write1B(NIL.getCode()) .array(); assertThat(new ErlangNil().toBytes()) @@ -64,7 +69,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(new ErlangNil().toBytes()) .isEqualTo(bytes()); } diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPidTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPidTest.java index 8951b8a..831fcb8 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPidTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPidTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,39 +24,45 @@ import erlang.OtpErlangPid; import erlang.OtpInputStream; import erlang.OtpOutputStream; + import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; + import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangPidTest { +@DisplayName("Check Erlang's Pid term type") +class ErlangPidTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val node = "popa"; val id = 500; val serial = 10; val creation = 42; - val bytes = Bytes.allocate() - .put1B(PID.getCode()) - .put(new ErlangAtom(node).toBytes()) - .put4B(id & 0x7FFF) - .put4B(serial & 0x1FFF) - .put1B(creation & 0x3) + val bytes = Bytes.resizableArray() + .write1B(PID.getCode()) + .writeNB(new ErlangAtom(node).toBytes()) + .write4B(id & 0x7FFF) + .write4B(serial & 0x1FFF) + .write1B(creation & 0x3) .array(); ErlangPid pid = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -78,18 +84,19 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val node = "popa"; val id = 500; val serial = 10; val creation = 42; - val expected = Bytes.allocate() - .put1B(PID.getCode()) - .put(new ErlangAtom(node).toBytes()) - .put4B(id & 0x7FFF) - .put4B(serial & 0x1FFF) - .put1B(creation & 0x3) + val expected = Bytes.resizableArray() + .write1B(PID.getCode()) + .writeNB(new ErlangAtom(node).toBytes()) + .write4B(id & 0x7FFF) + .write4B(serial & 0x1FFF) + .write1B(creation & 0x3) .array(); assertThat(ErlangPid.builder() @@ -104,7 +111,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(ErlangPid.builder() .node("popa@localhost") .id(1) @@ -128,13 +136,14 @@ public void encode () { } @Test - public void decode () throws Exception { - byte[] bytes1 = Bytes.allocate() - .put1B(PID.getCode()) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) - .put1B(Integer.MAX_VALUE) + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { + byte[] bytes1 = Bytes.resizableArray() + .write1B(PID.getCode()) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) + .write1B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes1)) { @@ -154,12 +163,12 @@ public void decode () throws Exception { .isEqualTo(otpPid.serial()); } - byte[] bytes2 = Bytes.allocate() - .put1B(NEW_PID.getCode()) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) + byte[] bytes2 = Bytes.resizableArray() + .write1B(NEW_PID.getCode()) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes2)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPortTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPortTest.java index 0265952..36b6d7e 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPortTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangPortTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangPort; import erlang.OtpInputStream; @@ -31,31 +30,36 @@ import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangPortTest { +@DisplayName("Check Erlang's Port term type") +class ErlangPortTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val node = "popa@localhost"; val id = 500; val creation = 42; - val bytes = Bytes.allocate() - .put1B(PORT.getCode()) - .put(new ErlangAtom(node).toBytes()) - .put4B(id & 0xFFFFFFF) - .put1B(creation & 0x3) + val bytes = Bytes.resizableArray() + .write1B(PORT.getCode()) + .writeNB(new ErlangAtom(node).toBytes()) + .write4B(id & 0xFFFFFFF) + .write1B(creation & 0x3) .array(); ErlangPort port = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -74,16 +78,17 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val node = "popa@localhost"; val id = 500; val creation = 42; - val expected = Bytes.allocate() - .put1B(PORT.getCode()) - .put(new ErlangAtom(node).toBytes()) - .put4B(id & 0xFFFFFFF) - .put1B(creation & 0x3) + val expected = Bytes.resizableArray() + .write1B(PORT.getCode()) + .writeNB(new ErlangAtom(node).toBytes()) + .write4B(id & 0xFFFFFFF) + .write1B(creation & 0x3) .array(); assertThat(ErlangPort.builder() @@ -97,7 +102,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(ErlangPort.builder() .node("popa@localhost") .id(1) @@ -119,12 +125,13 @@ public void encode () { } @Test - public void decode () throws Exception { - byte[] bytes1 = Bytes.allocate() - .put1B(PORT.getCode()) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put4B(Integer.MAX_VALUE) - .put1B(Integer.MAX_VALUE) + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { + byte[] bytes1 = Bytes.resizableArray() + .write1B(PORT.getCode()) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write4B(Integer.MAX_VALUE) + .write1B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes1)) { @@ -141,11 +148,11 @@ public void decode () throws Exception { .isEqualTo(otpPid.creation()); } - byte[] bytes2 = Bytes.allocate() - .put1B(NEW_PORT.getCode()) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) + byte[] bytes2 = Bytes.resizableArray() + .write1B(NEW_PORT.getCode()) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes2)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangReferenceTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangReferenceTest.java index 92ef439..e6ae23c 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangReferenceTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangReferenceTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,32 +26,34 @@ import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangRef; import erlang.OtpInputStream; import erlang.OtpOutputStream; import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -@Slf4j -public class ErlangReferenceTest { +@DisplayName("Check Erlang's Reference term type") +class ErlangReferenceTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void instantiate () { + @DisplayName("create new instance by constructor") + void instantiate () { assertThat(ErlangReference.builder() .node("popa") .id(3) @@ -63,21 +65,22 @@ public void instantiate () { } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val node = "popa@localhost"; val ids = new long[] { 1, 0, 0 }; val creation = 42; - val builder = Bytes.allocate() - .put1B(NEW_REFERENCE.getCode()) - .put2B(ids.length) - .put(new ErlangAtom(node).toBytes()) - .put1B(creation) - .put4B(ids[0]); + val builder = Bytes.resizableArray() + .write1B(NEW_REFERENCE.getCode()) + .write2B(ids.length) + .writeNB(new ErlangAtom(node).toBytes()) + .write1B(creation) + .write4B(ids[0]); LongStream.of(ids) .skip(1) - .forEachOrdered(builder::put4B); + .forEachOrdered(builder::write4B); val expected = builder.array(); @@ -100,21 +103,22 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val node = "popa@localhost"; val ids = new long[] { 1, 0, 0 }; val creation = 42; - val builder = Bytes.allocate() - .put1B(NEW_REFERENCE.getCode()) - .put2B(ids.length) - .put(new ErlangAtom(node).toBytes()) - .put1B(creation & 0x3) - .put4B(ids[0] & 0x3FFFF); + val builder = Bytes.resizableArray() + .write1B(NEW_REFERENCE.getCode()) + .write2B(ids.length) + .writeNB(new ErlangAtom(node).toBytes()) + .write1B(creation & 0x3) + .write4B(ids[0] & 0x3FFFF); LongStream.of(ids) .skip(1) - .forEachOrdered(builder::put4B); + .forEachOrdered(builder::write4B); val expected = builder.array(); @@ -129,7 +133,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(ErlangReference.builder() .node("popa@localhost") .ids(new long[] { 1 }) @@ -168,12 +173,13 @@ public void encode () { } @Test - public void decode () throws Exception { - byte[] bytes1 = Bytes.allocate() - .put1B(REFERENCE.getCode()) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put4B(Integer.MAX_VALUE) - .put1B(Integer.MAX_VALUE) + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { + byte[] bytes1 = Bytes.resizableArray() + .write1B(REFERENCE.getCode()) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write4B(Integer.MAX_VALUE) + .write1B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes1)) { @@ -193,14 +199,14 @@ public void decode () throws Exception { .isEqualTo(otpRef.creation()); } - byte[] bytes2 = Bytes.allocate() - .put1B(NEW_REFERENCE.getCode()) - .put2B(3) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put1B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) + byte[] bytes2 = Bytes.resizableArray() + .write1B(NEW_REFERENCE.getCode()) + .write2B(3) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write1B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes2)) { @@ -220,14 +226,14 @@ public void decode () throws Exception { .isEqualTo(otpRef.creation()); } - byte[] bytes3 = Bytes.allocate() - .put1B(NEWER_REFERENCE.getCode()) - .put2B(3) - .put(new ErlangAtom("popa@localhost").toBytes()) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) - .put4B(Integer.MAX_VALUE) + byte[] bytes3 = Bytes.resizableArray() + .write1B(NEWER_REFERENCE.getCode()) + .write2B(3) + .writeNB(new ErlangAtom("popa@localhost").toBytes()) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) + .write4B(Integer.MAX_VALUE) .array(); try (val input = new OtpInputStream(bytes3)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangStringTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangStringTest.java index 9ae437e..eaf1252 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangStringTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangStringTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,29 +25,33 @@ import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangString; import erlang.OtpInputStream; import erlang.OtpOutputStream; import lombok.SneakyThrows; import lombok.val; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangStringTest { +@DisplayName("Check Erlang's String term type") +class ErlangStringTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { assertThat(new ErlangString("").toBytes()) .isEqualTo(bytes("")); @@ -64,11 +68,12 @@ public void encode () { } @Test - public void decode () throws Exception { - val bytes = Bytes.allocate() - .put1B(STRING.getCode()) - .put2B("popa".length()) - .put("popa", ISO_8859_1) + @DisplayName("decode instance from byte array and compare with jinterface result") + void decode () throws Exception { + val bytes = Bytes.resizableArray() + .write1B(STRING.getCode()) + .write2B("popa".length()) + .writeNB("popa", ISO_8859_1) .array(); try (val input = new OtpInputStream(bytes)) { diff --git a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangTupleTest.java b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangTupleTest.java index 091cf5f..015fa0e 100644 --- a/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangTupleTest.java +++ b/encon-terms/src/test/java/io/appulse/encon/terms/type/ErlangTupleTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,6 @@ import io.appulse.encon.terms.ErlangTerm; import io.appulse.utils.Bytes; -import io.appulse.utils.test.TestMethodNamePrinter; import erlang.OtpErlangAtom; import erlang.OtpErlangInt; @@ -42,22 +41,27 @@ import lombok.SneakyThrows; import lombok.val; import org.assertj.core.api.SoftAssertions; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestRule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; /** * * @author Artem Labazin * @since 1.0.0 */ -public class ErlangTupleTest { +@DisplayName("Check Erlang's Tuple term type") +class ErlangTupleTest { - @Rule - public TestRule watcher = new TestMethodNamePrinter(); + @BeforeEach + void beforeEach (TestInfo testInfo) { + System.out.println("- " + testInfo.getDisplayName()); + } @Test - public void instantiate () { + @DisplayName("create new instance by constructor") + void instantiate () { assertThat(new ErlangTuple(new ErlangNil()).getType()) .isEqualTo(SMALL_TUPLE); @@ -71,12 +75,13 @@ public void instantiate () { } @Test - public void newInstance () { + @DisplayName("create new instance from bytes") + void newInstance () { val value = new ErlangNil(); - val bytes = Bytes.allocate() - .put1B(SMALL_TUPLE.getCode()) - .put1B(1) - .put(value.toBytes()) + val bytes = Bytes.resizableArray() + .write1B(SMALL_TUPLE.getCode()) + .write1B(1) + .writeNB(value.toBytes()) .array(); ErlangTuple tuple = ErlangTerm.newInstance(wrappedBuffer(bytes)); @@ -99,12 +104,13 @@ public void newInstance () { } @Test - public void toBytes () { + @DisplayName("convert instance to byte array") + void toBytes () { val value = new ErlangNil(); - val expected = Bytes.allocate() - .put1B(SMALL_TUPLE.getCode()) - .put1B(1) - .put(value.toBytes()) + val expected = Bytes.resizableArray() + .write1B(SMALL_TUPLE.getCode()) + .write1B(1) + .writeNB(value.toBytes()) .array(); assertThat(new ErlangTuple(value).toBytes()) @@ -112,7 +118,8 @@ public void toBytes () { } @Test - public void encode () { + @DisplayName("encode instance to byte array and compare with jinterface output") + void encode () { String[] values = new String[] { "one", "two", diff --git a/encon/README.md b/encon/README.md index e947af2..ebc4b31 100644 --- a/encon/README.md +++ b/encon/README.md @@ -1,5 +1,13 @@ # Overview +[![build_status](https://travis-ci.org/appulse-projects/encon-java.svg?branch=master)](https://travis-ci.org/appulse-projects/encon-java) +[![maven_central](https://maven-badges.herokuapp.com/maven-central/io.appulse.encon/encon/badge.svg)](https://search.maven.org/search?q=a:encon) +[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html) +[![JavaDoc](http://www.javadoc.io/badge/io.appulse.encon/encon.svg)](http://www.javadoc.io/doc/io.appulse.encon/encon) + + + + This project contains the general-purpose data-binding functionality. - [Add dependency](#add-dependency) @@ -21,7 +29,7 @@ Adding encon's dependency to your `JVM` app: io.appulse.encon encon - 1.6.5 + 2.0.0 ... @@ -30,7 +38,7 @@ Adding encon's dependency to your `JVM` app: **Gradle**: ```groovy -compile 'io.appulse.encon:encon:1.6.5' +compile 'io.appulse.encon:encon:2.0.0' ``` ## Start the Node diff --git a/encon/pom.xml b/encon/pom.xml index 9380b1d..a3813fb 100644 --- a/encon/pom.xml +++ b/encon/pom.xml @@ -1,7 +1,7 @@