You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by vl...@apache.org on 2023/06/16 06:29:02 UTC

[jmeter] branch master updated: fix: support unmodifiable collections as input for CollectionProperty

This is an automated email from the ASF dual-hosted git repository.

vladimirsitnikov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 049697b00b fix: support unmodifiable collections as input for CollectionProperty
049697b00b is described below

commit 049697b00b2b37e212cb8beb274bc78808964529
Author: Vladimir Sitnikov <si...@gmail.com>
AuthorDate: Fri Jun 16 09:28:50 2023 +0300

    fix: support unmodifiable collections as input for CollectionProperty
    
    Previously, CollectionProperty.normalizeCollection failed to create a copy
    of the input collection, and it resulted in null value.
    
    How we create ArrayList or LinkedHashSet instead.
---
 .../testelement/property/AbstractProperty.java     | 21 +++++++++++--
 .../testelement/property/CollectionPropertyTest.kt | 34 ++++++++++++++++++++++
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java b/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java
index 0b0ff4ef6c..5c6fcb8dc1 100644
--- a/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java
+++ b/src/core/src/main/java/org/apache/jmeter/testelement/property/AbstractProperty.java
@@ -17,8 +17,12 @@
 
 package org.apache.jmeter.testelement.property;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.jmeter.testelement.TestElement;
 import org.slf4j.Logger;
@@ -296,13 +300,26 @@ public abstract class AbstractProperty implements JMeterProperty {
      */
     protected Collection<JMeterProperty> normalizeList(Collection<?> coll) {
         try {
-            @SuppressWarnings("unchecked") // empty collection
-            Collection<JMeterProperty> newColl = coll.getClass().getDeclaredConstructor().newInstance();
+            Collection<JMeterProperty> newColl;
+            try {
+                @SuppressWarnings("unchecked")
+                Collection<JMeterProperty> tmp = coll.getClass().getDeclaredConstructor().newInstance();
+                newColl = tmp;
+            } catch (Exception e) {
+                if (coll instanceof List) {
+                    newColl = new ArrayList<>(coll.size());
+                } else if (coll instanceof Set) {
+                    newColl = new LinkedHashSet<>();
+                } else {
+                    throw e;
+                }
+            }
             for (Object item : coll) {
                 newColl.add(convertObject(item));
             }
             return newColl;
         } catch (Exception e) {// should not happen
+            // TODO: replace with throwing an error, however it might break backward compatibility
             log.error("Cannot create copy of {}", coll.getClass(), e);
             return null;
         }
diff --git a/src/core/src/test/java/org/apache/jmeter/testelement/property/CollectionPropertyTest.kt b/src/core/src/test/java/org/apache/jmeter/testelement/property/CollectionPropertyTest.kt
new file mode 100644
index 0000000000..57d6c73b7c
--- /dev/null
+++ b/src/core/src/test/java/org/apache/jmeter/testelement/property/CollectionPropertyTest.kt
@@ -0,0 +1,34 @@
+/*
+ * 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.jmeter.testelement.property
+
+import org.junit.jupiter.api.Assertions
+import org.junit.jupiter.api.Test
+
+class CollectionPropertyTest {
+    @Test
+    fun `creates with immutable list`() {
+        val inputValues = listOf(1, 2)
+        val prop = CollectionProperty("property_name", inputValues)
+        Assertions.assertEquals(
+            inputValues.toString(),
+            (prop.objectValue as? List<*>).toString(),
+            "CollectionProperty should contain the same elements as in the input"
+        )
+    }
+}