package de.tudresden.slr.model.bibtex.tests;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.StringWriter;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import de.tudresden.slr.model.bibtex.BibtexFactory;
import de.tudresden.slr.model.bibtex.Document;
import de.tudresden.slr.model.bibtex.util.BibtexResourceFactoryImpl;
import de.tudresden.slr.model.taxonomy.Model;
import de.tudresden.slr.model.taxonomy.TaxonomyFactory;
import de.tudresden.slr.model.taxonomy.Term;

public class BibtexTest {

	private ResourceSet resourceSet;
	private Resource resource;

	@BeforeClass
	public static void registerResourceFactory() {
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
				"bib", new BibtexResourceFactoryImpl());
	}

	@Before
	public void setUpFixtures() {
		resourceSet = new ResourceSetImpl();
		resource = resourceSet.createResource(URI.createURI("test.bib"));
	}

	@Test
	public void loadSingleDimensionWithoutSubTerms() throws Exception {
		final String bib = "@INPROCEEDINGS{Test01, classes = {test}}";

		resource.load(new URIConverter.ReadableInputStream(bib, "UTF-8"),
				Collections.EMPTY_MAP);

		assertThat(resource.getContents().get(0), instanceOf(Document.class));
		Document document = (Document) resource.getContents().get(0);
		Term term = document.getTaxonomy().getDimensions().get(0);
		assertThat(term.getName(), is("test"));
	}

	@Test
	public void storeSingleDimensionWithoutSubTerms() throws Exception {

		Model model = TaxonomyFactory.eINSTANCE.createModel();
		Term term = TaxonomyFactory.eINSTANCE.createTerm();

		term.setName("test");
		model.getDimensions().add(term);

		Document document = BibtexFactory.eINSTANCE.createDocument();
		document.setTaxonomy(model);
		document.setKey("test02");
		document.setType("article");

		StringWriter writer = new StringWriter();
		resource.getContents().add(document);
		resource.save(new URIConverter.WriteableOutputStream(writer, "UTF-8"),
				Collections.EMPTY_MAP);

		String bib = writer.toString();
		assertThat(bib, containsString("test02"));
	}

	@Test
	public void loadChangeAndStoreABibtexResource() throws Exception {
		final String bibIn = "@INPROCEEDINGS{Test01, classes = {test}}";
		StringWriter writer = new StringWriter();
		resource.load(new URIConverter.ReadableInputStream(bibIn, "UTF-8"),
				Collections.EMPTY_MAP);

		Term subTerm1 = TaxonomyFactory.eINSTANCE.createTerm();
		subTerm1.setName("sub-test1");
		Term subTerm2 = TaxonomyFactory.eINSTANCE.createTerm();
		subTerm2.setName("sub-test2");

		Document document = (Document) resource.getContents().get(0);
		Term term = document.getTaxonomy().getDimensions().get(0);
		term.getSubclasses().add(subTerm1);
		term.getSubclasses().add(subTerm2);

		resource.save(new URIConverter.WriteableOutputStream(writer, "UTF-8"),
				Collections.EMPTY_MAP);
		final String bibOut = writer.toString();
		final String expected = "@INPROCEEDINGS{Test01,\n\tclasses = {test{sub-test1, sub-test2}}\n}";
		assertThat(bibOut, is(expected));
	}

	@Test
	public void loadSingleDimensionWithSubTerms() throws Exception {
		final String bib = "@INPROCEEDINGS{Test01, classes = {dimension{sub{subsub}}}}";

		resource.load(new URIConverter.ReadableInputStream(bib, "UTF-8"),
				Collections.EMPTY_MAP);

		assertThat(resource.getContents().get(0), instanceOf(Document.class));

		Document document = (Document) resource.getContents().get(0);
		Term dimension = document.getTaxonomy().getDimensions().get(0);
		Term subDimension = dimension.getSubclasses().get(0);
		Term subSubDimension = subDimension.getSubclasses().get(0);

		assertThat(dimension.getName(), is("dimension"));
		assertThat(subDimension.getName(), is("sub"));
		assertThat(subSubDimension.getName(), is("subsub"));
	}
} // BibtexTests

@@ -4206,16 +4206,660 @@
 b%22));%0A%09%7D
+%0A%0A%09@Test%0A%09public void loadTwoDimensions() throws Exception %7B%0A%09%09final String bib = %22@INPROCEEDINGS%7BTest01, classes = %7Bdimension1%7Bsubterm1%7D dimension2%7Bsubterm2%7D%7D%7D%22;%0A%0A%09%09resource.load(new URIConverter.ReadableInputStream(bib, %22UTF-8%22),%0A%09%09%09%09Collections.EMPTY_MAP);%0A%0A%09%09assertThat(resource.getContents().get(0), instanceOf(Document.class));%0A%0A%09%09Document document = (Document) resource.getContents().get(0);%0A%09%09Term dimension1 = document.getTaxonomy().getDimensions().get(0);%0A%09%09Term dimension2 = document.getTaxonomy().getDimensions().get(1);%0A%0A%09%09assertThat(dimension1.getName(), is(%22dimension1%22));%0A%09%09assertThat(dimension2.getName(), is(%22dimension2%22));%0A%09%7D
 %0A%7D // Bi

Add test for two dimensions