package de.bwaldvogel.mongo;

import static org.junit.Assert.fail;

import java.net.InetSocketAddress;

import org.junit.Test;

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;

public class MongoServerTest {

    @Test(timeout = 5000)
    public void testStopListenting() {
        MongoServer server = new MongoServer();
        MongoClient client = null;
        try {
            InetSocketAddress serverAddress = server.bind();
            client = new MongoClient(new ServerAddress(serverAddress));
            // request something
            client.getDB("admin").command("serverStatus");

            server.stopListenting();

            // existing clients must still work
            client.getDB("admin").command("serverStatus");

            // new clients must fail
            client.close();
            try {
                client = new MongoClient(new ServerAddress(serverAddress));
                client.getDB("admin").command("serverStatus");
                fail("MongoException expected");
            } catch (MongoException e) {
                // expected
            }

        } finally {
            if (client != null) {
                client.close();
            }
            server.shutdownNow();
        }
    }

    @Test(timeout = 1000)
    public void testShutdownNow() {
        MongoServer server = new MongoServer();
        MongoClient client = null;
        InetSocketAddress serverAddress = server.bind();
        client = new MongoClient(new ServerAddress(serverAddress));

        // request something to open a connection
        client.getDB("admin").command("serverStatus");

        server.shutdownNow();
    }
}

@@ -632,34 +632,49 @@
 (%22serverStatus%22)
+.throwOnError()
 ;%0A
-
 %0A            ser
@@ -793,32 +793,47 @@
 (%22serverStatus%22)
+.throwOnError()
 ;%0A%0A            /
@@ -1646,16 +1646,16 @@
 nection%0A
-
         
@@ -1699,16 +1699,792 @@
 Status%22)
+.throwOnError();%0A%0A        server.shutdownNow();%0A    %7D%0A%0A    @Test(timeout = 1000)%0A    public void testShutdownAndRestart() %7B%0A        MongoServer server = new MongoServer();%0A        MongoClient client = null;%0A        InetSocketAddress serverAddress = server.bind();%0A        client = new MongoClient(new ServerAddress(serverAddress));%0A%0A        // request something to open a connection%0A        client.getDB(%22admin%22).command(%22serverStatus%22).throwOnError();%0A%0A        server.shutdownNow();%0A%0A        try %7B%0A            client.getDB(%22admin%22).command(%22serverStatus%22);%0A            fail(%22MongoException expected%22);%0A        %7D catch (MongoException e) %7B%0A            // okay%0A        %7D%0A%0A        server.bind(serverAddress);%0A%0A        client.getDB(%22admin%22).command(%22serverStatus%22).throwOnError()
 ;%0A%0A     

improve unit tests