Simplify MongoDbRule configuration #49

Closed
joel-costigliola opened this Issue Jan 26, 2013 · 2 comments

Comments

Projects
None yet
2 participants

I decided to change the port of my ManagedMongoDb in my tests, when I ran the tests, it failed because MongoDbRule was looking for a database on the default port.

@ClassRule
public static ManagedMongoDb managedMongoDb = newManagedMongoDbRule()
                  .mongodPath("/home/joe/prog/mongo/mongodb")
                  .appendSingleCommandLineArguments("--nojournal")
                  .port(LOCAL_DB_TEST_PORT) // CHANGING DB PORT
                  .build();
@Rule 
public MongoDbRule remoteMongoDbRule = 
       newMongoDbRule().defaultManagedMongoDb(ZORGLUX_TEST_DB);

The test errors were confusing because I thought MongoDbRule would had used LOCAL_DB_TEST_PORT but it wasn't the case.
I solved the problem by finely configuring MongoDbRule as shown below :

@Rule
public MongoDbRule remoteMongoDbRule = newMongoDbRule().configure( 
  mongoDb().port(LOCAL_DB_TEST_PORT).databaseName(ZORGLUX_TEST_DB).build()
 ).build();

I think MongoDbRule should use the same port than ManagedMongoDb, I feel I should not had to worry on database port as soon as I had configured it on ManagedMongoDb (unless I'm missing something here).

If it's not easy to share port configuration, another option would be to add a parameter to defaultManagedMongoDb, something like :

newMongoDbRule().defaultManagedMongoDb(ZORGLUX_TEST_DB, port);

Cheers,

Joel

Owner

lordofthejars commented Jan 27, 2013

Hi Joel, I questioned the same topic here, and finally I decided to split them in two rules, there were many reasons against and in favor, but finally I decided to create a default method for retrieving a MongoDbRule when no changes were made, or if we change some configuration in Managed rule (like the port) then you should explicitly specify by using configure method. Keep in mind that for example next version, I will have support for testing replication (Shards and replicaset) and then the concept of port is different because instead of having one ManagedMongoDb port, we will have one for each shard/replica server. Moreover in near future I will add support for Arquillian, and then the lifecycle management will be configured by using xml file and managed inside Arquillian. But apart from these reasons, for MongoDB is easy to set the port, because it is a parameter of executable, there are other engines which port is set by using a configuration file, so it is not always as direct as MongoDB to know all the information. On the other hand (I think) that there is no way to communicate directly a @classrule to a @rule (I think there is no context information like for example ServletContext), but of course it could be self implemented as a wrapper, but at the end I decided to not implement in this way and maintaining both of them separated.

For example, imagine next example:

@ClassRule
public static ManagedMongoDb managedMongoDb1 = newManagedMongoDbRule()
                  .mongodPath("/home/joe/prog/mongo/mongodb")
                  .appendSingleCommandLineArguments("--nojournal")
                  .port(27017) // CHANGING DB PORT
                  .build();

@ClassRule
public static ManagedMongoDb managedMongoDb = newManagedMongoDbRule()
                  .mongodPath("/home/joe/prog/mongo/mongodb")
                  .appendSingleCommandLineArguments("--nojournal")
                  .port(27018) // CHANGING DB PORT
                  .build();

You should configure explicitly the MongoDB port you want to populate the data.

As you can see although what you are pointing in the issue are completely right, I decided to maintain independently the lifecycle rules from connection rules. But of course I will consider that option again in the future.

About adding port attribute in defaultManagedMongoDb could be another solution that is valid, and thank you very much for suggesting it. I think that next version will implement this method too because it is a clean and fast solution without implying a lot of changes.

Thank you very much again for helping improve NoSQLUnit, I always take in consideration the opinions of users, and of course if you have any other suggestion or an idea for this issue, you can share without any problem

Cheers,
Alex.

Ok, I get your points.

Having a simple way to define port with defaultManagedMongoDb is fine to me, I would just add that it would be worth adding a note in the documentation telling that ManagedMongoDb and MongoDbRule don't share their configuration.

Keep the good work !

Joel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment