You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cs...@apache.org on 2023/12/07 14:38:21 UTC
(activemq-openwire) branch main updated: OPENWIRE-69 - Fix PartialCommand and ProducerAck sequence ids (#4)
This is an automated email from the ASF dual-hosted git repository.
cshannon pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-openwire.git
The following commit(s) were added to refs/heads/main by this push:
new db4b306 OPENWIRE-69 - Fix PartialCommand and ProducerAck sequence ids (#4)
db4b306 is described below
commit db4b306d28e05fb46a9c68346120c60511800f74
Author: Christopher L. Shannon <ch...@gmail.com>
AuthorDate: Thu Dec 7 09:38:16 2023 -0500
OPENWIRE-69 - Fix PartialCommand and ProducerAck sequence ids (#4)
PartialCommand and ProducerAck duplicate the same sequence id on more
than one property. This commit fixes the sequence and also adds
validation to the marshaller to verify that sequences are not duplicated
and are contiguous and if they are not an IllegalArgumentException is thrown.
---
.../activemq/openwire/commands/PartialCommand.java | 2 +-
.../activemq/openwire/commands/ProducerAck.java | 2 +-
openwire-generator/pom.xml | 16 +--
.../openwire/generator/GeneratorUtils.java | 8 +-
.../generator/OpenWirePropertyDescriptor.java | 2 +-
.../openwire/generator/OpenWireTypeDescriptor.java | 52 ++++---
.../generator/OpenWireTypeDescriptorTest.java | 155 +++++++++++++++++++++
7 files changed, 202 insertions(+), 35 deletions(-)
diff --git a/openwire-core/src/main/java/org/apache/activemq/openwire/commands/PartialCommand.java b/openwire-core/src/main/java/org/apache/activemq/openwire/commands/PartialCommand.java
index 6a69c24..a3e80ca 100644
--- a/openwire-core/src/main/java/org/apache/activemq/openwire/commands/PartialCommand.java
+++ b/openwire-core/src/main/java/org/apache/activemq/openwire/commands/PartialCommand.java
@@ -33,7 +33,7 @@ public class PartialCommand implements Command {
@OpenWireProperty(version = 1, sequence = 1)
private int commandId;
- @OpenWireProperty(version = 1, sequence = 1, mandatory = true)
+ @OpenWireProperty(version = 1, sequence = 2, mandatory = true)
private byte[] data;
public PartialCommand() {
diff --git a/openwire-core/src/main/java/org/apache/activemq/openwire/commands/ProducerAck.java b/openwire-core/src/main/java/org/apache/activemq/openwire/commands/ProducerAck.java
index e01a187..104a5e9 100644
--- a/openwire-core/src/main/java/org/apache/activemq/openwire/commands/ProducerAck.java
+++ b/openwire-core/src/main/java/org/apache/activemq/openwire/commands/ProducerAck.java
@@ -35,7 +35,7 @@ public class ProducerAck extends BaseCommand {
@OpenWireProperty(version = 3, sequence = 1)
protected ProducerId producerId;
- @OpenWireProperty(version = 3, sequence = 1)
+ @OpenWireProperty(version = 3, sequence = 2)
protected int size;
public ProducerAck() {
diff --git a/openwire-generator/pom.xml b/openwire-generator/pom.xml
index 20464b6..dc0dfba 100644
--- a/openwire-generator/pom.xml
+++ b/openwire-generator/pom.xml
@@ -52,6 +52,11 @@
<artifactId>ant</artifactId>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
@@ -59,15 +64,4 @@
</dependency>
</dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <skip>true</skip>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
</project>
diff --git a/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/GeneratorUtils.java b/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/GeneratorUtils.java
index 05b1f40..9333e4b 100644
--- a/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/GeneratorUtils.java
+++ b/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/GeneratorUtils.java
@@ -68,7 +68,7 @@ public class GeneratorUtils {
*
* @throws Exception if an error occurs while scanning for properties.
*/
- public static Set<Field> finalOpenWireProperties(Class<?> openWireType) throws Exception {
+ public static Set<Field> finalOpenWireProperties(Class<?> openWireType) {
@SuppressWarnings("unchecked")
final Set<Field> properties =
ReflectionUtils.getAllFields(openWireType, ReflectionUtils.withAnnotation(OpenWireProperty.class));
@@ -90,7 +90,7 @@ public class GeneratorUtils {
* @throws Exception if an error occurs finding the get method.
*/
@SuppressWarnings("unchecked")
- public static Method findGetMethodForProperty(Class<?> openWireType, OpenWirePropertyDescriptor property) throws Exception {
+ public static Method findGetMethodForProperty(Class<?> openWireType, OpenWirePropertyDescriptor property) {
if (property.getType().equals(boolean.class)) {
Set<Method> getters = getAllMethods(openWireType,
@@ -139,7 +139,7 @@ public class GeneratorUtils {
* @throws Exception if an error occurs finding the set method.
*/
@SuppressWarnings("unchecked")
- public static Method findSetMethodForProperty(Class<?> openWireType, OpenWirePropertyDescriptor property) throws Exception {
+ public static Method findSetMethodForProperty(Class<?> openWireType, OpenWirePropertyDescriptor property) {
Set<Method> setters = getAllMethods(openWireType,
withModifier(Modifier.PUBLIC)
@@ -170,7 +170,7 @@ public class GeneratorUtils {
*
* @throws Exception if an error occurs.
*/
- public static File createDestination(String base, String targetPackage) throws Exception {
+ public static File createDestination(String base, String targetPackage) {
targetPackage = targetPackage.replace(".", File.separator);
final File outputFolder = new File(base, targetPackage);
diff --git a/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWirePropertyDescriptor.java b/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWirePropertyDescriptor.java
index 24b6698..fc536fc 100644
--- a/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWirePropertyDescriptor.java
+++ b/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWirePropertyDescriptor.java
@@ -38,7 +38,7 @@ public class OpenWirePropertyDescriptor implements Comparable<OpenWirePropertyDe
private final String getterName;
private final String setterName;
- public OpenWirePropertyDescriptor(Class<?> openWireType, Field openWireProperty) throws Exception {
+ public OpenWirePropertyDescriptor(Class<?> openWireType, Field openWireProperty) {
this.openWireType = openWireType;
this.openWireProperty = openWireProperty;
this.propertyAnnotation = openWireProperty.getAnnotation(OpenWireProperty.class);
diff --git a/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWireTypeDescriptor.java b/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWireTypeDescriptor.java
index d0c1352..31f773b 100644
--- a/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWireTypeDescriptor.java
+++ b/openwire-generator/src/main/java/org/apache/activemq/openwire/generator/OpenWireTypeDescriptor.java
@@ -19,10 +19,12 @@ package org.apache.activemq.openwire.generator;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
-import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
-
+import java.util.concurrent.atomic.AtomicInteger;
import org.apache.activemq.openwire.annotations.OpenWireType;
/**
@@ -34,24 +36,18 @@ public class OpenWireTypeDescriptor {
private final OpenWireType typeAnnotation;
private final List<OpenWirePropertyDescriptor> properties;
- public OpenWireTypeDescriptor(Class<?> openWireType) throws Exception {
+ public OpenWireTypeDescriptor(Class<?> openWireType) {
this.openWireType = openWireType;
- this.typeAnnotation = openWireType.getAnnotation(OpenWireType.class);
-
- List<OpenWirePropertyDescriptor> properties = new ArrayList<OpenWirePropertyDescriptor>();
-
- Set<Field> fields = GeneratorUtils.finalOpenWireProperties(openWireType);
- for (Field field : fields) {
+ this.typeAnnotation = Optional.ofNullable(openWireType.getAnnotation(OpenWireType.class))
+ .orElseThrow(() -> new IllegalArgumentException(openWireType + " is missing required OpenWireType annotation."));
+ this.properties = GeneratorUtils.finalOpenWireProperties(openWireType)
// Only track fields from the given type and not its super types.
- if (field.getDeclaringClass().equals(openWireType)) {
- properties.add(new OpenWirePropertyDescriptor(openWireType, field));
- }
- }
-
- // Ensure ordering my marshaler sequence.
- Collections.sort(properties);
+ .stream().filter(field -> field.getDeclaringClass().equals(openWireType))
+ .map(field -> new OpenWirePropertyDescriptor(openWireType, field))
+ .sorted().toList();
- this.properties = Collections.unmodifiableList(properties);
+ // Make sure all sequence numbers seen are correct
+ validateSequenceNumbers();
}
/**
@@ -121,4 +117,26 @@ public class OpenWireTypeDescriptor {
public List<OpenWirePropertyDescriptor> getProperties() {
return properties;
}
+
+ private void validateSequenceNumbers() {
+ int expected = 1;
+ for (OpenWirePropertyDescriptor desc : properties) {
+ if (desc.getMarshalingSequence() < 1) {
+ sequenceError(desc, expected, "Sequence number must be positive");
+ } else if (desc.getMarshalingSequence() < expected) {
+ sequenceError(desc, expected, "Sequence numbers must not be duplicated");
+ } else if (expected != desc.getMarshalingSequence()) {
+ sequenceError(desc, expected, "Sequence numbers must be contiguous");
+ }
+ expected++;
+ }
+ }
+
+ private String sequenceError(OpenWirePropertyDescriptor descriptor, int expected, String error) {
+ throw new IllegalArgumentException("Property: '" + descriptor.getPropertyName()
+ + "' on OpenWireType: '" + openWireType + "' has invalid sequence number '"
+ + descriptor.getMarshalingSequence() + "'. Error: " + error
+ + ". Expected sequence number is: '" + expected + "'.");
+ }
+
}
diff --git a/openwire-generator/src/test/java/org/apache/activenq/openwire/generator/OpenWireTypeDescriptorTest.java b/openwire-generator/src/test/java/org/apache/activenq/openwire/generator/OpenWireTypeDescriptorTest.java
new file mode 100644
index 0000000..d4037a2
--- /dev/null
+++ b/openwire-generator/src/test/java/org/apache/activenq/openwire/generator/OpenWireTypeDescriptorTest.java
@@ -0,0 +1,155 @@
+/**
+ * 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.activenq.openwire.generator;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.apache.activemq.openwire.annotations.OpenWireProperty;
+import org.apache.activemq.openwire.annotations.OpenWireType;
+import org.apache.activemq.openwire.generator.OpenWireTypeDescriptor;
+import org.junit.Test;
+
+public class OpenWireTypeDescriptorTest {
+
+ @Test
+ public void testSuccess() {
+ new OpenWireTypeDescriptor(ValidOpenWireType.class);
+ }
+
+ @Test
+ public void testNotAnOpenWireType() {
+ assertIllegalArgumentException(String.class, "String is missing required OpenWireType annotation");
+ }
+
+ @Test
+ public void testDuplicateSequence() {
+ assertIllegalArgumentException(DuplicateSequence.class, "Sequence numbers must not be duplicated");
+ }
+
+ @Test
+ public void testZeroSequence() {
+ assertIllegalArgumentException(ZeroSequence.class, "Sequence number must be positive");
+ }
+
+ @Test
+ public void testNotContiguous() {
+ assertIllegalArgumentException(NotContiguousSequence.class, "Sequence numbers must be contiguous");
+ }
+
+ private void assertIllegalArgumentException(Class<?> openWireType, String error) {
+ try {
+ new OpenWireTypeDescriptor(openWireType);
+ fail("IllegalArgumentException was expected with error " + error);
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.toString().contains(error));
+ }
+ }
+
+ @OpenWireType(typeCode = 1, version = 1)
+ private static class ValidOpenWireType {
+
+ @OpenWireProperty(version = 1, sequence = 1)
+ protected int property1;
+
+ @OpenWireProperty(version = 1, sequence = 2)
+ protected int property2;
+
+ public int getProperty1() {
+ return property1;
+ }
+
+ public void setProperty1(int property1) {
+ this.property1 = property1;
+ }
+
+ public int getProperty2() {
+ return property2;
+ }
+
+ public void setProperty2(int property2) {
+ this.property2 = property2;
+ }
+ }
+
+ @OpenWireType(typeCode = 1, version = 1)
+ private static class DuplicateSequence {
+
+ @OpenWireProperty(version = 1, sequence = 1)
+ protected int property1;
+
+ @OpenWireProperty(version = 1, sequence = 1)
+ protected int property2;
+
+ public int getProperty1() {
+ return property1;
+ }
+
+ public void setProperty1(int property1) {
+ this.property1 = property1;
+ }
+
+ public int getProperty2() {
+ return property2;
+ }
+
+ public void setProperty2(int property2) {
+ this.property2 = property2;
+ }
+ }
+
+ @OpenWireType(typeCode = 1, version = 1)
+ private static class ZeroSequence {
+
+ @OpenWireProperty(version = 1, sequence = 0)
+ protected int property1;
+
+ public int getProperty1() {
+ return property1;
+ }
+
+ public void setProperty1(int property1) {
+ this.property1 = property1;
+ }
+ }
+
+ @OpenWireType(typeCode = 1, version = 1)
+ private static class NotContiguousSequence {
+
+ @OpenWireProperty(version = 1, sequence = 1)
+ protected int property1;
+
+ @OpenWireProperty(version = 1, sequence = 3)
+ protected int property2;
+
+ public int getProperty1() {
+ return property1;
+ }
+
+ public void setProperty1(int property1) {
+ this.property1 = property1;
+ }
+
+ public int getProperty2() {
+ return property2;
+ }
+
+ public void setProperty2(int property2) {
+ this.property2 = property2;
+ }
+ }
+}