package beaform.importer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Test;
import org.xml.sax.SAXException;

import beaform.entities.FormulaTag;

@SuppressWarnings("static-method")
public class TagHandlerTest {

	@Test
	public void testEndTags() throws SAXException {
		final MockedSAXHandlerMaster handlerMaster = new MockedSAXHandlerMaster();
		final TagHandler handler = new TagHandler(handlerMaster);

		handler.endElement("", "", "tags");

		assertTrue("The active handler hasn't changed", handlerMaster.hasChanged());
	}

	@Test
	public void testTagHandler() throws SAXException {
		final String firstTag = "firstTag";
		final String secondTag = "secondTag";
		final TagHandler handler = new TagHandler(new MockedSAXHandlerMaster());

		parseTagElement(firstTag, handler);
		parseTagElement(secondTag, handler);

		List<FormulaTag> newTags = handler.getTags();
		assertEquals("This isn't the expected number of tags", 2, newTags.size());
		assertEquals("This isn't the expected tag", firstTag, newTags.get(0).getName());
		assertEquals("This isn't the expected tag", secondTag, newTags.get(1).getName());
	}

	private void parseTagElement(final String tagName, final TagHandler handler) throws SAXException {
		handler.startElement("", "", "tag", null);
		handler.characters(tagName.toCharArray(), 0, tagName.length());
		handler.endElement("", "", "tag");
	}

}

@@ -1448,10 +1448,902 @@
 %22);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testWithForeignTag() throws SAXException %7B%0A%09%09final String firstTag = %22firstTag%22;%0A%09%09final String secondTag = %22secondTag%22;%0A%09%09final TagHandler handler = new TagHandler(new MockedSAXHandlerMaster());%0A%0A%09%09parseTagElement(firstTag, handler);%0A%09%09parseTagElement(secondTag, handler);%0A%09%09parseUnknownElement(%22some stuff%22, handler);%0A%0A%09%09List%3CFormulaTag%3E newTags = handler.getTags();%0A%09%09assertEquals(%22This isn't the expected number of tags%22, 2, newTags.size());%0A%09%09assertEquals(%22This isn't the expected tag%22, firstTag, newTags.get(0).getName());%0A%09%09assertEquals(%22This isn't the expected tag%22, secondTag, newTags.get(1).getName());%0A%09%7D%0A%0A%09private void parseUnknownElement(final String text, final TagHandler handler) throws SAXException %7B%0A%09%09handler.startElement(%22%22, %22%22, %22unknown%22, null);%0A%09%09handler.characters(text.toCharArray(), 0, text.length());%0A%09%09handler.endElement(%22%22, %22%22, %22unknown%22);%0A%09%7D%0A%0A
 %7D%0A

Add test for parsing unknown items in the tag handler