@Test public void shouldThrowAnExceptionIfSourcefolderDoesNotExist() throws Exception { File nonExistentSourceFolder = mock(File.class); when(nonExistentSourceFolder.getAbsolutePath()).thenReturn("/tmp/nonexistent"); when(nonExistentSourceFolder.exists()).thenReturn(false); try { util.loadConfig(nonExistentSourceFolder); fail("Exception should be thrown, as source folder does not exist"); } catch (JBakeException e) { assertThat(e.getMessage()).isEqualTo("The given source folder '/tmp/nonexistent' does not exist."); } }
private void checkRequiredFolderExists(String folderName, File path) { if (!FileUtil.isExistingFolder(path)) { throw new JBakeException("Error: Required folder cannot be found! Expected to find [" + folderName + "] at: " + path.getAbsolutePath()); } }
/** * Runs the app with the given arguments. * * @param args Application arguments */ public static void main(final String[] args) { try { new Main().run(args); } catch (final JBakeException e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(1); } catch (final Throwable e) { System.err.println("An unexpected error occurred: " + e.getMessage()); e.printStackTrace(); System.exit(2); } }
private void ensureDestination() { File destination = configuration.getDestinationFolder(); if (!destination.exists()) { destination.mkdirs(); } if (!destination.canWrite()) { throw new JBakeException("Error: Destination folder is not writable: " + destination.getAbsolutePath()); } }
@Test public void shouldThrowAnExceptionIfSourcefolderIsNotADirectory() throws Exception { File sourceFolder = mock(File.class); when(sourceFolder.exists()).thenReturn(true); when(sourceFolder.isDirectory()).thenReturn(false); try { util.loadConfig(sourceFolder); fail("Exception should be thrown if given source folder is not a directory."); } catch (JBakeException e) { assertThat(e.getMessage()).isEqualTo("The given source folder is not a directory."); } }
/** * Runs the app with the given arguments. * * @param args Application arguments */ public static void main(final String[] args) { try { new Main().run(args); } catch (final JBakeException e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(1); } catch (final Throwable e) { System.err.println("An unexpected error occurred: " + e.getMessage()); e.printStackTrace(); System.exit(2); } }
private void ensureSource() throws JBakeException { File source = configuration.getSourceFolder(); if (!FileUtil.isExistingFolder(source)) { throw new JBakeException("Error: Source folder must exist: " + source.getAbsolutePath()); } if (!configuration.getSourceFolder().canRead()) { throw new JBakeException("Error: Source folder is not readable: " + source.getAbsolutePath()); } }
@Test public void shouldThrowExceptionIfSourceFolderIsNotReadable() throws Exception { File nonReadableFile = mock(File.class); when(nonReadableFile.exists()).thenReturn(true); when(nonReadableFile.isDirectory()).thenReturn(true); when(nonReadableFile.canRead()).thenReturn(false); JBakeConfiguration configuration = mock(JBakeConfiguration.class); when(configuration.getSourceFolder()).thenReturn(nonReadableFile); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); try { inspector.inspect(); fail("should throw a JBakeException"); } catch (JBakeException e) { assertThat(e.getMessage()).isEqualTo("Error: Source folder is not readable: " + nonReadableFile.getAbsolutePath()); } }
public void bake(final JBakeConfiguration config) { final Oven oven = new Oven(config); 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)); } } }
@Test public void shouldThrowExceptionIfSourceFolderDoesNotExist() throws Exception { File nonExistentFile = new File(folder.toFile(), "nofolder"); JBakeConfiguration configuration = mock(JBakeConfiguration.class); when(configuration.getSourceFolder()).thenReturn(nonExistentFile); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); try { inspector.inspect(); fail("should throw a JBakeException"); } catch (JBakeException e) { assertThat(e.getMessage()).isEqualTo("Error: Source folder must exist: " + nonExistentFile.getAbsolutePath()); } }
private LaunchOptions parseArguments(String[] args) { LaunchOptions res = new LaunchOptions(); CmdLineParser parser = new CmdLineParser(res); try { parser.parseArgument(args); } catch (final CmdLineException e) { printUsage(res); throw new JBakeException("Invalid commandline arguments: " + e.getMessage(), e); } return res; }
@Test public void shouldThrowExceptionIfTemplateFolderDoesNotExist() throws Exception { String templateFolderName = "template"; File expectedFolder = new File(folder.toFile(), templateFolderName); JBakeConfiguration configuration = mock(JBakeConfiguration.class); when(configuration.getSourceFolder()).thenReturn(folder.toFile()); when(configuration.getTemplateFolder()).thenReturn(expectedFolder); when(configuration.getTemplateFolderName()).thenReturn(templateFolderName); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); try { inspector.inspect(); fail("should throw a JBakeException"); } catch (JBakeException e) { assertThat(e.getMessage()).isEqualTo("Error: Required folder cannot be found! Expected to find [" + templateFolderName + "] at: " + expectedFolder.getAbsolutePath()); } }
private CompositeConfiguration load(File source) throws ConfigurationException { if (!source.exists()) { throw new JBakeException("The given source folder '" + source.getAbsolutePath() + "' does not exist."); } if (!source.isDirectory()) { throw new JBakeException("The given source folder is not a directory."); } CompositeConfiguration config = new CompositeConfiguration(); config.setListDelimiter(','); File customConfigFile = new File(source, LEGACY_CONFIG_FILE); if (customConfigFile.exists()) { displayLegacyConfigFileWarningIfRequired(); config.addConfiguration(new PropertiesConfiguration(customConfigFile)); } customConfigFile = new File(source, CONFIG_FILE); if (customConfigFile.exists()) { config.addConfiguration(new PropertiesConfiguration(customConfigFile)); } config.addConfiguration(new PropertiesConfiguration(DEFAULT_CONFIG_FILE)); config.addConfiguration(new SystemConfiguration()); return config; }
@Test public void shouldThrowExceptionIfContentFolderDoesNotExist() throws Exception { String contentFolderName = "content"; String templateFolderName = "template"; File templateFolder = TestUtils.newFolder(folder.toFile(), templateFolderName); File contentFolder = new File(folder.toFile(), contentFolderName); JBakeConfiguration configuration = mock(JBakeConfiguration.class); when(configuration.getSourceFolder()).thenReturn(folder.toFile()); when(configuration.getTemplateFolder()).thenReturn(templateFolder); when(configuration.getTemplateFolderName()).thenReturn(templateFolderName); when(configuration.getContentFolder()).thenReturn(contentFolder); when(configuration.getContentFolderName()).thenReturn(contentFolderName); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); try { inspector.inspect(); fail("should throw a JBakeException"); } catch (JBakeException e) { assertThat(e.getMessage()).isEqualTo("Error: Required folder cannot be found! Expected to find [" + contentFolderName + "] at: " + contentFolder.getAbsolutePath()); } }
private void initStructure(String type, JBakeConfiguration config) { Init init = new Init(config); try { File templateFolder = FileUtil.getRunningLocation(); File outputFolder; if (config.getSourceFolder() != null) { outputFolder = config.getSourceFolder(); } else { outputFolder = new File("."); } init.run(outputFolder, templateFolder, type); System.out.println("Base folder structure successfully created."); } catch (final Exception e) { final String msg = "Failed to initialise structure: " + e.getMessage(); throw new JBakeException(msg, e); } }
@Test public void shouldThrowExceptionIfDestinationFolderNotWritable() throws Exception { String contentFolderName = "content"; String templateFolderName = "template"; File templateFolder = TestUtils.newFolder(folder.toFile(), templateFolderName); File contentFolder = TestUtils.newFolder(folder.toFile(), contentFolderName); File destinationFolder = mock(File.class); when(destinationFolder.exists()).thenReturn(true); JBakeConfiguration configuration = mock(JBakeConfiguration.class); when(configuration.getSourceFolder()).thenReturn(folder.toFile()); when(configuration.getTemplateFolder()).thenReturn(templateFolder); when(configuration.getTemplateFolderName()).thenReturn(templateFolderName); when(configuration.getContentFolder()).thenReturn(contentFolder); when(configuration.getContentFolderName()).thenReturn(contentFolderName); when(configuration.getDestinationFolder()).thenReturn(destinationFolder); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); try { inspector.inspect(); fail("should throw JBakeException"); } catch (JBakeException e) { assertThat(e.getMessage()).contains("Error: Destination folder is not writable:"); } }
/** * Get a document by sourceUri and update it from the given map. * @param incomingDocMap The document's db columns. * @return The saved document. * @throws IllegalArgumentException if sourceUri or docType are null, or if the document doesn't exist. */ public ODocument mergeDocument(Map<String, ? extends Object> incomingDocMap) { String sourceUri = (String) incomingDocMap.get(DocumentAttributes.SOURCE_URI.toString()); if (null == sourceUri) throw new IllegalArgumentException("Document sourceUri is null."); String docType = (String) incomingDocMap.get(Crawler.Attributes.TYPE); if (null == docType) throw new IllegalArgumentException("Document docType is null."); // Get a document by sourceUri String sql = "SELECT * FROM " + docType + " WHERE sourceuri=?"; activateOnCurrentThread(); List<ODocument> results = db.command(new OSQLSynchQuery<ODocument>(sql)).execute(sourceUri); if (results.size() == 0) throw new JBakeException("No document with sourceUri '"+sourceUri+"'."); // Update it from the given map. ODocument incomingDoc = new ODocument(docType); incomingDoc.fromMap(incomingDocMap); ODocument merged = results.get(0).merge(incomingDoc, true, false); return merged; }
protected void run(String[] args) { SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); LaunchOptions res = parseArguments(args); final JBakeConfiguration config; try { if (res.isRunServer()) { config = getJBakeConfigurationFactory().createJettyJbakeConfiguration(res.getSource(), res.getDestination(), res.isClearCache()); } else { config = getJBakeConfigurationFactory().createDefaultJbakeConfiguration(res.getSource(), res.getDestination(), res.isClearCache()); } } catch (final ConfigurationException e) { throw new JBakeException("Configuration error: " + e.getMessage(), e); } run(res, config); }
private void ensureDestination() { File destination = configuration.getDestinationFolder(); if (!destination.exists()) { destination.mkdirs(); } if (!destination.canWrite()) { throw new JBakeException("Error: Destination folder is not writable: " + destination.getAbsolutePath()); } }
private void checkRequiredFolderExists(String folderName, File path) { if (!FileUtil.isExistingFolder(path)) { throw new JBakeException("Error: Required folder cannot be found! Expected to find [" + folderName + "] at: " + path.getAbsolutePath()); } }