CopyPastor

Detecting plagiarism made easy.

Score: 1.821859359741211; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2025-04-16
by Abhijit Sarkar

Original Post

Original - Posted on 2025-04-16
by Abhijit Sarkar



            
Present in both answers; Present only in the new answer; Present only in the old answer;

With a little ingenuity, we can convert to almost any iterable with full generic support, including primitive and wrapper type arrays.
I use the following implementation and let [jackson-databind](https://github.com/FasterXML/jackson-databind) do all the heavy lifting. It has the added advantage that I get to use the familiar notation of JSON list in my tests instead of some homegrown weirdity.
Note that the following code uses constructs from Java 17, such as [Text Blocks](https://docs.oracle.com/en/java/javase/17/text-blocks/index.html) and [Pattern Matching for instanceof](https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof.html).
**Converter:**
```java package junit;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.params.converter.ArgumentConversionException; import org.junit.jupiter.params.converter.SimpleArgumentConverter;
public class IterableConverter extends SimpleArgumentConverter { private static final ObjectMapper MAPPER = new ObjectMapper();
@Override protected Object convert(Object source, Class<?> targetType) throws ArgumentConversionException { if (source instanceof String s) { try { return MAPPER.readValue(s, targetType); } catch (JsonProcessingException jpe) { throw new ArgumentConversionException(jpe.getMessage(), jpe); } } else { throw new ArgumentConversionException("Can only convert from String"); } } } ```
**Test:**
```java import junit.IterableConverter; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.converter.ConvertWith; import org.junit.jupiter.params.provider.CsvSource;
class MyTests { @ParameterizedTest @CsvSource( delimiter = '|', textBlock = """ [5,7,7,8,8,10] | 8 | [3, 4] [5,7,7,8,8,10] | 6 | [-1,-1] [] | 0 | [-1,-1] [1] | 0 | [-1,-1] """) public void testStuff( @ConvertWith(IterableConverter.class) int[] nums, int target, @ConvertWith(IterableConverter.class) int[] expected) { // test code; } }

You didn't show how you intend to represent the list as a test parameter. While the solution provided by @m-justin works for a list, there's no reason to confine yourself to a list; with a little ingenuity, we can convert to arrays as well, including primitive and wrapper types.
I use the following implementation and let [jackson-databind](https://github.com/FasterXML/jackson-databind) do all the heavy lifting. It has the added advantage that I get to use the familiar notation of JSON list in my tests instead of some homegrown weirdity.
Note that the following code uses constructs from Java 17, such as [Text Blocks](https://docs.oracle.com/en/java/javase/17/text-blocks/index.html) and [Pattern Matching for instanceof](https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof.html).
**Converter:**
```java package junit;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.params.converter.ArgumentConversionException; import org.junit.jupiter.params.converter.SimpleArgumentConverter;
public class IterableConverter extends SimpleArgumentConverter { private static final ObjectMapper MAPPER = new ObjectMapper();
@Override protected Object convert(Object source, Class<?> targetType) throws ArgumentConversionException { if (source instanceof String s) { try { return MAPPER.readValue(s, targetType); } catch (JsonProcessingException jpe) { throw new ArgumentConversionException(jpe.getMessage(), jpe); } } else { throw new ArgumentConversionException("Can only convert from String"); } } } ```
**Test:**
```java import junit.IterableConverter; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.converter.ConvertWith; import org.junit.jupiter.params.provider.CsvSource;
class MyTests { @ParameterizedTest @CsvSource( delimiter = '|', textBlock = """ [5,7,7,8,8,10] | 8 | [3, 4] [5,7,7,8,8,10] | 6 | [-1,-1] [] | 0 | [-1,-1] [1] | 0 | [-1,-1] """) public void testStuff( @ConvertWith(IterableConverter.class) int[] nums, int target, @ConvertWith(IterableConverter.class) int[] expected) { // test code; } }


        
Present in both answers; Present only in the new answer; Present only in the old answer;