Skip to content
Snippets Groups Projects
Commit 4ad70fc9 authored by Pierre-Jean Vardanega's avatar Pierre-Jean Vardanega
Browse files

Update mojo with feedbacks from #41

parent 4f592d5e
No related branches found
No related tags found
No related merge requests found
......@@ -116,6 +116,13 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
......
......@@ -43,7 +43,7 @@ public class InitDataMongoMojo extends AbstractMojo {
* @parameter
* @required
*/
private File dataFolder;
private File scriptsDirectory;
/**
* Optional. Specify the port only if you want have a different one from the default value.
......@@ -59,15 +59,24 @@ public class InitDataMongoMojo extends AbstractMojo {
*/
private String databaseName;
public InitDataMongoMojo() {
}
InitDataMongoMojo(File scriptsDirectory, int port, String databaseName) {
this.scriptsDirectory = scriptsDirectory;
this.port = port;
this.databaseName = databaseName;
}
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
DB db = getConnectToMongoAndGetDatabase();
DB db = connectToMongoAndGetDatabase();
if (dataFolder.isDirectory()) {
if (scriptsDirectory.isDirectory()) {
Scanner scanner = null;
StringBuilder instructions = new StringBuilder();
File[] files = dataFolder.listFiles();
getLog().info("Folder " + dataFolder.getAbsolutePath() + " contains " + files.length + " file(s):");
File[] files = scriptsDirectory.listFiles();
getLog().info("Folder " + scriptsDirectory.getAbsolutePath() + " contains " + files.length + " file(s):");
for (File file : files) {
try {
scanner = new Scanner(file);
......@@ -97,7 +106,7 @@ public class InitDataMongoMojo extends AbstractMojo {
}
}
private DB getConnectToMongoAndGetDatabase() throws MojoExecutionException {
DB connectToMongoAndGetDatabase() throws MojoExecutionException {
if (databaseName == null || databaseName.trim().length() == 0) {
throw new MojoExecutionException("Database name is missing");
}
......@@ -111,14 +120,4 @@ public class InitDataMongoMojo extends AbstractMojo {
getLog().info("Connected to MongoDB");
return mongoClient.getDB(databaseName);
}
protected InitDataMongoMojo setDataFolder(File dataFolder) {
this.dataFolder = dataFolder;
return this;
}
protected InitDataMongoMojo setDatabaseName(String databaseName) {
this.databaseName = databaseName;
return this;
}
}
......@@ -16,18 +16,28 @@
package com.github.joelittlejohn.embedmongo;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.UnknownHostException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.runners.MockitoJUnitRunner;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.EmbedMongoDB;
@RunWith(MockitoJUnitRunner.class)
public class InitDataMongoMojoTest {
@Rule
......@@ -36,37 +46,43 @@ public class InitDataMongoMojoTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final static int PORT = 27017;
private File rootFolder;
private File rootFolderWithError;
@Ignore("Need an instance of MongoDB to pass")
@Test public void
should_execute_instructions() throws MojoFailureException, MojoExecutionException, IOException {
initFolder();
try {
new InitDataMongoMojo().setDatabaseName("myDB").setDataFolder(rootFolder).execute();
new InitDataMongoMojoForTest(rootFolder, PORT, "myDB").execute();
} catch (Exception e) {
e.printStackTrace();
fail("Should not fail!");
}
}
@Test public void
should_fail_when_database_name_is_not_provided() throws MojoFailureException, MojoExecutionException {
should_fail_when_database_name_is_not_provided() throws MojoFailureException, MojoExecutionException, IOException {
initFolder();
thrown.expect(MojoExecutionException.class);
thrown.expectMessage("Database name is missing");
new InitDataMongoMojo().execute();
new InitDataMongoMojo(rootFolder, PORT, null).execute();
}
@Ignore("Need an instance of MongoDB to pass")
@Test public void
should_fail_to_execute_instruction_with_error() throws IOException, MojoFailureException, MojoExecutionException {
DB database = mock(DB.class);
initFolderWithError();
CommandResult result = new EmbedMongoDB("myDB").notOkErrorResult("Error while executing instructions from file '" + rootFolderWithError.listFiles()[0].getName());
given(database.doEval(anyString(), Matchers.<Object>anyVararg())).willReturn(result);
thrown.expect(MojoExecutionException.class);
thrown.expectMessage("Error while executing instructions from file '" + rootFolderWithError.listFiles()[0].getName());
new InitDataMongoMojo().setDatabaseName("myDB").setDataFolder(rootFolderWithError).execute();
new InitDataMongoMojoForTest(rootFolderWithError, PORT, "myDB", database).execute();
}
private void initFolder() throws IOException {
......@@ -87,16 +103,35 @@ public class InitDataMongoMojoTest {
private void initFolderWithError() throws IOException {
File instructionsFile = createSchemaFolder.newFile();
BufferedWriter out = null;
BufferedWriter reader = null;
try {
out = new BufferedWriter(new FileWriter(instructionsFile));
out.write("db.unknownInstruction();\n");
reader = new BufferedWriter(new FileWriter(instructionsFile));
reader.write("db.unknownInstruction();\n");
} finally {
if (out != null) {
out.close();
if (reader != null) {
reader.close();
}
}
rootFolderWithError = instructionsFile.getParentFile();
rootFolderWithError.mkdir();
}
static class InitDataMongoMojoForTest extends InitDataMongoMojo {
private final DB database;
public InitDataMongoMojoForTest(File dataFolder, int port, String databaseName) throws UnknownHostException {
this(dataFolder, port, databaseName, new EmbedMongoDB("myDB"));
}
public InitDataMongoMojoForTest(File dataFolder, int port, String databaseName, DB database) {
super(dataFolder, port, databaseName);
this.database = database;
}
@Override
DB connectToMongoAndGetDatabase() throws MojoExecutionException {
return database;
}
}
}
/**
* Copyright © 2012 Joe Littlejohn
*
* 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 java.net.UnknownHostException;
public class EmbedMongoClient extends MongoClient {
public EmbedMongoClient() throws UnknownHostException {
}
}
/**
* Copyright © 2012 Joe Littlejohn
*
* 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 java.net.UnknownHostException;
import java.util.Set;
public class EmbedMongoDB extends DB {
public EmbedMongoDB(String name) throws UnknownHostException {
super(new EmbedMongoClient(), name);
}
public CommandResult notOkErrorResult(String message) {
try {
CommandResult commandResult = new CommandResult(new ServerAddress("localhost"));
commandResult.put("errmsg", message);
commandResult.put("ok", 0);
return commandResult;
} catch (UnknownHostException e) {
return null;
}
}
@Override
public CommandResult doEval(String code, Object... args) {
CommandResult commandResult;
try {
commandResult = new CommandResult(new ServerAddress("localhost"));
commandResult.put("ok", 1.0);
commandResult.put("retval", "null");
} catch (UnknownHostException e) {
return notOkErrorResult(e.getMessage());
}
return commandResult;
}
@Override
public void requestStart() {
}
@Override
public void requestDone() {
}
@Override
public void requestEnsureConnection() {
}
@Override
protected DBCollection doGetCollection(String name) {
return null;
}
@Override
public Set<String> getCollectionNames() {
return null;
}
@Override
CommandResult doAuthenticate(MongoCredential credentials) {
return null;
}
@Override
public void cleanCursors(boolean force) {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment