/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.jmeter.gui.action.template;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.jmeter.junit.JMeterTestCase;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class TestTemplateManager extends JMeterTestCase {

    private Map<String, Template> templateMap;

    @Before
    public void setup() {
        templateMap = readTemplateFromFile();
        assertEquals(3, templateMap.size());
    }

    private Map<String, Template> readTemplateFromFile() {
        File xmlTemplate = new File(this.getClass().getResource("validTemplates.xml").getFile());
        try {
            return TemplateManager.getInstance().parseTemplateFile(xmlTemplate);
        } catch (IOException | SAXException | ParserConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testValidTemplateFile() {
        Template testTemplate = templateMap.get("testTemplateWithParameters");
        assertTrue(testTemplate.isTestPlan());
        assertEquals("testTemplateWithParameters", testTemplate.getName());
        assertEquals("/bin/templates/testTemplate.jmx.fmkr", testTemplate.getFileName());
        assertEquals("Template with parameters", testTemplate.getDescription());
        Map<String, String> testTemplateParameters = testTemplate.getParameters();
        assertEquals("n 1", testTemplateParameters.get("testKey1"));
        assertEquals("n 2", testTemplateParameters.get("testKey2"));
        assertEquals("n 3", testTemplateParameters.get("testKey3"));

        testTemplate = templateMap.get("testTemplateNotTestPlan");
        assertFalse(testTemplate.isTestPlan());
        assertEquals("testTemplateNotTestPlan", testTemplate.getName());
        assertEquals("/bin/templates/testTemplateNotTestPlan.jmx", testTemplate.getFileName());
        assertEquals("testTemplateNotTestPlan desc", testTemplate.getDescription());
        assertNull(testTemplate.getParameters());

        testTemplate = templateMap.get("testTemplate");
        assertTrue(testTemplate.isTestPlan());
        assertEquals("testTemplate", testTemplate.getName());
        assertEquals("/bin/templates/testTemplate.jmx", testTemplate.getFileName());
        assertEquals("testTemplate desc", testTemplate.getDescription());
        assertNull(testTemplate.getParameters());
    }

    @Test(expected = FileNotFoundException.class)
    public void testNonExistantXmlFileThrowsFileNotFoundException() throws Exception {
        File xmlTemplateFile = new File("missing.xml");
        TemplateManager.getInstance().parseTemplateFile(xmlTemplateFile);
    }

    @Test
    public void testInvalidTemplateXml() throws IOException, SAXException, ParserConfigurationException {
        try {
            String xmlTemplatePath = this.getClass().getResource("invalidTemplates.xml").getFile();
            File templateFile = new File(xmlTemplatePath);
            TemplateManager.getInstance().parseTemplateFile(templateFile);
        } catch (SAXParseException ex) {
            assertTrue("Exception did not contains expected message, got:" + ex.getMessage(),
                    ex.getMessage().contains("Element type \"key\" must be declared."));
        }
    }

    @Test
    public void testDifferentTemplatesAreNotEqual() {
        Template testTemplate1 = templateMap.get("testTemplateWithParameters");
        Template testTemplate2 = templateMap.get("testTemplateNotTestPlan");
        Template testTemplate3 = templateMap.get("testTemplate");

        assertNotEquals(testTemplate1, testTemplate2);
        assertNotEquals(testTemplate1, testTemplate3);
        assertNotEquals(testTemplate2, testTemplate1);
        assertNotEquals(testTemplate2, testTemplate3);
        assertNotEquals(testTemplate3, testTemplate1);
        assertNotEquals(testTemplate3, testTemplate2);

        assertNotEquals(testTemplate1.hashCode(), testTemplate2.hashCode());
        assertNotEquals(testTemplate1.hashCode(), testTemplate3.hashCode());
        assertNotEquals(testTemplate2.hashCode(), testTemplate1.hashCode());
        assertNotEquals(testTemplate2.hashCode(), testTemplate3.hashCode());
        assertNotEquals(testTemplate3.hashCode(), testTemplate1.hashCode());
        assertNotEquals(testTemplate3.hashCode(), testTemplate2.hashCode());
    }

    @Test
    public void testSameTemplatesAreEqual() {
        Template template = templateMap.get("testTemplateWithParameters");
        assertEquals(template, template);
        assertEquals(template.hashCode(), template.hashCode());
    }

    @Test
    public void testSameButParamsTemplatesAreNotEqual() {
        Template template1 = readTemplateFromFile().get("testTemplateWithParameters");
        Template template2 = readTemplateFromFile().get("testTemplateWithParameters");
        template2.setParameters(Collections.singletonMap("key", "value"));
        assertNotEquals(template1, template2);
        assertNotEquals(template1.hashCode(), template2.hashCode());
    }
}

@@ -5084,471 +5084,8 @@
 e2);
-%0A%0A        assertNotEquals(testTemplate1.hashCode(), testTemplate2.hashCode());%0A        assertNotEquals(testTemplate1.hashCode(), testTemplate3.hashCode());%0A        assertNotEquals(testTemplate2.hashCode(), testTemplate1.hashCode());%0A        assertNotEquals(testTemplate2.hashCode(), testTemplate3.hashCode());%0A        assertNotEquals(testTemplate3.hashCode(), testTemplate1.hashCode());%0A        assertNotEquals(testTemplate3.hashCode(), testTemplate2.hashCode());
 %0A   
@@ -5700,77 +5700,8 @@
 2);%0A
-        assertNotEquals(template1.hashCode(), template2.hashCode());%0A
     

Remove hashcode inequality tests because hashcodes might collide by accident