/*
 * This file is part of the DITA Open Toolkit project.
 *
 * Copyright 2011 Jarno Elovirta
 *
 * See the accompanying LICENSE file for applicable license.
 */
package org.dita.dost.util;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.apache.xerces.impl.dtd.DTDGrammar;
import org.apache.xerces.impl.dtd.XMLDTDDescription;
import org.apache.xerces.xni.grammars.Grammar;
import org.junit.Test;


public class XMLGrammarPoolImplUtilsTest {

    @Test
    public void testCompareDescriptorsByPublicID() {
    	//#3568 We have two DITA topics located in different folders. The DOCTYPES are something like:
    	//<!DOCTYPE "publicID" "topic.dita">
    	//The DTD description for each of them has a different expanded system ID because the relative system ID
    	//is made absolute to the XML file location
    	//But the grammar will still be found because we consider the public ID to have more importance when comparing the descriptions.
    	File base = new File("abc.xml");
    	File expanded = new File("abc.dtd");
    	XMLDTDDescription desc1 = new XMLDTDDescription("publicID", "topic.dtd", base.toURI().toASCIIString(), expanded.toURI().toASCIIString(), "topic");
    	DTDGrammar grammar = new DTDGrammar(null, desc1);
    	XMLGrammarPoolImplUtils utils = new XMLGrammarPoolImplUtils();
    	utils.putGrammar(grammar);
    	
    	File base2 = new File("../abc.xml");
    	File expanded2 = new File("../abc.dtd");
    	XMLDTDDescription desc2 = new XMLDTDDescription("publicID", "topic.dtd", base2.toURI().toASCIIString(), expanded2.toURI().toASCIIString(), "topic");
    	Grammar retrieved = utils.getGrammar(desc2);
    	assertEquals("Should have retrieved the grammar, even though the expanded system ID differs", grammar, retrieved); 
    }
}

/*
 * This file is part of the DITA Open Toolkit project.
 *
 * Copyright 2020 Radu Coravu
 *
 * See the accompanying LICENSE file for applicable license.
 */
package org.dita.dost.util;

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

import java.io.File;

import org.apache.xerces.impl.dtd.DTDGrammar;
import org.apache.xerces.impl.dtd.XMLDTDDescription;
import org.apache.xerces.xni.grammars.Grammar;
import org.junit.Before;
import org.junit.Test;


public class XMLGrammarPoolImplUtilsTest {

	private XMLGrammarPoolImplUtils utils;

	@Before
	public void setUp() {
		utils = new XMLGrammarPoolImplUtils();
	}

	@Test
	public void testCompareDescriptorsByPublicID_same() {
		final XMLDTDDescription desc1 = getXmldtdDescription("file:/foo/abc.xml", "file:/foo/abc.dtd", "publicID");
		final DTDGrammar exp = new DTDGrammar(null, desc1);
		utils.putGrammar(exp);

		final XMLDTDDescription desc2 = getXmldtdDescription("file:/abc.xml", "file:/abc.dtd", "publicID");
		final Grammar act = utils.getGrammar(desc2);

		assertEquals(exp, act);
	}

	@Test
    public void testCompareDescriptorsByPublicID_different() {
		final XMLDTDDescription desc1 = getXmldtdDescription("file:/foo/abc.xml", "file:/foo/abc.dtd", "publicID");
		final DTDGrammar exp = new DTDGrammar(null, desc1);
    	utils.putGrammar(exp);

		final XMLDTDDescription desc2 = getXmldtdDescription("file:/abc.xml", "file:/abc.dtd", "differentId");
		final Grammar act = utils.getGrammar(desc2);

		assertNull(act);
    }

	@Test
	public void testCompareDescriptorsBySystemID_same() {
		final XMLDTDDescription desc1 = getXmldtdDescription("file:/foo/abc.xml", "file:/foo/abc.dtd", null);
		final DTDGrammar exp = new DTDGrammar(null, desc1);
		utils.putGrammar(exp);

		final XMLDTDDescription desc2 = getXmldtdDescription("file:/abc.xml", "file:/foo/abc.dtd", null);
		final Grammar act = utils.getGrammar(desc2);

		assertEquals(exp, act);
	}

	@Test
    public void testCompareDescriptorsBySystemID_different() {
		final XMLDTDDescription desc1 = getXmldtdDescription("file:/foo/abc.xml", "file:/foo/abc.dtd", null);
		final DTDGrammar exp = new DTDGrammar(null, desc1);
    	utils.putGrammar(exp);

		final XMLDTDDescription desc2 = getXmldtdDescription("file:/abc.xml", "file:/abc.dtd", null);
		final Grammar act = utils.getGrammar(desc2);

    	assertNull(act);
    }

	private XMLDTDDescription getXmldtdDescription(String base, String systemId, String publicId) {
		return new XMLDTDDescription(publicId, "topic.dtd", base, systemId, "topic");
	}

}

Improve grammar pool test