|
| 1 | +package edu.harvard.iq.dataverse.util.bagit; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.lang.reflect.InvocationTargetException; |
| 6 | +import java.lang.reflect.Method; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests adapted for DD-2093: verify the behavior of BagGenerator.multilineWrap. |
| 13 | + */ |
| 14 | +public class BagGeneratorMultilineWrapTest { |
| 15 | + |
| 16 | + private static Method multilineWrap; |
| 17 | + |
| 18 | + @BeforeAll |
| 19 | + static void setUp() throws NoSuchMethodException { |
| 20 | + // Access the private static method via reflection |
| 21 | + multilineWrap = BagGenerator.class.getDeclaredMethod("multilineWrap", String.class); |
| 22 | + multilineWrap.setAccessible(true); |
| 23 | + } |
| 24 | + |
| 25 | + private String callMultilineWrap(String input) { |
| 26 | + try { |
| 27 | + return (String) multilineWrap.invoke(null, input); |
| 28 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 29 | + throw new RuntimeException(e); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void shortLine_noWrap() { |
| 35 | + String input = "Hello world"; |
| 36 | + String out = callMultilineWrap(input); |
| 37 | + assertThat(out).isEqualTo("Hello world"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void exactBoundary_78chars_noWrap() { |
| 42 | + String input = repeat('a', 78); |
| 43 | + String out = callMultilineWrap(input); |
| 44 | + assertThat(out).isEqualTo(input); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void longSingleWord_wrapsAt78WithIndent() { |
| 49 | + String input = repeat('a', 100); |
| 50 | + String expected = repeat('a', 78) + "\r\n " + repeat('a', 22); |
| 51 | + String out = callMultilineWrap(input); |
| 52 | + assertThat(out).isEqualTo(expected); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void multiline_input_indentsSecondAndSubsequentOriginalLines() { |
| 57 | + String input = "Line1\nLine2"; |
| 58 | + String expected = "Line1\r\n Line2"; |
| 59 | + String out = callMultilineWrap(input); |
| 60 | + assertThat(out).isEqualTo(expected); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void multiline_withCRLF_normalizedAndIndented() { |
| 65 | + String input = "First line\r\nSecond line"; |
| 66 | + String expected = "First line\r\n Second line"; |
| 67 | + String out = callMultilineWrap(input); |
| 68 | + assertThat(out).isEqualTo(expected); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void emptyLines_trimmedAndSkipped() { |
| 73 | + String input = "Line1\n\nLine3"; |
| 74 | + String expected = "Line1\r\n Line3"; |
| 75 | + String out = callMultilineWrap(input); |
| 76 | + assertThat(out).isEqualTo(expected); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void whitespaceOnlyLines_ignored() { |
| 81 | + String input = "Line1\n \n\t\t\nLine3"; |
| 82 | + String expected = "Line1\r\n Line3"; |
| 83 | + String out = callMultilineWrap(input); |
| 84 | + assertThat(out).isEqualTo(expected); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void longSecondLine_preservesIndentOnWraps() { |
| 89 | + String line1 = "Header"; |
| 90 | + String line2 = repeat('b', 90); |
| 91 | + String input = line1 + "\n" + line2; |
| 92 | + String expected = "Header\r\n " + repeat('b', 78) + "\r\n " + repeat('b', 12); |
| 93 | + String out = callMultilineWrap(input); |
| 94 | + assertThat(out).isEqualTo(expected); |
| 95 | + } |
| 96 | + |
| 97 | + private static String repeat(char c, int n) { |
| 98 | + StringBuilder sb = new StringBuilder(n); |
| 99 | + for (int i = 0; i < n; i++) sb.append(c); |
| 100 | + return sb.toString(); |
| 101 | + } |
| 102 | +} |
0 commit comments