/*
 * Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb;

import org.junit.Test;

import java.net.UnknownHostException;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.hasItems;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;

public class MongoTest {

    @Test
    public void shouldDefaultToLocalhost() throws UnknownHostException {
        final Mongo mongo = new MongoClient();
        assertEquals(Arrays.asList(new ServerAddress()), mongo.getServerAddressList());
    }

    @Test
    public void shouldUseGivenHost() throws UnknownHostException {
        final Mongo mongo = new MongoClient("localhost");
        assertEquals(Arrays.asList(new ServerAddress("localhost")), mongo.getServerAddressList());
    }

    @Test
    public void shouldUseGivenServerAddress() throws UnknownHostException {
        final Mongo mongo = new MongoClient(new ServerAddress("localhost"));
        assertEquals(Arrays.asList(new ServerAddress("localhost")), mongo.getServerAddressList());
    }

    @Test
    public void shouldDefaultToPrimaryReadPreference() throws UnknownHostException {
        final Mongo mongo = new MongoClient();
        assertEquals(ReadPreference.primary(), mongo.getReadPreference());
    }

    @Test
    public void shouldSaveDefaultReadPreference() throws UnknownHostException {
        final Mongo mongo = new MongoClient();
        mongo.setReadPreference(ReadPreference.nearest());
        assertEquals(ReadPreference.nearest(), mongo.getReadPreference());
    }

    @Test
    public void shouldSaveDefaultWriteConcern() throws UnknownHostException {
        final Mongo mongo = new MongoClient();
        mongo.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        assertEquals(WriteConcern.ACKNOWLEDGED, mongo.getWriteConcern());
    }

    @Test
    public void shouldGetDB() throws UnknownHostException {
        final Mongo mongo = new MongoClient();
        final DB db = mongo.getDB("test");
        assertNotNull(db);
        assertEquals("test", db.getName());
    }

    @Test
    public void shouldGetSameDB() throws UnknownHostException {
        final Mongo mongo = new MongoClient();
        assertSame(mongo.getDB("test"), mongo.getDB("test"));
    }

    @Test
    public void shouldGetDatabaseNames() throws UnknownHostException {
        final Mongo mongo = new MongoClient();

        mongo.getDB("test1").getCollectionNames();
        mongo.getDB("test2").getCollectionNames();

        assertThat(mongo.getDatabaseNames(), hasItems("test1", "test2"));
    }
}

@@ -1103,32 +1103,50 @@
  MongoClient();%0A
+        try %7B%0A    
         assertEq
@@ -1209,32 +1209,89 @@
 AddressList());%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @Test
@@ -1412,24 +1412,42 @@
 ocalhost%22);%0A
+        try %7B%0A    
         asse
@@ -1525,32 +1525,89 @@
 AddressList());%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @Test
@@ -1756,24 +1756,42 @@
 calhost%22));%0A
+        try %7B%0A    
         asse
@@ -1873,24 +1873,81 @@
 essList());%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @
@@ -2075,32 +2075,50 @@
  MongoClient();%0A
+        try %7B%0A    
         assertEq
@@ -2168,32 +2168,89 @@
 dPreference());%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @Test
@@ -2645,32 +2645,50 @@
  MongoClient();%0A
+        try %7B%0A    
         mongo.se
@@ -2729,16 +2729,20 @@
 EDGED);%0A
+    
         
@@ -2803,24 +2803,81 @@
 Concern());%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @
@@ -2980,32 +2980,50 @@
  MongoClient();%0A
+        try %7B%0A    
         final DB
@@ -3045,24 +3045,28 @@
 DB(%22test%22);%0A
+    
         asse
@@ -3080,32 +3080,36 @@
 ll(db);%0A        
+    
 assertEquals(%22te
@@ -3124,24 +3124,81 @@
 getName());%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @
@@ -3305,32 +3305,50 @@
  MongoClient();%0A
+        try %7B%0A    
         assertSa
@@ -3389,24 +3389,81 @@
 B(%22test%22));%0A
+        %7D finally %7B%0A            mongo.close();%0A        %7D%0A
     %7D%0A%0A    @
@@ -3582,24 +3582,42 @@
 oClient();%0A%0A
+        try %7B%0A    
         mong
@@ -3655,32 +3655,36 @@
 ames();%0A        
+    
 mongo.getDB(%22tes
@@ -3715,24 +3715,28 @@
 );%0A%0A        
+ 
+   
 assertThat(m
@@ -3785,20 +3785,159 @@
  %22test2%22));%0A
+        %7D finally %7B%0A            mongo.dropDatabase(%22test1%22);%0A            mongo.dropDatabase(%22test2%22);%0A            mongo.close();%0A        %7D%0A
     %7D%0A%7D%0A

Test cleanup