This is an automated email from the ASF dual-hosted git repository. sruehl pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/master by this push: new fb2d3a8 added tests for factory methods. fb2d3a8 is described below commit fb2d3a8e5d71f5188574bde80209eaa625bfff51 Author: Sebastian Rühl <sru...@apache.org> AuthorDate: Sat Feb 17 00:52:13 2018 +0100 added tests for factory methods. --- .../plc4x/java/ads/api/commands/types/Device.java | 5 +- .../java/ads/api/commands/types/MajorVersion.java | 4 + .../java/ads/api/commands/types/MinorVersion.java | 4 + .../java/ads/api/commands/types/TimeStamp.java | 8 ++ .../plc4x/java/ads/api/generic/types/AMSError.java | 7 +- .../plc4x/java/ads/api/generic/types/Command.java | 8 ++ .../plc4x/java/ads/api/generic/types/State.java | 1 - .../types/CommandTypesFactoryMethodTest.java | 87 ++++++++++++++++++++++ .../types/GenericTypesFactoryMethodTest.java | 80 ++++++++++++++++++++ 9 files changed, 199 insertions(+), 5 deletions(-) diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Device.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Device.java index 69f43f7..77f14f4 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Device.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Device.java @@ -19,6 +19,7 @@ package org.apache.plc4x.java.ads.api.commands.types; import io.netty.buffer.ByteBuf; +import org.apache.commons.lang3.StringUtils; import org.apache.plc4x.java.ads.api.util.ByteValue; import java.nio.charset.Charset; @@ -46,12 +47,12 @@ public class Device extends ByteValue { public static Device of(String value) { requireNonNull(value); - return new Device(value.getBytes()); + return new Device(StringUtils.leftPad(value,NUM_BYTES).getBytes()); } public static Device of(String value, Charset charset) { requireNonNull(value); - return new Device(value.getBytes(charset)); + return new Device(StringUtils.leftPad(value,NUM_BYTES).getBytes(charset)); } @Override diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MajorVersion.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MajorVersion.java index 47ca19a..5ada248 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MajorVersion.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MajorVersion.java @@ -40,6 +40,10 @@ public class MajorVersion extends ByteValue { return new MajorVersion((byte) value); } + public static MajorVersion of(String value) { + return of(Integer.parseInt(value)); + } + public static MajorVersion of(ByteBuf byteBuf) { byte[] values = new byte[NUM_BYTES]; byteBuf.readBytes(values); diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MinorVersion.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MinorVersion.java index a118b44..54b55ce 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MinorVersion.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/MinorVersion.java @@ -40,6 +40,10 @@ public class MinorVersion extends ByteValue { return new MinorVersion((byte) value); } + public static MinorVersion of(String value) { + return of(Integer.parseInt(value)); + } + public static MinorVersion of(ByteBuf byteBuf) { byte[] values = new byte[NUM_BYTES]; byteBuf.readBytes(values); diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java index 2e42d66..c7a1e63 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java @@ -89,10 +89,18 @@ public class TimeStamp extends ByteValue { return of(BigInteger.valueOf(value)); } + public static TimeStamp of(String value) { + return of(Long.valueOf(value)); + } + public static TimeStamp ofWinTime(long value) { return of(javaToWinTime(BigInteger.valueOf(value))); } + public static TimeStamp ofWinTime(String value) { + return of(Long.valueOf(value)); + } + private static TimeStamp of(byte... values) { return new TimeStamp(values); } diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSError.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSError.java index de115ed..2a658e5 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSError.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSError.java @@ -44,11 +44,14 @@ public class AMSError extends UnsignedIntLEByteValue { return new AMSError(values); } - private static AMSError of(long errorCode) { - checkUnsignedBounds(errorCode, NUM_BYTES); + public static AMSError of(long errorCode) { return new AMSError(errorCode); } + public static AMSError of(String errorCode) { + return of(Long.parseLong(errorCode)); + } + public static AMSError of(ByteBuf byteBuf) { return new AMSError(byteBuf); } diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/Command.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/Command.java index 1c78b74..de536f6 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/Command.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/Command.java @@ -101,6 +101,14 @@ public enum Command implements ByteReadable { return UNKNOWN; } + public static Command of(String value) { + return valueOf(value); + } + + public static Command ofInt(String intValue) { + return of(Integer.parseInt(intValue)); + } + public static Command of(ByteBuf byteBuf) { return of(byteBuf.readUnsignedShortLE()); } diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/State.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/State.java index 027328f..30c974e 100644 --- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/State.java +++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/State.java @@ -109,7 +109,6 @@ public class State extends UnsignedShortLEByteValue { } private static State of(int value) { - checkUnsignedBounds(value, NUM_BYTES); return new State(value); } diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/CommandTypesFactoryMethodTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/CommandTypesFactoryMethodTest.java new file mode 100644 index 0000000..1f9dd8a --- /dev/null +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/CommandTypesFactoryMethodTest.java @@ -0,0 +1,87 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.plc4x.java.ads.api.commands.types; + +import org.apache.plc4x.java.ads.api.util.UnsignedIntLEByteValue; +import org.apache.plc4x.java.ads.api.util.UnsignedShortLEByteValue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assume.assumeThat; + +@RunWith(Parameterized.class) +public class CommandTypesFactoryMethodTest { + + @Parameterized.Parameter + public Class<?> clazz; + + @Parameterized.Parameters(name = "{index} {0}") + public static Collection<Object[]> data() { + return Stream.of( + ADSState.class, + CycleTime.class, + Data.class, + Device.class, + DeviceState.class, + IndexGroup.class, + IndexOffset.class, + Length.class, + MajorVersion.class, + MaxDelay.class, + MinorVersion.class, + NotificationHandle.class, + ReadLength.class, + Result.class, + Samples.class, + SampleSize.class, + Stamps.class, + TimeStamp.class, + TransmissionMode.class, + Version.class, + WriteLength.class + ).map(clazz -> new Object[]{clazz}).collect(Collectors.toList()); + } + + @Test + public void testOfInt() throws Exception { + assumeThat(clazz, instanceOf(UnsignedShortLEByteValue.class)); + Method ofMethod = clazz.getDeclaredMethod("of", int.class); + ofMethod.invoke(null, 1); + } + + @Test + public void testOfLong() throws Exception { + assumeThat(clazz, instanceOf(UnsignedIntLEByteValue.class)); + Method ofMethod = clazz.getDeclaredMethod("of", long.class); + ofMethod.invoke(null, 1L); + } + + @Test + public void testOfString() throws Exception { + Method ofMethod = clazz.getDeclaredMethod("of", String.class); + ofMethod.invoke(null, "1"); + } +} diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/GenericTypesFactoryMethodTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/GenericTypesFactoryMethodTest.java new file mode 100644 index 0000000..275bace --- /dev/null +++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/generic/types/GenericTypesFactoryMethodTest.java @@ -0,0 +1,80 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.plc4x.java.ads.api.generic.types; + +import org.apache.plc4x.java.ads.api.util.UnsignedIntLEByteValue; +import org.apache.plc4x.java.ads.api.util.UnsignedShortLEByteValue; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assume.assumeThat; + +@RunWith(Parameterized.class) +public class GenericTypesFactoryMethodTest { + + @Parameterized.Parameter + public Class<?> clazz; + + @Parameterized.Parameters(name = "{index} {0}") + public static Collection<Object[]> data() { + return Stream.of( + AMSError.class, + AMSNetId.class, + AMSPort.class, + Command.class, + DataLength.class, + Invoke.class, + Length.class, + State.class + ).map(clazz -> new Object[]{clazz}).collect(Collectors.toList()); + } + + @Test + public void testOfInt() throws Exception { + assumeThat(clazz, instanceOf(UnsignedShortLEByteValue.class)); + Method ofMethod = clazz.getDeclaredMethod("of", int.class); + ofMethod.invoke(null, 1); + } + + @Test + public void testOfLong() throws Exception { + assumeThat(clazz, instanceOf(UnsignedIntLEByteValue.class)); + Method ofMethod = clazz.getDeclaredMethod("of", long.class); + ofMethod.invoke(null, 1L); + } + + @Test + public void testOfString() throws Exception { + Method ofMethod = clazz.getDeclaredMethod("of", String.class); + String testString = "1"; + if (clazz == AMSNetId.class) { + testString = "1.1.1.1.1.1"; + } else if (clazz == Command.class) { + testString = Command.ADS_Add_Device_Notification.name(); + } + ofMethod.invoke(null, testString); + } +} -- To stop receiving notification emails like this one, please contact sru...@apache.org.