diff --git a/.travis.yml b/.travis.yml index d9e866d..62d76e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: clojure sudo: required lein: lein +dist: xenial before_script: - - ./bin/ci/install_mongodb.sh - mongod --version - ./bin/ci/before_script.sh script: lein do clean, javac, test diff --git a/bin/ci/before_script.sh b/bin/ci/before_script.sh index 711d6dd..77f1a97 100755 --- a/bin/ci/before_script.sh +++ b/bin/ci/before_script.sh @@ -1,7 +1,7 @@ #!/bin/sh # MongoDB seems to need some time to boot first. MK. -sleep 5 +sleep 15 # MongoDB Java driver won't run authentication twice on the same DB instance, # so we need to use multiple DBs. diff --git a/src/clojure/monger/core.clj b/src/clojure/monger/core.clj index 880a259..e5314a0 100644 --- a/src/clojure/monger/core.clj +++ b/src/clojure/monger/core.clj @@ -233,10 +233,11 @@ Commonly used for PaaS-based applications, for example, running on Heroku. If username and password are provided, performs authentication." [^String uri-string] - (let [uri (MongoClientURI. uri-string) - conn (MongoClient. uri) - db (.getDB conn (.getDatabase uri))] - {:conn conn :db db})) + (let [uri (MongoClientURI. uri-string) + conn (MongoClient. uri)] + (if-let [dbName (.getDatabase uri)] + {:conn conn :db (.getDB conn dbName)} + (throw (IllegalArgumentException. "No database name specified in URI. Monger requires a database to be explicitly configured."))))) (defn ^com.mongodb.CommandResult command "Runs a database command (please check MongoDB documentation for the complete list of commands). diff --git a/test/monger/test/core_test.clj b/test/monger/test/core_test.clj index eeaa5b6..02f45dc 100644 --- a/test/monger/test/core_test.clj +++ b/test/monger/test/core_test.clj @@ -70,3 +70,7 @@ :cursor-finalizer-enabled true :required-replica-set-name "rs"}] (is (instance? com.mongodb.MongoClientOptions$Builder (mg/mongo-options-builder opts))))) + +(deftest connect-to-uri-without-db-name + (let [uri "mongodb://localhost:27017"] + (is (thrown? IllegalArgumentException (mg/connect-via-uri uri)))))