// Path: src/test/java/no/difi/datahotel/BaseTest.java
// public class BaseTest {
// 
//     @BeforeClass
//     public static void beforeClass() throws Exception {
//         System.setProperty("datahotel.home", new File(ChunkBeanTest.class.getResource("/").toURI()).toString() + File.separator + "datahotel");
//     }
// }
// 
// Path: src/main/java/no/difi/datahotel/util/Filesystem.java
// public class Filesystem {
// 
// 	private static final String HOME = "datahotel";
// 
// 	public static final String FOLDER_MASTER = "master";
// 	public static final String FOLDER_MASTER_DEFINITION = FOLDER_MASTER + File.separator + "definition";
// 	public static final String FOLDER_SLAVE = "slave";
// 	public static final String FOLDER_CACHE = "cache";
// 	public static final String FOLDER_CACHE_CHUNK = FOLDER_CACHE + File.separator + "chunk";
// 	public static final String FOLDER_CACHE_INDEX = FOLDER_CACHE + File.separator + "index";
// 
// 	public static final String FILE_DATASET = "dataset.csv";
// 	public static final String FILE_DATASET_ORIGINAL = "original.csv";
// 	public static final String FILE_FIELDS = "fields.xml";
// 	public static final String FILE_DEFINITIONS = "definitions.xml";
// 	public static final String FILE_METADATA = "meta.xml";
// 	public static final String FILE_VERSION = "version.xml";
// 
// 	private static String home;
// 	
// 	public static String getHome() {
// 		if (home != null)
// 			return home;
// 		
// 		String dir;
// 
// 		try {
// 			Context initCtx = new InitialContext();
// 			Context envCtx = (Context) initCtx.lookup("java:comp/env");
// 			
// 			dir = envCtx.lookup("datahotel.home") + File.separator;
// 		} catch (NamingException e) {
// 			if (System.getProperty("datahotel.home") != null)
// 				dir = System.getProperty("datahotel.home") + File.separator;
// 			else
// 				dir = System.getProperty("user.home") + File.separator + HOME + File.separator;
// 		}
// 		
// 		dir = dir.replace(File.separator + File.separator, File.separator);
// 		new File(dir).mkdirs();
// 		
// 		home = dir;
// 		return dir;
// 	}
// 
// 	public static File getFolderPath(String... folder) {
// 		String dir = getHome();
// 		for (String f : folder)
// 			if (!"".equals(f))
// 				dir += f.replace("/", File.separator) + File.separator;
// 		return new File(dir);
// 	}
// 
// 	public static File getFolder(String... folder) {
// 		File dir = getFolderPath(folder);
// 
// 		if (!dir.exists())
// 			dir.mkdirs();
// 
// 		return dir;
// 	}
// 
// 	public static File getFile(String... uri) {
// 		String[] dir = new String[uri.length - 1];
// 		for (int i = 0; i < uri.length - 1; i++)
// 			dir[i] = uri[i];
// 
// 		return new File(getFolder(dir).toString() + File.separator + uri[uri.length - 1]);
// 	}
// 
// 	public static File getFile(File folder, String... uri) {
// 		String path = folder.toString();
// 		for (int i = 0; i < uri.length - 1; i++)
// 			path += uri[i].replace("/", File.separator) + File.separator;
// 
// 		return new File(path + File.separator + uri[uri.length - 1]);
// 	}
// 
// 	public static void delete(String folder, String location) {
// 		delete(getFolder(folder, location));
// 	}
// 
// 	public static void delete(String folder) {
// 		delete(getFolder(folder));
// 	}
// 
// 	public static void delete(File folder) {
// 		for (File f : folder.listFiles()) {
// 			if (f.isDirectory())
// 				delete(f);
// 			else
// 				f.delete();
// 		}
// 		folder.delete();
// 	}
// }


