package test.at.gv.egovernment.moa.util; import java.io.FileInputStream; import java.util.Map; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import test.at.gv.egovernment.moa.*; import at.gv.egovernment.moa.util.Constants; import at.gv.egovernment.moa.util.DOMUtils; /** * @author Patrick Peck * @version $Id$ */ public class DOMUtilsTest extends MOATestCase { private static final String TESTDATA_BASE = TESTDATA_ROOT + "xml/"; private static boolean grammarsInitialized = false; /** * Constructor for DOMUtilsTest. * @param name */ public DOMUtilsTest(String name) { super(name); } protected void setUp() throws Exception { if (!grammarsInitialized) { // preparse XML schema DOMUtils.addSchemaToPool( getClass().getResourceAsStream(Constants.XML_SCHEMA_LOCATION), Constants.XML_NS_URI); // preparse XMLDsig Filter2 schema DOMUtils.addSchemaToPool( getClass().getResourceAsStream(Constants.DSIG_FILTER2_SCHEMA_LOCATION), Constants.DSIG_FILTER2_NS_URI); // preparse XMLDsig schema DOMUtils.addSchemaToPool( getClass().getResourceAsStream(Constants.DSIG_SCHEMA_LOCATION), Constants.DSIG_NS_URI); // preparse MOA schema DOMUtils.addSchemaToPool( getClass().getResourceAsStream(Constants.MOA_SCHEMA_LOCATION), Constants.MOA_NS_URI); grammarsInitialized = true; } } private Document parse(String fileName) throws Exception { return DOMUtils.parseDocument( new FileInputStream(fileName), true, Constants.ALL_SCHEMA_LOCATIONS, null); } public void testParseCreateXMLSignature() throws Exception { parse(TESTDATA_BASE + "CreateXMLSignature/TestGeneratorCX2.005.Req.xml"); parse(TESTDATA_BASE + "CreateXMLSignature/Req000.xml"); parse(TESTDATA_BASE + "CreateXMLSignature/Req001.xml"); parse(TESTDATA_BASE + "CreateXMLSignature/Req002.xml"); parse(TESTDATA_BASE + "CreateXMLSignature/Req004.xml"); } public void testParseVerifyCMSSignature() throws Exception { parse(TESTDATA_BASE + "VerifyCMSSignature/Req000.xml"); } public void testParseVerifyXMLSignature() throws Exception { parse(TESTDATA_BASE + "VerifyXMLSignature/Req000.xml"); parse(TESTDATA_BASE + "VerifyXMLSignature/Req001.xml"); parse(TESTDATA_BASE + "VerifyXMLSignature/Req002.xml"); parse(TESTDATA_BASE + "VerifyXMLSignature/TestGeneratorVX.002.Req.xml"); //parse(TESTDATA_BASE + "VerifyXMLSignature/TestGeneratorVX.006.Req.xml"); parse(TESTDATA_BASE + "VerifyXMLSignature/VerifySAMLRequest.xml"); } public void testParseInfobox() throws Exception { parse(TESTDATA_BASE + "Infobox/InfoboxReadResponseMOA4.xml"); parse(TESTDATA_BASE + "Infobox/InfoboxReadResponse.xml"); } private Document parsePlain(String fileName) throws Exception { return DOMUtils.parseDocument( new FileInputStream(fileName), false, null, null); } public void testValidateCreateXMLSignature() throws Exception { Document doc; boolean valid; // test a valid request doc = parsePlain(TESTDATA_BASE + "CreateXMLSignature/Req000.xml"); valid = DOMUtils.validateElement( doc.getDocumentElement(), Constants.ALL_SCHEMA_LOCATIONS, null); assertTrue(valid); // test an invalid request doc = parsePlain(TESTDATA_BASE + "CreateXMLSignature/invalid.xml"); try { valid = DOMUtils.validateElement( doc.getDocumentElement(), Constants.ALL_SCHEMA_LOCATIONS, null); fail(); } catch (Exception e) { } } public void testGetNamespaceDeclarations() throws Exception { Document doc; NodeList nl; Element elem; Map nsDecls; doc = parse(TESTDATA_BASE + "VerifyXMLSignature/Req002.xml"); nl = doc.getElementsByTagNameNS(Constants.DSIG_NS_URI, "Reference"); elem = (Element) nl.item(0); nsDecls = DOMUtils.getNamespaceDeclarations(elem); assertEquals(2, nsDecls.size()); assertEquals(Constants.DSIG_NS_URI, nsDecls.get("dsig")); assertEquals(Constants.MOA_NS_URI, nsDecls.get("")); } }