package com.bottlerocketstudios.continuity;

import android.support.test.runner.AndroidJUnit4;

import com.bottlerocketstudios.continuity.model.ContinuousTestClass;
import com.bottlerocketstudios.continuity.model.TestAnchor;
import com.bottlerocketstudios.continuity.util.SequentialNumberGenerator;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created on 8/23/16.
 */
@RunWith(AndroidJUnit4.class)
public class ManualRemovalTest extends ContinuityTest {

    @Test
    public void testManualRemoval() {
        int taskId = SequentialNumberGenerator.generateNumber();
        TestAnchor testAnchor = new TestAnchor();

        ContinuousTestClass beforeRemoval = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();

        getContinuityRepository().with(new TestAnchor(), ContinuousTestClass.class).task(taskId).remove();
        Assert.assertTrue("Instance was not notified of discard", beforeRemoval.isDiscarded());

        ContinuousTestClass afterRemoval = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();

        Assert.assertNotEquals("Instance was retained after removal", beforeRemoval, afterRemoval);
    }

    @Test
    public void testDestroyAfterManualRemoval() {
        int taskId = SequentialNumberGenerator.generateNumber();
        TestAnchor testAnchor = new TestAnchor();

        ContinuousTestClass beforeRemoval = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();

        getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).remove();
        //This just ensures that even if the testAnchor->List<ContainerId> exists without a corresponding ContainerId->ContinuousObject, there is no issue.
        getContinuityRepository().onDestroy(testAnchor);
        Assert.assertFalse("This shoud not have been notified of destruction.", beforeRemoval.isDestroyed());

        ContinuousTestClass afterRemoval = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();

        Assert.assertNotEquals("Instance was retained after removal", beforeRemoval, afterRemoval);
    }

    @Test
    public void testDestroyThenRemove() {
        int taskId = SequentialNumberGenerator.generateNumber();
        TestAnchor testAnchor = new TestAnchor();

        ContinuousTestClass beforeRemoval = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();

        getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).destroyThenRemove();
        Assert.assertTrue("Object was not notified of destruction.", beforeRemoval.isDestroyed());
        Assert.assertTrue("Object was not notified of discard.", beforeRemoval.isDiscarded());

        ContinuousTestClass afterRemoval = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();

        Assert.assertNotEquals("Instance was retained after removal", beforeRemoval, afterRemoval);
    }
}

@@ -218,16 +218,73 @@
 Anchor;%0A
+import com.bottlerocketstudios.continuity.util.SafeWait;%0A
 import c
@@ -3132,11 +3132,1865 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testDestroyThenRemoveBeforeCreation() %7B%0A        int taskId = SequentialNumberGenerator.generateNumber();%0A        //Verify that this does not crash the app. Should effectively be a noop.%0A        getContinuityRepository().with(new TestAnchor(), ContinuousTestClass.class).task(taskId).destroyThenRemove();%0A    %7D%0A%0A    @Test%0A    public void testDestroyThenRemoveRetentionOfSecondObject() %7B%0A        int taskId = SequentialNumberGenerator.generateNumber();%0A        TestAnchor testAnchor = new TestAnchor();%0A%0A        //Attach two objects to the same anchor%0A        ContinuousTestClass objectA1 = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();%0A        SimpleTestClass objectB1 = getContinuityRepository().with(testAnchor, SimpleTestClass.class).task(taskId).build();%0A%0A        getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).destroyThenRemove();%0A        Assert.assertTrue(%22Object was not notified of destruction.%22, objectA1.isDestroyed());%0A        Assert.assertTrue(%22Object was not notified of discard.%22, objectA1.isDiscarded());%0A%0A        ContinuousTestClass objectA2 = getContinuityRepository().with(testAnchor, ContinuousTestClass.class).task(taskId).build();%0A        Assert.assertNotEquals(%22First object was incorrectly retained after manual removal%22, objectA1, objectA2);%0A%0A        SafeWait.safeWait(ContinuityRepository.DEFAULT_LIFETIME_MS + ContinuityRepository.DEFAULT_CHECK_INTERVAL_MS * 2);%0A%0A        SimpleTestClass objectB2 = getContinuityRepository().with(testAnchor, SimpleTestClass.class).task(taskId).build();%0A        Assert.assertEquals(%22Second object was not retained when first object was removed%22, objectB1, objectB2);%0A    %7D%0A%0A    public static class SimpleTestClass %7B%0A%0A        public SimpleTestClass() %7B%0A            //NOOP%0A        %7D%0A    %7D
 %0A%7D%0A

Add test for one to many with manual destroy then remove operation on single ContinuousObject.