Permalink
Browse files

update documentation about the new automated port selection behaviour

  • Loading branch information...
1 parent 48c7f57 commit de82269a777eb7c8218d2c0573f7384fc60b92fc @thiloplanz committed Feb 26, 2013
View
@@ -6,17 +6,31 @@ This has the advantage that you can run your code (with the standard MongoDB Jav
Only very few commands and queries are implemented. This is very much a work in progress, and progress only happens on demand (which at the moment is just me when I need to improve test coverage of my projects).
-## Port 2307
+## Database server port
Early versions of jmockmongo listened on the same default port used
-by MongoDB (port 27017). That was convenient, because you did not have to configure the Mongo client being tested at all. However, it was also quite risky, because it made it easy for automated tests
+by MongoDB (port 27017). That was convenient, because you did not have to configure the Mongo client being tested at all.
+
+However, it was also quite risky, because it made it easy for automated tests
to accidentally connect to (and possibly damage) real databases,
especially since MongoDB allows connections without passwords,
usually listens (via mongos) on each application server's localhost,
and will serve queries against non-existing databases and collections
quite happily.
-So instead, jmockmongo now listens on port 2307.
+So instead, jmockmongo now selects any available server port and binds to that.
+You have to query it for which port that is after it has been started:
+
+ MockMongo mock = new MockMongo();
+ MongoURI uri = mock.getMongoURI();
+ // or
+ int port = mock.getPort();
+
+As an alternative, you can also explicitly specify any port you want:
+
+ MockMongo mock = new MockMongo(2307);
+
+
## Example
@@ -26,20 +40,22 @@ So instead, jmockmongo now listens on port 2307.
mock.start();
// use the normal MongoDB client to talk to it
- Mongo mongo =
- new Mongo("0.0.0.0", 2307);
- // or new Mongo(MockMongo.DEFAULT_URI);
-
+ Mongo mongo = new Mongo(mock.getMongoURI());
+
mongo.getDatabase("test").getCollection("test").insert(
new BasicDBObject("x", 1));
// shutdown (necessary to unbind the server port)
mock.stop();
+
+
+
+
### with JUnit
-If you are using JUnit, there is a helper TestCase base class that starts and stops the mock MongoDB in the setup and teardown methods, and has helpers to bootstrap test data and assert results.
+If you are using JUnit 3, there is a helper TestCase base class that starts and stops the mock MongoDB in the setup and teardown methods, and has helpers to bootstrap test data and assert results.
public class UpdateTest extends MockMongoTestCaseSupport {
@@ -18,11 +18,11 @@
package jmockmongo;
+import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
-import java.io.IOException;
import jmockmongo.commands.Count;
import jmockmongo.commands.DbStats;
@@ -47,25 +47,28 @@
import com.mongodb.MongoURI;
+/**
+ *
+ * @author Thilo Planz
+ * @author Julien Polo
+ *
+ */
+
public class MockMongo {
/**
* Utility method to find a free port
*/
- public static int anyPort() {
- try {
- ServerSocket socket = new ServerSocket(0);
- int port = socket.getLocalPort();
- socket.close();
- return port;
- } catch (IOException e) {
- return 2307;
- }
+ static int anyPort() throws IOException {
+ ServerSocket socket = new ServerSocket(0);
+ int port = socket.getLocalPort();
+ socket.close();
+ return port;
}
- private int port;
+ private final int port;
- private MongoURI uri;
+ private final MongoURI uri;
private ChannelGroup channels;
@@ -74,19 +77,24 @@ public static int anyPort() {
private ConcurrentHashMap<String, MockDB> data;
MockMongo() {
- this(MockMongo.anyPort());
+ try {
+ this.port = MockMongo.anyPort();
+ this.uri = new MongoURI("mongodb://0.0.0.0:" + port);
+ } catch (IOException e) {
+ throw new RuntimeException("failed to find a free port", e);
+ }
}
MockMongo(int port) {
- this.port = port;
- this.uri = new MongoURI("mongodb://0.0.0.0:" + port);
+ this.port = port;
+ this.uri = new MongoURI("mongodb://0.0.0.0:" + port);
}
- int getPort() {
+ public int getPort() {
return port;
}
- MongoURI getMongoURI() {
+ public MongoURI getMongoURI() {
return uri;
}
@@ -122,8 +130,6 @@ MockDBCollection getCollection(String database, String collectionName) {
return db.getCollection(collectionName);
}
-
-
public void start() {
data = new ConcurrentHashMap<String, MockDB>();
final ChannelFactory factory = new NioServerSocketChannelFactory(
@@ -29,6 +29,13 @@
import com.mongodb.Mongo;
import com.mongodb.ServerAddress;
+/**
+ *
+ * @author Thilo Planz
+ * @author Julien Polo
+ *
+ */
+
public abstract class MockMongoTestCaseSupport extends TestCase {
private MockMongo mockMongo;
@@ -39,17 +46,17 @@
protected void setUp() throws Exception {
// make sure we cannot connect before this
// (this could mean a real server is running on our port)
-
+
mockMongo = new MockMongo();
-
+
try {
new DBPort(new ServerAddress("0.0.0.0", mockMongo.getPort()))
.ensureOpen();
fail("something is already listening at the Mongo port! Is a real mongo process running?");
} catch (IOException e) {
}
mockMongo.start();
-
+
mongo = new Mongo(mockMongo.getMongoURI());
}
@@ -29,6 +29,13 @@
import com.mongodb.Mongo;
import com.mongodb.MongoException;
+/**
+ *
+ * @author Thilo Planz
+ * @author Julien Polo
+ *
+ */
+
public class MockMongoTest extends TestCase {
public void testStartStop() throws UnknownHostException, MongoException,

0 comments on commit de82269

Please sign in to comment.