[java-opensaml COMMIT] /trunk/opensaml-core/src/test/java/org/opensaml/core/xml/XMLObjectBaseTestCase.java

noreply at shibboleth.net noreply at shibboleth.net
Fri Jan 24 17:31:40 EST 2014


Author: scantor
Date: Fri Jan 24 17:31:40 2014
New Revision: 3573

URL: http://svn.shibboleth.net/view/java-opensaml?rev=3573&view=rev
Log:
Add a method to convert duration attributes into milliseconds.

Modified:
    trunk/opensaml-core/src/test/java/org/opensaml/core/xml/XMLObjectBaseTestCase.java

Modified: trunk/opensaml-core/src/test/java/org/opensaml/core/xml/XMLObjectBaseTestCase.java
URL: http://svn.shibboleth.net/view/java-opensaml/trunk/opensaml-core/src/test/java/org/opensaml/core/xml/XMLObjectBaseTestCase.java?rev=3573&r1=3572&r2=3573&view=diff
==============================================================================
--- trunk/opensaml-core/src/test/java/org/opensaml/core/xml/XMLObjectBaseTestCase.java (original)
+++ trunk/opensaml-core/src/test/java/org/opensaml/core/xml/XMLObjectBaseTestCase.java Fri Jan 24 17:31:40 2014
@@ -21,16 +21,24 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+import javax.annotation.Nonnull;
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
 import javax.xml.namespace.QName;
 
+import net.shibboleth.utilities.java.support.xml.AttributeSupport;
 import net.shibboleth.utilities.java.support.xml.ParserPool;
 import net.shibboleth.utilities.java.support.xml.QNameSupport;
 import net.shibboleth.utilities.java.support.xml.SerializeSupport;
 import net.shibboleth.utilities.java.support.xml.XMLParserException;
 
 import org.custommonkey.xmlunit.Diff;
+
 import net.shibboleth.utilities.java.support.xml.XMLAssertTestNG;
+
 import org.custommonkey.xmlunit.XMLUnit;
 import org.opensaml.core.OpenSAMLInitBaseTestCase;
 import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
@@ -45,6 +53,7 @@
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -72,6 +81,26 @@
     /** QName for SimpleXMLObject */
     protected static QName simpleXMLObjectQName = new QName(SimpleXMLObject.NAMESPACE, SimpleXMLObject.LOCAL_NAME);
 
+    /** Baseline for duration calculations (comes from XML Schema standard). */
+    private static Calendar baseline = new GregorianCalendar(1696, 9, 1, 0, 0, 0);
+    
+    /**
+     * Helper method to extract the value of an XML duration attribute in milliseconds.
+     * 
+     * @param element element to pull attribute from
+     * @param name  name of attribute
+     * 
+     * @return the attribute value converted to milliseconds
+     * @throws DatatypeConfigurationException if a {@link DatatypeFactory} can't be constructed
+     */
+    public static long fetchDuration(@Nonnull final Element element, @Nonnull final QName name)
+            throws DatatypeConfigurationException {
+        final DatatypeFactory dtf = DatatypeFactory.newInstance();
+        final Attr attr = AttributeSupport.getAttribute(element, name);
+        Assert.assertNotNull(attr);
+        return dtf.newDuration(attr.getValue()).getTimeInMillis(baseline);
+    }
+    
     @BeforeClass
 	protected void initXMLObjectSupport() throws Exception {
         XMLUnit.setIgnoreWhitespace(true);



More information about the commits mailing list