// Path: src/test/java/no/met/metadataeditor/TestHelpers.java
// public class TestHelpers {
// 
//     public static String formattedXMLAsString(String filename) {
// 
//         String output = "";
//         try {
//             URL fileUrl = TestHelpers.class.getResource(filename);
//             SAXBuilder builder = new SAXBuilder();
//             Document doc = builder.build(new InputSource(fileUrl.openStream()));
//             XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
//             StringWriter writer = new StringWriter();
//             xout.output(doc, writer);
//             output = writer.toString();
// 
//         } catch (JDOMException e) {
//             fail(e.getMessage());
//         } catch (IOException e) {
//             fail(e.getMessage());
//         }
//         return output;
// 
//     }
// 
//     public static String fileAsString(String filename) {
// 
//         URL fileUrl = TestHelpers.class.getResource(filename);
//         StringBuilder sb = new StringBuilder();
//         BufferedReader br = null;
//         try {
//             br = new BufferedReader(new InputStreamReader(fileUrl.openStream()));
//             String line = br.readLine();
// 
//             while (line != null) {
//                 sb.append(line);
//                 sb.append("\n");
//                 line = br.readLine();
//             }
//         } catch (IOException e) {
//             throw new EditorException(e.getMessage(), EditorException.IO_ERROR);
//         } finally {
//             if (br != null) {
//                 try {
//                     br.close();
//                 } catch (IOException e) {
//                     Logger.getLogger(TestHelpers.class.getName()).log(Level.SEVERE, null, e);
//                 }
//             }
//         }
//         return sb.toString();
// 
//     }
// 
//     public static Map<String, List<EditorVariableContent>> getContent(String templateResource, String metadataResource ){
// 
//         URL templateUrl = TestHelpers.class.getResource(templateResource);
//         URL metadataUrl = TestHelpers.class.getResource(metadataResource);
//         EditorTemplate et = null;
//         Map<String, List<EditorVariableContent>> content = null;
//         try {
//             et = new EditorTemplate(new InputSource(templateUrl.openStream()));
//             content = et.getContent(new InputSource(metadataUrl.openStream()));
// 
//         } catch (SAXException e) {
//             e.printStackTrace();
//             fail();
//         } catch (IOException e) {
//             e.printStackTrace();
//             fail();
//         } catch (ParserConfigurationException e) {
//             e.printStackTrace();
//             fail();
//         }
//         return content;
//     }
// 
//     public static EditorConfiguration getConfiguration(String configurationResource){
// 
//         String configString = fileAsString(configurationResource);
//         EditorConfiguration config = EditorConfigurationFactory.unmarshallConfiguration(configString);
//         return config;
// 
//     }
// 
//     public static Map<String, EditorVariable> getVariables(String templateResource ){
// 
//         URL url = TestHelpers.class.getResource(templateResource);
//         Map<String, EditorVariable> mse = null;
//         EditorTemplate et = null;
//         try {
//             et = new EditorTemplate(new InputSource(url.openStream()));
//             mse = et.getVarMap();
//         } catch (SAXException e) {
//             e.printStackTrace();
//             fail();
//         } catch (IOException e) {
//             e.printStackTrace();
//             fail();
//         }
//         return mse;
//     }
// 
//     /**
//      * read the editor configuration "metadataeditor.properties from this path
//      * instead of from resources and reset the DataStoreFactory so that it
//      * rereads the new config
//      *
//      * @param editorConfigPath
//      */
//     public static void setEditorConfigEnv(String editorConfigPath) {
//         @SuppressWarnings("rawtypes")
//         Class[] classes = Collections.class.getDeclaredClasses();
//         Map<String, String> env = System.getenv();
//         for (@SuppressWarnings("rawtypes") Class cl : classes) {
//             if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
//                 Field field;
//                 try {
//                     field = cl.getDeclaredField("m");
//                     field.setAccessible(true);
//                     Object obj = field.get(env);
//                     @SuppressWarnings("unchecked")
//                     Map<String, String> map = (Map<String, String>) obj;
//                     map.put(Config.ENV_NAME, editorConfigPath);
// 
//                 } catch (NoSuchFieldException e) {
//                     fail(e.getMessage());
//                 } catch (SecurityException e) {
//                     fail(e.getMessage());
//                 } catch (IllegalArgumentException e) {
//                     fail(e.getMessage());
//                 } catch (IllegalAccessException e) {
//                     fail(e.getMessage());
//                 }
//             }
//         }
//         DataStoreFactory.reset();
//     }
// 
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import no.met.metadataeditor.TestHelpers;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Test
    public void testVariableNames() {

        Map<String, EditorVariable> mse = getVariables("/mm2TemplateTest.xml");

        List<String> expectedVarNames = Arrays.asList(new String[] { "wmsSetup", "globalBB", "localBB", "variableList",
                "PIname", "timeExtendTo", "timeExtendFrom", "dataRef" });

        assertEquals("Number of variables in template as expected", expectedVarNames.size(), mse.size());

        for (String varName : expectedVarNames) {
            assertEquals("Variable call '" + varName + "' found", true, mse.containsKey(varName));
        }

    }

    @Test
    public void testChildren() {

        Map<String, EditorVariable> mse = getVariables("/testChildren/basic.xml");

        EditorVariable parent = mse.get("parent");
        Map<String,EditorVariable> children = parent.getChildren();

// Path: src/test/java/no/met/metadataeditor/TestHelpers.java
// public class TestHelpers {
// 
//     public static String formattedXMLAsString(String filename) {
// 
//         String output = "";
//         try {
//             URL fileUrl = TestHelpers.class.getResource(filename);
//             SAXBuilder builder = new SAXBuilder();
//             Document doc = builder.build(new InputSource(fileUrl.openStream()));
//             XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
//             StringWriter writer = new StringWriter();
//             xout.output(doc, writer);
//             output = writer.toString();
// 
//         } catch (JDOMException e) {
//             fail(e.getMessage());
//         } catch (IOException e) {
//             fail(e.getMessage());
//         }
//         return output;
// 
//     }
// 
//     public static String fileAsString(String filename) {
// 
//         URL fileUrl = TestHelpers.class.getResource(filename);
//         StringBuilder sb = new StringBuilder();
//         BufferedReader br = null;
//         try {
//             br = new BufferedReader(new InputStreamReader(fileUrl.openStream()));
//             String line = br.readLine();
// 
//             while (line != null) {
//                 sb.append(line);
//                 sb.append("\n");
//                 line = br.readLine();
//             }
//         } catch (IOException e) {
//             throw new EditorException(e.getMessage(), EditorException.IO_ERROR);
//         } finally {
//             if (br != null) {
//                 try {
//                     br.close();
//                 } catch (IOException e) {
//                     Logger.getLogger(TestHelpers.class.getName()).log(Level.SEVERE, null, e);
//                 }
//             }
//         }
//         return sb.toString();
// 
//     }
// 
//     public static Map<String, List<EditorVariableContent>> getContent(String templateResource, String metadataResource ){
// 
//         URL templateUrl = TestHelpers.class.getResource(templateResource);
//         URL metadataUrl = TestHelpers.class.getResource(metadataResource);
//         EditorTemplate et = null;
//         Map<String, List<EditorVariableContent>> content = null;
//         try {
//             et = new EditorTemplate(new InputSource(templateUrl.openStream()));
//             content = et.getContent(new InputSource(metadataUrl.openStream()));
// 
//         } catch (SAXException e) {
//             e.printStackTrace();
//             fail();
//         } catch (IOException e) {
//             e.printStackTrace();
//             fail();
//         } catch (ParserConfigurationException e) {
//             e.printStackTrace();
//             fail();
//         }
//         return content;
//     }
// 
//     public static EditorConfiguration getConfiguration(String configurationResource){
// 
//         String configString = fileAsString(configurationResource);
//         EditorConfiguration config = EditorConfigurationFactory.unmarshallConfiguration(configString);
//         return config;
// 
//     }
// 
//     public static Map<String, EditorVariable> getVariables(String templateResource ){
// 
//         URL url = TestHelpers.class.getResource(templateResource);
//         Map<String, EditorVariable> mse = null;
//         EditorTemplate et = null;
//         try {
//             et = new EditorTemplate(new InputSource(url.openStream()));
//             mse = et.getVarMap();
//         } catch (SAXException e) {
//             e.printStackTrace();
//             fail();
//         } catch (IOException e) {
//             e.printStackTrace();
//             fail();
//         }
//         return mse;
//     }
// 
//     /**
//      * read the editor configuration "metadataeditor.properties from this path
//      * instead of from resources and reset the DataStoreFactory so that it
//      * rereads the new config
//      *
//      * @param editorConfigPath
//      */
//     public static void setEditorConfigEnv(String editorConfigPath) {
//         @SuppressWarnings("rawtypes")
//         Class[] classes = Collections.class.getDeclaredClasses();
//         Map<String, String> env = System.getenv();
//         for (@SuppressWarnings("rawtypes") Class cl : classes) {
//             if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
//                 Field field;
//                 try {
//                     field = cl.getDeclaredField("m");
//                     field.setAccessible(true);
//                     Object obj = field.get(env);
//                     @SuppressWarnings("unchecked")
//                     Map<String, String> map = (Map<String, String>) obj;
//                     map.put(Config.ENV_NAME, editorConfigPath);
// 
//                 } catch (NoSuchFieldException e) {
//                     fail(e.getMessage());
//                 } catch (SecurityException e) {
//                     fail(e.getMessage());
//                 } catch (IllegalArgumentException e) {
//                     fail(e.getMessage());
//                 } catch (IllegalAccessException e) {
//                     fail(e.getMessage());
//                 }
//             }
//         }
//         DataStoreFactory.reset();
//     }
// 
// }


// Path: src/test/java/no/met/metadataeditor/dataTypes/TemplateHandlerTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import no.met.metadataeditor.TestHelpers;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Test
    public void testVariableNames() {

        Map<String, EditorVariable> mse = getVariables("/mm2TemplateTest.xml");

        List<String> expectedVarNames = Arrays.asList(new String[] { "wmsSetup", "globalBB", "localBB", "variableList",
                "PIname", "timeExtendTo", "timeExtendFrom", "dataRef" });

        assertEquals("Number of variables in template as expected", expectedVarNames.size(), mse.size());

        for (String varName : expectedVarNames) {
            assertEquals("Variable call '" + varName + "' found", true, mse.containsKey(varName));
        }

    }

    @Test
    public void testChildren() {

        Map<String, EditorVariable> mse = getVariables("/testChildren/basic.xml");

        EditorVariable parent = mse.get("parent");
        Map<String,EditorVariable> children = parent.getChildren();

        assertEquals("Correct number of children", 2, children.size());