import no.difi.datahotel.BaseTest;
import no.difi.datahotel.util.Filesystem;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;

        dataBean = new DataBean();
        logger = Mockito.mock(Logger.class);

        MetadataBean m = new MetadataBean();
        m.setDataEJB(dataBean);
        m.setUpdateEJB(Mockito.mock(UpdateBean.class));

        Field settingsLoggerField = MetadataBean.class.getDeclaredField("logger");
        settingsLoggerField.setAccessible(true);
        settingsLoggerField.set(m, logger);

        return m;
    }

    @Test
    public void testReading() {
        metadataBean.update();
        assertEquals(1, dataBean.getChildren().size());
        assertEquals("http://www.difi.no/", dataBean.getChild("difi").getUrl());
        assertEquals(5, dataBean.getDatasets().size());

        assertEquals(null, dataBean.getChildren("not/seen/here"));

        assertEquals(2, dataBean.getChild("difi", "geo").getChildren().size());
    }

    @Test
    public void testError() throws IOException {
        File folder = Filesystem.getFolder(Filesystem.FOLDER_SLAVE, "google");
// Path: src/test/java/no/difi/datahotel/BaseTest.java
// public class BaseTest {
// 
//     @BeforeClass
//     public static void beforeClass() throws Exception {
//         System.setProperty("datahotel.home", new File(ChunkBeanTest.class.getResource("/").toURI()).toString() + File.separator + "datahotel");
//     }
// }
// 
// Path: src/main/java/no/difi/datahotel/util/Filesystem.java
// public class Filesystem {
// 
// 	private static final String HOME = "datahotel";
// 
// 	public static final String FOLDER_MASTER = "master";
// 	public static final String FOLDER_MASTER_DEFINITION = FOLDER_MASTER + File.separator + "definition";
// 	public static final String FOLDER_SLAVE = "slave";
// 	public static final String FOLDER_CACHE = "cache";
// 	public static final String FOLDER_CACHE_CHUNK = FOLDER_CACHE + File.separator + "chunk";
// 	public static final String FOLDER_CACHE_INDEX = FOLDER_CACHE + File.separator + "index";
// 
// 	public static final String FILE_DATASET = "dataset.csv";
// 	public static final String FILE_DATASET_ORIGINAL = "original.csv";
// 	public static final String FILE_FIELDS = "fields.xml";
// 	public static final String FILE_DEFINITIONS = "definitions.xml";
// 	public static final String FILE_METADATA = "meta.xml";
// 	public static final String FILE_VERSION = "version.xml";
// 
// 	private static String home;
// 	
// 	public static String getHome() {
// 		if (home != null)
// 			return home;
// 		
// 		String dir;
// 
// 		try {
// 			Context initCtx = new InitialContext();
// 			Context envCtx = (Context) initCtx.lookup("java:comp/env");
// 			
// 			dir = envCtx.lookup("datahotel.home") + File.separator;
// 		} catch (NamingException e) {
// 			if (System.getProperty("datahotel.home") != null)
// 				dir = System.getProperty("datahotel.home") + File.separator;
// 			else
// 				dir = System.getProperty("user.home") + File.separator + HOME + File.separator;
// 		}
// 		
// 		dir = dir.replace(File.separator + File.separator, File.separator);
// 		new File(dir).mkdirs();
// 		
// 		home = dir;
// 		return dir;
// 	}
// 
// 	public static File getFolderPath(String... folder) {
// 		String dir = getHome();
// 		for (String f : folder)
// 			if (!"".equals(f))
// 				dir += f.replace("/", File.separator) + File.separator;
// 		return new File(dir);
// 	}
// 
// 	public static File getFolder(String... folder) {
// 		File dir = getFolderPath(folder);
// 
// 		if (!dir.exists())
// 			dir.mkdirs();
// 
// 		return dir;
// 	}
// 
// 	public static File getFile(String... uri) {
// 		String[] dir = new String[uri.length - 1];
// 		for (int i = 0; i < uri.length - 1; i++)
// 			dir[i] = uri[i];
// 
// 		return new File(getFolder(dir).toString() + File.separator + uri[uri.length - 1]);
// 	}
// 
// 	public static File getFile(File folder, String... uri) {
// 		String path = folder.toString();
// 		for (int i = 0; i < uri.length - 1; i++)
// 			path += uri[i].replace("/", File.separator) + File.separator;
// 
// 		return new File(path + File.separator + uri[uri.length - 1]);
// 	}
// 
// 	public static void delete(String folder, String location) {
// 		delete(getFolder(folder, location));
// 	}
// 
// 	public static void delete(String folder) {
// 		delete(getFolder(folder));
// 	}
// 
// 	public static void delete(File folder) {
// 		for (File f : folder.listFiles()) {
// 			if (f.isDirectory())
// 				delete(f);
// 			else
// 				f.delete();
// 		}
// 		folder.delete();
// 	}
// }


// Path: src/test/java/no/difi/datahotel/logic/MetadataBeanTest.java
import no.difi.datahotel.BaseTest;
import no.difi.datahotel.util.Filesystem;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;


        dataBean = new DataBean();
        logger = Mockito.mock(Logger.class);

        MetadataBean m = new MetadataBean();
        m.setDataEJB(dataBean);
        m.setUpdateEJB(Mockito.mock(UpdateBean.class));

        Field settingsLoggerField = MetadataBean.class.getDeclaredField("logger");
        settingsLoggerField.setAccessible(true);
        settingsLoggerField.set(m, logger);

        return m;
    }

    @Test
    public void testReading() {
        metadataBean.update();
        assertEquals(1, dataBean.getChildren().size());
        assertEquals("http://www.difi.no/", dataBean.getChild("difi").getUrl());
        assertEquals(5, dataBean.getDatasets().size());

        assertEquals(null, dataBean.getChildren("not/seen/here"));

        assertEquals(2, dataBean.getChild("difi", "geo").getChildren().size());
    }

    @Test
    public void testError() throws IOException {
        File folder = Filesystem.getFolder(Filesystem.FOLDER_SLAVE, "google");
        File file = Filesystem.getFile(folder, "meta.xml");