package org.jbake.app;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.ConfigUtil.Keys;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class AssetTest {

	private CompositeConfiguration config;

	@Before
    public void setup() throws Exception, IOException, URISyntaxException {
		config = ConfigUtil.load(new File(this.getClass().getResource("/").getFile()));
        Assert.assertEquals(".html", config.getString(Keys.OUTPUT_EXTENSION));
	}

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();

	@Test
	public void copy() throws Exception {
		URL assetsUrl = this.getClass().getResource("/assets");
		File assets = new File(assetsUrl.getFile());
		Asset asset = new Asset(assets.getParentFile(), folder.getRoot(), config);
		asset.copy(assets);

		File cssFile = new File(folder.getRoot().getPath() + File.separatorChar + "css" + File.separatorChar + "bootstrap.min.css");
		Assert.assertTrue("File " + cssFile.getAbsolutePath() + " does not exist", cssFile.exists());
		File imgFile = new File(folder.getRoot().getPath() + File.separatorChar + "img" + File.separatorChar + "glyphicons-halflings.png");
		Assert.assertTrue("File " + imgFile.getAbsolutePath() + " does not exist", imgFile.exists());
		File jsFile = new File(folder.getRoot().getPath() + File.separatorChar + "js" + File.separatorChar + "bootstrap.min.js");
		Assert.assertTrue("File " + jsFile.getAbsolutePath() + " does not exist", jsFile.exists());
	}

	@Test
	public void copyCustomFolder() throws Exception {
		config.setProperty(ConfigUtil.Keys.ASSET_FOLDER, "media");
		URL assetsUrl = this.getClass().getResource("/media");
		File assets = new File(assetsUrl.getFile());
		Asset asset = new Asset(assets.getParentFile(), folder.getRoot(), config);
		asset.copy(assets);

		File favFile = new File(folder.getRoot().getPath() + File.separatorChar + "favicon.ico");
		Assert.assertTrue("File " + favFile.getAbsolutePath() + " does not exist", favFile.exists());
	}

	@Test
	public void copyIgnore() throws Exception {
		config.setProperty(Keys.ASSET_FOLDER, "ignorables");
		config.setProperty(Keys.ASSET_IGNORE_HIDDEN, "true");
		URL assetsUrl = this.getClass().getResource("/ignorables");
		File assets = new File(assetsUrl.getFile());
		Asset asset = new Asset(assets.getParentFile(), folder.getRoot(), config);
		asset.copy(assets);

		File testFile = new File(folder.getRoot(), "test.txt");
		Assert.assertTrue("File " + testFile.getAbsolutePath() + " does not exist", testFile.exists());
		File testIgnoreFile = new File(folder.getRoot(), ".test.txt");
		Assert.assertFalse("File " + testIgnoreFile.getAbsolutePath() + " does exist", testIgnoreFile.exists());
	}
}

@@ -676,19 +676,61 @@
 SION));%0A
+%09%09readOnlyFolder.getRoot().setReadOnly();%0A
 %09%7D%0A
-
 %0A%09@Rule%0A
@@ -786,16 +786,88 @@
 der();%0A%0A
+%09@Rule%0A%09public TemporaryFolder readOnlyFolder = new TemporaryFolder();%0A%0A
 %09@Test%0A%09
@@ -1771,32 +1771,114 @@
 sFile.exists());
+%0A%0A%09%09Assert.assertTrue(%22Errors during asset copying%22, asset.getErrors().isEmpty());
 %0A%09%7D%0A%0A%09@Test%0A%09pub
@@ -2372,24 +2372,106 @@
 e.exists());
+%0A%0A%09%09Assert.assertTrue(%22Errors during asset copying%22, asset.getErrors().isEmpty());
 %0A%09%7D%0A%0A%09@Test%0A
@@ -3048,32 +3048,32 @@
 , %22.test.txt%22);%0A
-
 %09%09Assert.assertF
@@ -3158,18 +3158,1040 @@
 e.exists());
+%0A%0A%09%09Assert.assertTrue(%22Errors during asset copying%22, asset.getErrors().isEmpty());%0A%09%7D%0A%0A%09/**%0A%09 * Primary intention is to extend test cases to increase coverage.%0A%09 *%0A%09 * @throws Exception%0A%09 */%0A%09@Test%0A%09public void testWriteProtected() throws Exception %7B%0A%09%09URL assetsUrl = this.getClass().getResource(%22/assets%22);%0A%09%09File assets = new File(assetsUrl.getFile());%0A%09%09Asset asset = new Asset(assets.getParentFile(), readOnlyFolder.getRoot(), config);%0A%09%09asset.copy(assets);%0A%0A%09%09Assert.assertFalse(%22At least one error during copy expected%22, asset.getErrors().isEmpty());%0A%09%7D%0A%0A%09/**%0A%09 * Primary intention is to extend test cases to increase coverage.%0A%09 *%0A%09 * @throws Exception%0A%09 */%0A%09@Test%0A%09public void testUnlistable() throws Exception %7B%0A%09%09config.setProperty(Keys.ASSET_FOLDER, %22non-existent%22);%0A%09%09URL assetsUrl = this.getClass().getResource(%22/%22);%0A%09%09File assets = new File(assetsUrl.getFile() + File.separatorChar + %22non-existent%22);%0A%09%09Asset asset = new Asset(assets.getParentFile(), readOnlyFolder.getRoot(), config);%0A%09%09asset.copy(assets);
 %0A%09%7D%0A%7D%0A

Enhance AssetTest to raise code coverage during tests