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()); } }
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 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()); } }
@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()); } }
@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()); } }
/** * Starts watching the file system for changes to trigger a bake. * * @param config JBakeConfiguration settings */ public void start(JBakeConfiguration config) { try { FileSystemManager fsMan = VFS.getManager(); FileObject listenPath = fsMan.resolveFile(config.getContentFolder().toURI()); FileObject templateListenPath = fsMan.resolveFile(config.getTemplateFolder().toURI()); FileObject assetPath = fsMan.resolveFile(config.getAssetFolder().toURI()); logger.info("Watching for (content, template, asset) changes in [{}]", config.getSourceFolder().getPath()); DefaultFileMonitor monitor = new DefaultFileMonitor(new CustomFSChangeListener(config)); monitor.setRecursive(true); monitor.addFile(listenPath); monitor.addFile(templateListenPath); monitor.addFile(assetPath); monitor.start(); } catch (FileSystemException e) { logger.error("Problems watching filesystem changes", e); } } }
@Test public void shouldCreateDestinationFolderIfNotExists() throws Exception { String contentFolderName = "content"; String templateFolderName = "template"; String destinationFolderName = "output"; File templateFolder = TestUtils.newFolder(folder.toFile(), templateFolderName); File contentFolder = TestUtils.newFolder(folder.toFile(), contentFolderName); File destinationFolder = new File(folder.toFile(), destinationFolderName); 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); when(configuration.getAssetFolder()).thenReturn(destinationFolder); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); inspector.inspect(); assertThat(destinationFolder).exists(); }
@Test public void shouldLogWarningIfAssetFolderDoesNotExist() throws Exception { String contentFolderName = "content"; String templateFolderName = "template"; String destinationFolderName = "output"; String assetFolderName = "assets"; File templateFolder = TestUtils.newFolder(folder.toFile(), templateFolderName); File contentFolder = TestUtils.newFolder(folder.toFile(), contentFolderName); File destinationFolder = TestUtils.newFolder(folder.toFile(), destinationFolderName); File assetFolder = new File(folder.toFile(), assetFolderName); 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); when(configuration.getAssetFolder()).thenReturn(assetFolder); JBakeConfigurationInspector inspector = new JBakeConfigurationInspector(configuration); inspector.inspect(); verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture()); LoggingEvent loggingEvent = captorLoggingEvent.getValue(); assertThat(loggingEvent.getMessage()).isEqualTo("No asset folder '{}' was found!"); assertThat(loggingEvent.getFormattedMessage()).isEqualTo("No asset folder '" + assetFolder.getAbsolutePath() + "' was found!"); }
@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()); } }
@Test public void shouldAddSourcefolderToConfiguration() throws Exception { File sourceFolder = TestUtils.getTestResourcesAsSourceFolder(); JBakeConfiguration config = util.loadConfig(sourceFolder); assertThat(config.getSourceFolder()).isEqualTo(sourceFolder); }
@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:"); } }
@Test public void shouldReturnDefaultConfigurationWithDefaultFolders() throws Exception { File sourceFolder = folder.getRoot(); File destinationFolder = folder.newFolder("output"); File templateFolder = new File(folder.getRoot(), "templates"); File assetFolder = new File(folder.getRoot(), "assets"); JBakeConfiguration configuration = new JBakeConfigurationFactory().createDefaultJbakeConfiguration(sourceFolder, destinationFolder, true); assertThat(configuration.getSourceFolder()).isEqualTo(sourceFolder); assertThat(configuration.getDestinationFolder()).isEqualTo(destinationFolder); assertThat(configuration.getTemplateFolder()).isEqualTo(templateFolder); assertThat(configuration.getAssetFolder()).isEqualTo(assetFolder); assertThat(configuration.getClearCache()).isEqualTo(true); }
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()); } }
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); } }
/** * Starts watching the file system for changes to trigger a bake. * * @param config JBakeConfiguration settings */ public void start(JBakeConfiguration config) { try { FileSystemManager fsMan = VFS.getManager(); FileObject listenPath = fsMan.resolveFile(config.getContentFolder().toURI()); FileObject templateListenPath = fsMan.resolveFile(config.getTemplateFolder().toURI()); FileObject assetPath = fsMan.resolveFile(config.getAssetFolder().toURI()); logger.info("Watching for (content, template, asset) changes in [{}]", config.getSourceFolder().getPath()); DefaultFileMonitor monitor = new DefaultFileMonitor(new CustomFSChangeListener(config)); monitor.setRecursive(true); monitor.addFile(listenPath); monitor.addFile(templateListenPath); monitor.addFile(assetPath); monitor.start(); } catch (FileSystemException e) { logger.error("Problems watching filesystem changes", e); } } }