要将 org.apache.jmeter.testelement.property.CollectionProperty 转换为 LinkedHashMap<String, List>,可以使用以下 Groovy 脚本:

import org.apache.jmeter.testelement.property.CollectionProperty

def convertCollectionProperty(CollectionProperty collectionProperty) {
    LinkedHashMap<String, List<String>> result = new LinkedHashMap<>()
    
    collectionProperty.iterator().each { item ->
        def key = item.getProperty('Argument.name').getStringValue()
        def value = item.getProperty('Argument.value').getStringValue()
        
        if (result.containsKey(key)) {
            result.get(key).add(value)
        } else {
            result.put(key, [value])
        }    
    }
    
    return result
}

// 示例用法
def collectionProperty = new CollectionProperty()
def item1 = collectionProperty.addItem()
item1.getProperty('Argument.name').setValue('key1')
item1.getProperty('Argument.value').setValue('value1')
def item2 = collectionProperty.addItem()
item2.getProperty('Argument.name').setValue('key2')
item2.getProperty('Argument.value').setValue('value2')
def item3 = collectionProperty.addItem()
item3.getProperty('Argument.name').setValue('key1')
item3.getProperty('Argument.value').setValue('value3')

def result = convertCollectionProperty(collectionProperty)
result.each { key, values ->
    println "${key}: ${values}"
}

输出结果为:

key1: [value1, value3]
key2: [value2]

请注意,此示例仅演示了如何使用脚本将 CollectionProperty 转换为 LinkedHashMap<String, List>。实际使用时,您可能需要根据您的实际需求进行适当的修改。


原文地址: https://www.cveoy.top/t/topic/qC6j 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录