[java-metadata-aggregator COMMIT] /trunk/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/SetCacheD...

noreply at shibboleth.net noreply at shibboleth.net
Tue Jan 21 03:55:30 EST 2014


Author: iay
Date: Tue Jan 21 03:55:30 2014
New Revision: 325

URL: http://svn.shibboleth.net/view/java-metadata-aggregator?rev=325&view=rev
Log:
Make test tolerant of different JAXP implementations.

Modified:
    trunk/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/SetCacheDurationStageTest.java

Modified: trunk/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/SetCacheDurationStageTest.java
URL: http://svn.shibboleth.net/view/java-metadata-aggregator/trunk/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/SetCacheDurationStageTest.java?rev=325&r1=324&r2=325&view=diff
==============================================================================
--- trunk/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/SetCacheDurationStageTest.java (original)
+++ trunk/aggregator-pipeline/src/test/java/net/shibboleth/metadata/dom/saml/SetCacheDurationStageTest.java Tue Jan 21 03:55:30 2014
@@ -18,6 +18,10 @@
 package net.shibboleth.metadata.dom.saml;
 
 import java.util.ArrayList;
+import java.util.Date;
+
+import javax.xml.datatype.DatatypeConfigurationException;
+import javax.xml.datatype.DatatypeFactory;
 
 import net.shibboleth.metadata.Item;
 import net.shibboleth.metadata.dom.BaseDOMTest;
@@ -42,6 +46,21 @@
     }
 
     /**
+     * Helper method to extract the value of a descriptor's XML duration attribute in milliseconds.
+     * 
+     * @param descriptor EntitiesDescriptor or EntityDescriptor to pull the attribute from
+     * @return the cache duration attribute value converted to milliseconds
+     * @throws DatatypeConfigurationException if a {@link DatatypeFactory} can't be constructed
+     */
+    private long fetchDuration(Element descriptor) throws DatatypeConfigurationException {
+        final DatatypeFactory dtf = DatatypeFactory.newInstance();
+        final Attr cacheDurationAttr = AttributeSupport.getAttribute(descriptor,
+                SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME);
+        Assert.assertNotNull(cacheDurationAttr);
+        return dtf.newDuration(cacheDurationAttr.getValue()).getTimeInMillis(new Date());
+    }
+    
+    /**
      * Tests that the duration is properly set on an element when it doesn't already contain a duration.
      * 
      * @throws Exception thrown if there is an error
@@ -49,11 +68,12 @@
     @Test
     public void testWithoutExistingCacheDuration() throws Exception {
         final Element entitiesDescriptor = readXMLData("in.xml");
+        final Item<Element> item = new DOMElementItem(entitiesDescriptor);
 
         Assert.assertTrue(AttributeSupport.getAttribute(entitiesDescriptor, SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME) == null);
 
         final ArrayList<Item<Element>> metadataCollection = new ArrayList<>();
-        metadataCollection.add(new DOMElementItem(entitiesDescriptor));
+        metadataCollection.add(item);
 
         long duration = 123456;
         SetCacheDurationStage stage = new SetCacheDurationStage();
@@ -63,10 +83,7 @@
 
         stage.execute(metadataCollection);
 
-        Attr cacheDurationAttr = AttributeSupport.getAttribute(metadataCollection.iterator().next().unwrap(),
-                SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME);
-        Assert.assertNotNull(cacheDurationAttr);
-        Assert.assertEquals(cacheDurationAttr.getValue(), "PT2M3.456S");
+        Assert.assertEquals(fetchDuration(item.unwrap()), duration);
     }
 
     /**
@@ -77,13 +94,17 @@
     @Test
     public void testWithExistingCacheDuration() throws Exception {
         final Element entitiesDescriptor = readXMLData("in.xml");
+        final Item<Element> item = new DOMElementItem(entitiesDescriptor);
         
-        AttributeSupport.appendDurationAttribute(entitiesDescriptor, SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME, 987654);
+        final long originalDuration = 987654;
+        AttributeSupport.appendDurationAttribute(entitiesDescriptor, SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME,
+                originalDuration);
 
         Assert.assertTrue(AttributeSupport.getAttribute(entitiesDescriptor, SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME) != null);
+        Assert.assertEquals(fetchDuration(entitiesDescriptor), originalDuration);
 
         final ArrayList<Item<Element>> metadataCollection = new ArrayList<>();
-        metadataCollection.add(new DOMElementItem(entitiesDescriptor));
+        metadataCollection.add(item);
 
         long duration = 123456;
         SetCacheDurationStage stage = new SetCacheDurationStage();
@@ -93,10 +114,7 @@
 
         stage.execute(metadataCollection);
 
-        Attr cacheDurationAttr = AttributeSupport.getAttribute(entitiesDescriptor,
-                SAMLMetadataSupport.CACHE_DURATION_ATTRIB_NAME);
-        Assert.assertNotNull(cacheDurationAttr);
-        Assert.assertEquals(cacheDurationAttr.getValue(), "PT16M27.654S");
+        Assert.assertEquals(fetchDuration(item.unwrap()), duration);
     }
 
     /**



More information about the commits mailing list