package com.couchbase.lite;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class ReplicatorOfflineTest extends BaseReplicatorTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void testEditReadOnlyConfiguration() throws Exception {
        Endpoint endpoint = getRemoteTargetEndpoint();
        ReplicatorConfiguration config = makeConfig(
                true,
                false,
                true,
                endpoint
        );
        config.setContinuous(false);
        repl = new Replicator(config);

        thrown.expect(IllegalStateException.class);
        repl.getConfig().setContinuous(true);
    }

    @Test
    public void testStopReplicatorAfterOffline() throws URISyntaxException, InterruptedException {
        URLEndpoint target = new URLEndpoint(new URI("ws://foo.couchbase.com/db"));
        ReplicatorConfiguration config = makeConfig(false, true, true, db, target);
        Replicator repl = new Replicator(config);
        final CountDownLatch offline = new CountDownLatch(1);
        final CountDownLatch stopped = new CountDownLatch(1);
        ListenerToken token = repl.addChangeListener(executor, new ReplicatorChangeListener() {
            @Override
            public void changed(ReplicatorChange change) {
                Replicator.Status status = change.getStatus();
                if (status.getActivityLevel() == Replicator.ActivityLevel.OFFLINE) {
                    change.getReplicator().stop();
                    offline.countDown();
                }
                if (status.getActivityLevel() == Replicator.ActivityLevel.STOPPED) {
                    stopped.countDown();
                }
            }
        });
        repl.start();
        assertTrue(offline.await(10, TimeUnit.SECONDS));
        assertTrue(stopped.await(10, TimeUnit.SECONDS));
        repl.removeChangeListener(token);
    }

    @Test
    public void testChangeListenerEmptyArg() throws Exception {
        Endpoint endpoint = getRemoteTargetEndpoint();
        Replicator repl = new Replicator(makeConfig(
                true,
                false,
                true,
                endpoint
        ));

        thrown.expect(IllegalArgumentException.class);
        repl.addChangeListener(null);

        thrown.expect(IllegalArgumentException.class);
        repl.addChangeListener(executor, null);
    }

    private URLEndpoint getRemoteTargetEndpoint() throws URISyntaxException {
        URI uri = new URI("ws://10.0.2.2:4984/this_is_only_for_test");
        return new URLEndpoint(uri);
    }
}

@@ -1066,19 +1066,16 @@
         
-URL
 Endpoint
@@ -1088,60 +1088,32 @@
 t = 
-new URLEndpoint(new URI(%22ws://foo.couchbase.com/db%22)
+getRemoteTargetEndpoint(
 );%0A 
@@ -2151,32 +2151,962 @@
 (token);%0A    %7D%0A%0A
+    @Test%0A    public void testStartSingleShotReplicatorInOffline() throws URISyntaxException, InterruptedException %7B%0A        Endpoint endpoint = getRemoteTargetEndpoint();%0A        Replicator repl = new Replicator(makeConfig(%0A                true,%0A                false,%0A                false,%0A                endpoint%0A        ));%0A        final CountDownLatch stopped = new CountDownLatch(1);%0A        ListenerToken token = repl.addChangeListener(executor, new ReplicatorChangeListener() %7B%0A            @Override%0A            public void changed(ReplicatorChange change) %7B%0A                Replicator.Status status = change.getStatus();%0A                if (status.getActivityLevel() == Replicator.ActivityLevel.STOPPED) %7B%0A                    stopped.countDown();%0A                %7D%0A            %7D%0A        %7D);%0A        repl.start();%0A        assertTrue(stopped.await(10, TimeUnit.SECONDS));%0A        repl.removeChangeListener(token);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -3670,105 +3670,67 @@
     
-URI uri = new URI(%22ws://10.0.2.2:4984/this_is_only_for_test%22);%0A        return new URLEndpoint(uri
+return new URLEndpoint(new URI(%22ws://foo.couchbase.com/db%22)
 );%0A 

check offline single shot replicator (#1862)