-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from jmcgarr/fix/issue-314
Improve testability of Main.java while fixing issue #314
- Loading branch information
Showing
5 changed files
with
223 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,38 @@ | |||
package org.jbake.launcher; | |||
|
|||
import org.apache.commons.configuration.CompositeConfiguration; | |||
import org.apache.commons.vfs2.FileObject; | |||
import org.apache.commons.vfs2.FileSystemException; | |||
import org.apache.commons.vfs2.FileSystemManager; | |||
import org.apache.commons.vfs2.VFS; | |||
import org.apache.commons.vfs2.impl.DefaultFileMonitor; | |||
import org.jbake.app.ConfigUtil; | |||
|
|||
/** | |||
* Delegate responsible for watching the file system for changes. | |||
* | |||
* @author jmcgarr@gmail.com | |||
*/ | |||
public class BakeWatcher { | |||
|
|||
/** | |||
* Starts watching the file system for changes to trigger a bake. | |||
* | |||
* @param res Commandline options | |||
* @param config Configuration settings | |||
*/ | |||
public void start(final LaunchOptions res, CompositeConfiguration config) { | |||
try { | |||
FileSystemManager fsMan = VFS.getManager(); | |||
FileObject listenPath = fsMan.resolveFile(res.getSource(), config.getString( ConfigUtil.Keys.CONTENT_FOLDER)); | |||
|
|||
System.out.println("Watching for changes in [" + res.getSource() + "]"); | |||
DefaultFileMonitor monitor = new DefaultFileMonitor(new CustomFSChangeListener(res, config)); | |||
monitor.setRecursive(true); | |||
monitor.addFile(listenPath); | |||
monitor.start(); | |||
} catch (FileSystemException e) { | |||
e.printStackTrace(); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
package org.jbake.launcher; | |||
|
|||
import org.apache.commons.configuration.CompositeConfiguration; | |||
import org.jbake.app.JBakeException; | |||
import org.jbake.app.Oven; | |||
|
|||
import java.text.MessageFormat; | |||
import java.util.List; | |||
|
|||
/** | |||
* Delegate class responsible for launching a Bake. | |||
* | |||
* @author jmcgarr@gmail.com | |||
*/ | |||
public class Baker { | |||
|
|||
public void bake(final LaunchOptions options, final CompositeConfiguration config) { | |||
final Oven oven = new Oven(options.getSource(), options.getDestination(), config, options.isClearCache()); | |||
oven.setupPaths(); | |||
oven.bake(); | |||
|
|||
final List<Throwable> errors = oven.getErrors(); | |||
if (!errors.isEmpty()) { | |||
final StringBuilder msg = new StringBuilder(); | |||
// TODO: decide, if we want the all errors here | |||
msg.append( MessageFormat.format("JBake failed with {0} errors:\n", errors.size())); | |||
int errNr = 1; | |||
for (final Throwable error : errors) { | |||
msg.append(MessageFormat.format("{0}. {1}\n", errNr, error.getMessage())); | |||
++errNr; | |||
} | |||
throw new JBakeException(msg.toString(), errors.get(0)); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,105 @@ | |||
package org.jbake.launcher; | |||
|
|||
import org.apache.commons.configuration.CompositeConfiguration; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import org.junit.runner.RunWith; | |||
import org.kohsuke.args4j.CmdLineException; | |||
import org.kohsuke.args4j.CmdLineParser; | |||
import org.mockito.Mock; | |||
import org.mockito.runners.MockitoJUnitRunner; | |||
|
|||
import java.util.HashMap; | |||
import java.util.Iterator; | |||
import java.util.Map; | |||
|
|||
import static org.mockito.Mockito.verify; | |||
|
|||
@RunWith( MockitoJUnitRunner.class ) | |||
public class MainTest { | |||
|
|||
private Main main; | |||
|
|||
@Mock private Baker mockBaker; | |||
@Mock private JettyServer mockJetty; | |||
@Mock private BakeWatcher mockWatcher; | |||
|
|||
@Before | |||
public void setup() { | |||
this.main = new Main(mockBaker, mockJetty, mockWatcher); | |||
} | |||
|
|||
@Test | |||
public void launchJetty() { | |||
String[] args = {"-s"}; | |||
main.run(args); | |||
|
|||
verify(mockJetty).run("output","8820"); | |||
} | |||
|
|||
@Test | |||
public void launchJettyWithCustomSourceDir() { | |||
String[] args = {"src/jbake", "-s"}; | |||
main.run(args); | |||
|
|||
verify(mockJetty).run("output","8820"); | |||
} | |||
|
|||
// Documentation states these two commands will define the custom output, but the LaunchOptions file isn't setup for that. | |||
// I have written this test to define the existing functionality of the code and not that defined in docs. | |||
@Test | |||
public void launchJettyWithCustomDestinationDir() { | |||
String[] args = {"-s", "build/jbake"}; | |||
main.run(args); | |||
|
|||
verify(mockJetty).run("output","8820"); | |||
} | |||
|
|||
@Test | |||
public void launchJettyWithCustomSrcAndDestDir() { | |||
String[] args = {"jbake", "build/jbake", "-s"}; | |||
main.run(args); | |||
|
|||
verify(mockJetty).run("build/jbake","8820"); | |||
} | |||
|
|||
@Test | |||
public void launchJettyWithCustomDestViaConfig() throws CmdLineException { | |||
String[] args = {"-s"}; | |||
Map properties = new HashMap(){{ | |||
put("destination.folder", "build/jbake"); | |||
}}; | |||
main.run(stubOptions(args), stubConfig(properties)); | |||
|
|||
verify(mockJetty).run("build/jbake","8820"); | |||
} | |||
|
|||
@Test | |||
public void launchJettyWithCmdlineOverridingProperties() throws CmdLineException { | |||
String[] args = {"src/jbake", "build/jbake", "-s"}; | |||
Map properties = new HashMap(){{ | |||
put("destination.folder", "target/jbake"); | |||
}}; | |||
main.run(stubOptions(args), stubConfig(properties)); | |||
|
|||
verify(mockJetty).run("build/jbake","8820"); | |||
} | |||
|
|||
private LaunchOptions stubOptions(String[] args) throws CmdLineException { | |||
LaunchOptions res = new LaunchOptions(); | |||
CmdLineParser parser = new CmdLineParser(res); | |||
parser.parseArgument(args); | |||
return res; | |||
} | |||
|
|||
private CompositeConfiguration stubConfig(Map<String, String> properties) { | |||
CompositeConfiguration config = new CompositeConfiguration(); | |||
config.addProperty("server.port", "8820"); | |||
Iterator it = properties.entrySet().iterator(); | |||
while(it.hasNext()) { | |||
Map.Entry<String,String> pair = (Map.Entry<String,String>)it.next(); | |||
config.addProperty( pair.getKey(), pair.getValue() ); | |||
} | |||
return config; | |||
} | |||
} |