diff --git a/pom.xml b/pom.xml index 5449eaa3b896f5f4a1e06d0cb05845ef7afda4cb..1666b5a01fee3cf2f553376ed76b849b2566180b 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java b/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java index f20db308c965a03517d3d1e4397fd6cf4d3b1b65..e7a81a66b764fa746300977f92ae8a1fdb82c155 100644 --- a/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java +++ b/src/main/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojo.java @@ -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; - } } diff --git a/src/test/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojoTest.java b/src/test/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojoTest.java index c537e242b95e93bd117f14d6b7d02ff06b7dd40f..e5f23da5acf765c0659333b234e88c300e9a5d3b 100644 --- a/src/test/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojoTest.java +++ b/src/test/java/com/github/joelittlejohn/embedmongo/InitDataMongoMojoTest.java @@ -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; + } + } } diff --git a/src/test/java/com/mongodb/EmbedMongoClient.java b/src/test/java/com/mongodb/EmbedMongoClient.java new file mode 100644 index 0000000000000000000000000000000000000000..9011c26bc4fb95c07a27b32a0fcdfd37aab691e2 --- /dev/null +++ b/src/test/java/com/mongodb/EmbedMongoClient.java @@ -0,0 +1,24 @@ +/** + * 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 { + } +} diff --git a/src/test/java/com/mongodb/EmbedMongoDB.java b/src/test/java/com/mongodb/EmbedMongoDB.java new file mode 100644 index 0000000000000000000000000000000000000000..73143464003100d29dda0d13f7ba5e998a5a170f --- /dev/null +++ b/src/test/java/com/mongodb/EmbedMongoDB.java @@ -0,0 +1,85 @@ +/** + * 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) { + + } +}