package org.inferred.cjp39.j8stages;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import org.junit.Test;

public class ExceptionClonerTest {

    private static final class MyUnfriendlyException extends Exception {
        private MyUnfriendlyException(String message, Throwable cause) {
            super(message, cause);
        }
    }

    @Test
    public void clone_matches_original() {
        MyUnfriendlyException x =
                new MyUnfriendlyException("Clone this, baby!", new RuntimeException("cause"));
        MyUnfriendlyException clone = ExceptionCloner.clone(x);
        assertEquals(x.getMessage(), clone.getMessage());
        assertSame(x.getCause(), clone.getCause());
    }

    @Test
    public void modifying_clone_does_not_modify_original() {
        MyUnfriendlyException x =
                new MyUnfriendlyException("Clone this, baby!", new RuntimeException("cause"));
        MyUnfriendlyException clone = ExceptionCloner.clone(x);
        clone.addSuppressed(new RuntimeException("dormouse"));
        assertEquals(0, x.getSuppressed().length);
    }
}

@@ -1124,11 +1124,714 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void suppressed_array_not_aliased() %7B%0A        MyUnfriendlyException x =%0A                new MyUnfriendlyException(%22Clone this, baby!%22, new RuntimeException(%22cause%22));%0A        x.addSuppressed(new RuntimeException(%22Help, help, I'm being repressed!%22));%0A        MyUnfriendlyException clone = ExceptionCloner.clone(x);%0A        clone.addSuppressed(new RuntimeException(%22dormouse%22));%0A        MyUnfriendlyException clone2 = ExceptionCloner.clone(clone);%0A        clone2.addSuppressed(new RuntimeException(%22in a teapot%22));%0A        assertEquals(3, clone2.getSuppressed().length);%0A        assertEquals(2, clone.getSuppressed().length);%0A        assertEquals(1, x.getSuppressed().length);%0A    %7D
 %0A%7D%0A

Add a test to ensure the list of suppressed exceptions in a Throwable is not accidentally aliased during the shallow copy.