Author: claude
Date: Sun Sep 22 08:25:49 2013
New Revision: 1525332

URL: http://svn.apache.org/r1525332
Log:
Cleaned up prefixmapping tests

Modified:
    
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/graph/AbstractGraphPrefixMappingTest.java
    
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/shared/AbstractPrefixMappingTest.java
    
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/testing_framework/TestUtils.java

Modified: 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/graph/AbstractGraphPrefixMappingTest.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/graph/AbstractGraphPrefixMappingTest.java?rev=1525332&r1=1525331&r2=1525332&view=diff
==============================================================================
--- 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/graph/AbstractGraphPrefixMappingTest.java
 (original)
+++ 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/graph/AbstractGraphPrefixMappingTest.java
 Sun Sep 22 08:25:49 2013
@@ -45,9 +45,9 @@ public abstract class AbstractGraphPrefi
        public void testGraphPrefixMapping() {
                Graph g = getGraphProducer().newGraph();
                PrefixMapping pm = g.getPrefixMapping();
-               pm.setNsPrefix("crisp", crispURI);
-               pm.setNsPrefix("butter", butterURI);
+               pm.setNsPrefix("pfx1", AbstractPrefixMappingTest.httpURI);
+               pm.setNsPrefix("pfx2", AbstractPrefixMappingTest.urnURI);
 
-               assertTrue(pm == g.getPrefixMapping());
+               assertSame(pm, g.getPrefixMapping());
        }
 }

Modified: 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/shared/AbstractPrefixMappingTest.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/shared/AbstractPrefixMappingTest.java?rev=1525332&r1=1525331&r2=1525332&view=diff
==============================================================================
--- 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/shared/AbstractPrefixMappingTest.java
 (original)
+++ 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/shared/AbstractPrefixMappingTest.java
 Sun Sep 22 08:25:49 2013
@@ -22,18 +22,13 @@ import static org.junit.Assert.*;
 import static com.hp.hpl.jena.testing_framework.GraphHelper.*;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
-import org.junit.Assert;
 import org.junit.Test;
 
-import com.hp.hpl.jena.graph.Factory;
-import com.hp.hpl.jena.graph.Graph;
-import com.hp.hpl.jena.rdf.model.AbstractModelPrefixMappingTest;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-
 /**
  * Test prefix mappings - subclass this test and override getMapping() to
  * deliver the prefixMapping to be tested.
@@ -43,160 +38,82 @@ public abstract class AbstractPrefixMapp
 
        /**
         * Subclasses implement to return a new, empty prefixMapping of their
-        * preferred kind.
+        * preferred implementation.
         */
        abstract protected PrefixMapping getMapping();
+       
+       static final String[] illegalPrefixes = { "<hello>", "has:colon", "with 
a space",
+       "-dashStart",  "has#octothorp",  "has/slash", "1numericStart", 
".dotStart" };
 
-       public static final String crispURI = "http://crisp.nosuch.net/";;
-       public static final String ropeURI = "scheme:rope/string#";
-       public static final String butterURI = 
"ftp://ftp.nowhere.at.all/cream#";;
-
-       /**
-        * The empty prefix is specifically allowed [for the default namespace].
-        */
-       @Test
-       public void testEmptyPrefix() {
-               PrefixMapping pm = getMapping();
-               pm.setNsPrefix("", crispURI);
-               assertEquals(crispURI, pm.getNsPrefixURI(""));
-       }
-
-       static final String[] badNames = { "<hello>", "foo:bar", "with a space",
-                       "-argument" };
-
-       /**
-        * Test that various illegal names are trapped.
-        */
-       @Test
-       public void testCheckNames() {
-               PrefixMapping ns = getMapping();
-               for (int i = 0; i < badNames.length; i += 1) {
-                       String bad = badNames[i];
-                       try {
-                               ns.setNsPrefix(bad, crispURI);
-                               fail("'" + bad + "' is an illegal prefix and 
should be trapped");
-                       } catch (PrefixMapping.IllegalPrefixException e) {
-                               // expected
-                       }
-               }
-       }
+       public static final String httpURI = "http://example.com/";;
+       public static final String httpPrefix = "hpfx";
 
-       @Test
-       public void testNullURITrapped() {
-               try {
-                       getMapping().setNsPrefix("xy", null);
-                       fail("shouild trap null URI in setNsPrefix");
-               } catch (NullPointerException e) {
-                       // expected
-               }
-       }
+       public static final String urnURI = "scheme:rope/string#";
+       public static final String urnPrefix = "upfx";
 
-       /**
-        * test that a PrefixMapping maps names to URIs. The names and URIs are 
all
-        * fully distinct - overlapping names/uris are dealt with in other 
tests.
-        */
-       @Test
-       public void testPrefixMappingMapping() {
-               String toast = "ftp://ftp.nowhere.not/";;
-               assertDiffer("crisp and toast must differ", crispURI, toast);
-               /* */
-               PrefixMapping ns = getMapping();
-               assertEquals("crisp should be unset", null, 
ns.getNsPrefixURI("crisp"));
-               assertEquals("toast should be unset", null, 
ns.getNsPrefixURI("toast"));
-               assertEquals("butter should be unset", null,
-                               ns.getNsPrefixURI("butter"));
-               /* */
-               ns.setNsPrefix("crisp", crispURI);
-               assertEquals("crisp should be set", crispURI,
-                               ns.getNsPrefixURI("crisp"));
-               assertEquals("toast should still be unset", null,
-                               ns.getNsPrefixURI("toast"));
-               assertEquals("butter should still be unset", null,
-                               ns.getNsPrefixURI("butter"));
-               /* */
-               ns.setNsPrefix("toast", toast);
-               assertEquals("crisp should be set", crispURI,
-                               ns.getNsPrefixURI("crisp"));
-               assertEquals("toast should be set", toast, 
ns.getNsPrefixURI("toast"));
-               assertEquals("butter should still be unset", null,
-                               ns.getNsPrefixURI("butter"));
-       }
+       public static final String[] prefixes = { httpPrefix, urnPrefix };
+       public static final String[] uris = { httpURI, urnURI };
 
-       /**
-        * Test that we can run the prefix mapping in reverse - from URIs to
-        * prefixes. uriB is a prefix of uriA to try and ensure that the 
ordering of
-        * the map doesn't matter.
-        */
-       @Test
-       public void testReversePrefixMapping() {
-               PrefixMapping ns = getMapping();
-               String uriA = "http://jena.hpl.hp.com/A#";, uriB = 
"http://jena.hpl.hp.com/";;
-               String uriC = "http://jena.hpl.hp.com/Csharp/";;
-               String prefixA = "aa", prefixB = "bb";
-               ns.setNsPrefix(prefixA, uriA).setNsPrefix(prefixB, uriB);
-               assertEquals(null, ns.getNsURIPrefix(uriC));
-               assertEquals(prefixA, ns.getNsURIPrefix(uriA));
-               assertEquals(prefixB, ns.getNsURIPrefix(uriB));
+       private String createfqn( String prefix, String suffix )
+       {
+               return String.format( "%s:%s", prefix, suffix );
        }
-
+       
        /**
         * test that we can extract a proper Map from a PrefixMapping
         */
        @Test
-       public void testPrefixMappingMap() {
-               PrefixMapping ns = getCrispyRope();
-               Map<String, String> map = ns.getNsPrefixMap();
+       public void testGetNsPrefixMap() {
+               PrefixMapping pm = getConfiguredMapping();
+               Map<String, String> map = pm.getNsPrefixMap();
                assertEquals("map should have two elements", 2, map.size());
-               assertEquals(crispURI, map.get("crisp"));
-               assertEquals("scheme:rope/string#", map.get("rope"));
-       }
-
-       /**
-        * test that the Map returned by getNsPrefixMap does not alias (parts 
of)
-        * the secret internal map of the PrefixMapping
-        */
-       @Test
-       public void testPrefixMappingSecret() {
-               PrefixMapping ns = getCrispyRope();
-               Map<String, String> map = ns.getNsPrefixMap();
-               /* */
-               map.put("crisp", "with/onions");
-               map.put("sandwich", "with/cheese");
-               assertEquals(crispURI, ns.getNsPrefixURI("crisp"));
-               assertEquals(ropeURI, ns.getNsPrefixURI("rope"));
-               assertEquals(null, ns.getNsPrefixURI("sandwich"));
+               assertEquals(httpURI, map.get(httpPrefix));
+               assertEquals(urnURI, map.get(urnPrefix));
+               
+               // verify adding to map does not change PrefixMap.
+               map.put("newPfx", "http://example.com/new";);
+               assertNull( "Should not have added new prefix", 
pm.getNsPrefixURI("newPfx"));
+               assertNull( "Should not have added new uri", 
pm.getNsURIPrefix("http://example.com/new";));
+               
+               // verify changing map does not change PrefixMap
+               map.put( httpPrefix, urnURI );
+               assertEquals(httpURI, pm.getNsPrefixURI(httpPrefix));
+               assertEquals(urnPrefix, pm.getNsURIPrefix(urnURI));
+               
        }
 
-       private PrefixMapping getCrispyRope() {
+       private PrefixMapping getConfiguredMapping() {
                PrefixMapping ns = getMapping();
-               ns.setNsPrefix("crisp", crispURI);
-               ns.setNsPrefix("rope", ropeURI);
+               ns.setNsPrefix(httpPrefix, httpURI);
+               ns.setNsPrefix(urnPrefix, urnURI);
                return ns;
        }
 
-       /**
-        * these are strings that should not change when they are 
prefix-expanded
-        * with crisp and rope as legal prefixes.
-        */
-       static final String[] dontChange = { "",
-                       "http://www.somedomain.something/whatever#";, 
"crispy:cabbage",
-                       "cris:isOnInfiniteEarths", "rop:tangled/web", 
"roped:abseiling" };
-
-       /**
-        * these are the required mappings which the test cases below should
-        * satisfy: an array of 2-arrays, where element 0 is the string to 
expand
-        * and element 1 is the string it should expand to.
-        */
-       static final String[][] expansions = {
-                       { "crisp:pathPart", crispURI + "pathPart" },
-                       { "rope:partPath", ropeURI + "partPath" },
-                       { "crisp:path:part", crispURI + "path:part" }, };
 
        @Test
-       public void testExpandPrefix() {
-               PrefixMapping ns = getMapping();
-               ns.setNsPrefix("crisp", crispURI);
-               ns.setNsPrefix("rope", ropeURI);
+       public void testExpandPrefix_String() {
+               
+               /**
+                * these are the required mappings which the test cases below 
should
+                * satisfy: an array of 2-arrays, where element 0 is the string 
to expand
+                * and element 1 is the string it should expand to.
+                */
+               String[][] expansions = {
+                               { createfqn(httpPrefix, "pathPart"), httpURI + 
"pathPart" },
+                               { createfqn(urnPrefix, "partPath"), urnURI + 
"partPath" },
+                               { createfqn(httpPrefix, "path:part"), httpURI + 
"path:part" }, };
+               
+               /**
+                * these are strings that should not change when they are 
prefix-expanded
+                * with crisp and rope as legal prefixes.
+                */
+               String[] dontChange = { "",
+                               "http://www.somedomain.something/whatever#";, 
"crispy:cabbage",
+                               "cris:isOnInfiniteEarths", "rop:tangled/web", 
"roped:abseiling" };
+
+               
+               PrefixMapping ns = getConfiguredMapping();
+               
                /* */
                for (int i = 0; i < dontChange.length; i += 1)
                        assertEquals("should be unchanged", dontChange[i],
@@ -208,190 +125,173 @@ public abstract class AbstractPrefixMapp
        }
 
        @Test
-       public void testUseEasyPrefix() {
-               testUseEasyPrefix( getMapping());
-               testShortForm( getMapping());
-       }
-
-       public static void testUseEasyPrefix( PrefixMapping ns) {
-               testShortForm( ns);
-       }
-
-       public static void testShortForm(PrefixMapping ns) {
-               ns.setNsPrefix("crisp", crispURI);
-               ns.setNsPrefix("butter", butterURI);
+       public void testShortForm_String() {
+               PrefixMapping ns = getConfiguredMapping();
                assertEquals("", ns.shortForm(""));
-               assertEquals( ropeURI, ns.shortForm(ropeURI));
-               assertEquals( "crisp:tail", ns.shortForm(crispURI + "tail"));
-               assertEquals( "butter:here:we:are",
-                               ns.shortForm(butterURI + "here:we:are"));
-       }
-
-       @Test
-       public void testEasyQName() {
-               PrefixMapping ns = getMapping();
-               String alphaURI = "http://seasonal.song/preamble/";;
-               ns.setNsPrefix("alpha", alphaURI);
-               assertEquals("alpha:rowboat", ns.qnameFor(alphaURI + 
"rowboat"));
-       }
-
-       @Test
-       public void testNoQNameNoPrefix() {
-               PrefixMapping ns = getMapping();
-               String alphaURI = "http://seasonal.song/preamble/";;
-               ns.setNsPrefix("alpha", alphaURI);
-               assertEquals(null, ns.qnameFor("eg:rowboat"));
-       }
-
-       @Test
-       public void testNoQNameBadLocal() {
-               PrefixMapping ns = getMapping();
-               String alphaURI = "http://seasonal.song/preamble/";;
-               ns.setNsPrefix("alpha", alphaURI);
-               assertEquals(null, ns.qnameFor(alphaURI + "12345"));
+               assertEquals( "should not change", "foo:bar/stuff", 
ns.shortForm( "foo:bar/stuff") );
+               for (int i=0;i<prefixes.length;i++)
+               {
+                       logAssertEquals( this.getClass(), "matching uri should 
return uri", uris[i], ns.shortForm( uris[i] ));
+                       
+                       assertEquals( createfqn( prefixes[i], "tail"), 
ns.shortForm(uris[i] + "tail"));
+                       assertEquals( createfqn( prefixes[i], "two/segments"), 
ns.shortForm(uris[i] + "two/segments"));
+                       assertEquals( createfqn( prefixes[i], 
"colon:separated"), ns.shortForm(uris[i] + "colon:separated"));
+               }
        }
 
-       /**
-        * The tests implied by the email where Chris suggested adding qnameFor;
-        * shortForm generates illegal qnames but qnameFor does not.
-        */
        @Test
-       public void testQnameFromEmail() {
-               String uri = "http://some.long.uri/for/a/namespace#";;
-               PrefixMapping ns = getMapping();
-               ns.setNsPrefix("x", uri);
-               assertEquals(null, ns.qnameFor(uri));
-               assertEquals(null, ns.qnameFor(uri + "non/fiction"));
+       public void testQnameFor_String() {
+               PrefixMapping ns = getConfiguredMapping();
+               
+               assertNull("Should return null if the ns is not registered", 
ns.qnameFor("eg:rowboat"));
+               
+               for (int i=0;i<prefixes.length;i++)
+               {
+                       assertNull("Should return null if local name starts 
with number", ns.qnameFor(uris[i] + "12345"));
+                       assertNull("Should return null if it is URI only", 
ns.qnameFor( uris[i] ));
+                       assertNull("Should return null if local name contains a 
slash", ns.qnameFor( uris[i]+"non/fiction" ));
+                       
+                       assertEquals("Should create qName for "+uris[i] + 
"rowboat", createfqn( prefixes[i], "rowboat"), ns.qnameFor(uris[i] + 
"rowboat"));
+               
+                       // add long version
+                       ns.setNsPrefix( prefixes[i]+"Long", uris[i]+"long/");
+                       assertEquals("Should return long version if local name 
contains a slash", createfqn( prefixes[i]+"Long", "name" ), ns.qnameFor( 
uris[i]+"long/name" ));
+               
+               }
        }
+       
 
-       /**
-        * test that we can add the maplets from another PrefixMapping without
-        * losing our own.
-        */
+       
        @Test
-       public void testAddOtherPrefixMapping() {
+       public void testSetNsPrefixes_PrefixMapping() {
                PrefixMapping a = getMapping();
                PrefixMapping b = getMapping();
                assertFalse("must have two diffferent maps", a == b);
-               a.setNsPrefix("crisp", crispURI);
-               a.setNsPrefix("rope", ropeURI);
-               b.setNsPrefix("butter", butterURI);
-               assertEquals(null, b.getNsPrefixURI("crisp"));
-               assertEquals(null, b.getNsPrefixURI("rope"));
-               b.setNsPrefixes(a);
-               checkContainsMapping(b);
+               a.setNsPrefix(httpPrefix, httpURI);
+               a.setNsPrefix("duplicate", "http://example.com/duplicate";);
+               b.setNsPrefix(urnPrefix, urnURI);
+               b.setNsPrefix("duplicate", "http://example.com/duplicate2";);
+               assertEquals(null, b.getNsPrefixURI(httpPrefix));
+               assertEquals(null, a.getNsPrefixURI(urnPrefix));
+               assertSame( "setPrefixes( PrefixMapping) must return 
prefixMapping", b, b.setNsPrefixes(a) );
+               assertEquals(httpURI, b.getNsPrefixURI(httpPrefix));
+               assertEquals(urnURI, b.getNsPrefixURI(urnPrefix));
+               
+               // verify duplicate was changed
+               assertEquals( "duplicate should now match 'a' version", 
"http://example.com/duplicate";, b.getNsPrefixURI("duplicate" ));
+               
        }
 
-       private void checkContainsMapping(PrefixMapping b) {
-               assertEquals(crispURI, b.getNsPrefixURI("crisp"));
-               assertEquals(ropeURI, b.getNsPrefixURI("rope"));
-               assertEquals(butterURI, b.getNsPrefixURI("butter"));
-       }
 
+       @Test
+       public void testGetNsPrefixURI_String()
+       {
+               PrefixMapping pm = getConfiguredMapping();
+               assertEquals( httpURI, pm.getNsPrefixURI(httpPrefix));
+               assertEquals( urnURI, pm.getNsPrefixURI(urnPrefix));
+               assertNull( "Missing prefix should return null", 
pm.getNsPrefixURI("missingPrefix"));
+       }
+       
+       @Test
+       public void testGetNsURIPrefix_String()
+       {
+               PrefixMapping pm = getConfiguredMapping();
+               assertEquals( httpPrefix, pm.getNsURIPrefix(httpURI));
+               assertEquals( urnPrefix, pm.getNsURIPrefix(urnURI));
+               assertNull( "missing URI must return null", 
pm.getNsURIPrefix("http://example.com/misisng";));
+               
+               // setup a duplicate prefix 
+               pm.setNsPrefix(httpPrefix+"2", httpURI);
+               String foundPrefix = pm.getNsURIPrefix( httpURI );
+               assertNotNull( "duplicated URI must return a prefix", 
foundPrefix );
+               assertEquals( "Must return most recently added in this case", 
httpPrefix+"2",foundPrefix);
+               
+               // test removing prefix uncovers previous map
+               pm.removeNsPrefix(httpPrefix+"2");
+               assertEquals( "Must return earlier prefix in this case", 
httpPrefix ,pm.getNsURIPrefix( httpURI ));
+       }
+       
        /**
         * as for testAddOtherPrefixMapping, except that it's a plain Map we're
         * adding.
         */
        @Test
-       public void testAddMap() {
+       public void testSetNsPrefixes_Map() {
                PrefixMapping b = getMapping();
                Map<String, String> map = new HashMap<String, String>();
-               map.put("crisp", crispURI);
-               map.put("rope", ropeURI);
-               b.setNsPrefix("butter", butterURI);
+               map.put(httpPrefix, httpURI);
+               map.put("over", "http://example.com/overwritten/";);
+               b.setNsPrefix(urnPrefix, urnURI);
+               b.setNsPrefix("over", "overwriteme");
                b.setNsPrefixes(map);
-               checkContainsMapping(b);
+               assertEquals(httpURI, b.getNsPrefixURI(httpPrefix));
+               assertEquals(urnURI, b.getNsPrefixURI(urnPrefix));
+               assertEquals("http://example.com/overwritten/";, 
b.getNsPrefixURI("over"));
        }
 
        @Test
-       public void testAddDefaultMap() {
+       public void testWithDefaultMappings_PrefixMap() {
                PrefixMapping pm = getMapping();
                PrefixMapping root = PrefixMapping.Factory.create();
-               pm.setNsPrefix("a", "aPrefix:");
-               pm.setNsPrefix("b", "bPrefix:");
-               root.setNsPrefix("a", "pootle:");
-               root.setNsPrefix("z", "bPrefix:");
-               root.setNsPrefix("c", "cootle:");
+               pm.setNsPrefix("a", "aFirst#");
+               pm.setNsPrefix("b", "bFirst#");
+               root.setNsPrefix("a", "aSecond/");
+               root.setNsPrefix("z", "bFirst#"); // an alias
+               root.setNsPrefix("c", "cSecond/");
+               // should only add c from root
                assertSame(pm, pm.withDefaultMappings(root));
-               assertEquals("aPrefix:", pm.getNsPrefixURI("a"));
+               assertEquals("aFirst#", pm.getNsPrefixURI("a"));
                assertEquals(null, pm.getNsPrefixURI("z"));
-               assertEquals("bPrefix:", pm.getNsPrefixURI("b"));
-               assertEquals("cootle:", pm.getNsPrefixURI("c"));
+               assertEquals("bFirst#", pm.getNsPrefixURI("b"));
+               assertEquals("cSecond/", pm.getNsPrefixURI("c"));
        }
 
        @Test
        public void testSecondPrefixRetainsExistingMap() {
                PrefixMapping A = getMapping();
-               A.setNsPrefix("a", crispURI);
-               A.setNsPrefix("b", crispURI);
-               assertEquals(crispURI, A.getNsPrefixURI("a"));
-               assertEquals(crispURI, A.getNsPrefixURI("b"));
+               A.setNsPrefix("a", httpURI);
+               A.setNsPrefix("b", httpURI);
+               assertEquals(httpURI, A.getNsPrefixURI("a"));
+               assertEquals(httpURI, A.getNsPrefixURI("b"));
        }
 
        @Test
        public void testSecondPrefixReplacesReverseMap() {
                PrefixMapping A = getMapping();
-               A.setNsPrefix("a", crispURI);
-               A.setNsPrefix("b", crispURI);
-               assertEquals("b", A.getNsURIPrefix(crispURI));
+               A.setNsPrefix("a", httpURI);
+               A.setNsPrefix("b", httpURI);
+               assertEquals("b", A.getNsURIPrefix(httpURI));
        }
 
        @Test
-       public void testSecondPrefixDeletedUncoversPreviousMap() {
-               PrefixMapping A = getMapping();
-               A.setNsPrefix("x", crispURI);
-               A.setNsPrefix("y", crispURI);
-               A.removeNsPrefix("y");
-               assertEquals("x", A.getNsURIPrefix(crispURI));
+       public void testRemoveNsPrefix_String() {
+               
+               PrefixMapping pm = getConfiguredMapping();
+               
+               pm.setNsPrefix("", "http://example.com/default";);
+               
+               int sz = pm.getNsPrefixMap().keySet().size();
+               // test removing missing entries
+               assertSame( pm, pm.removeNsPrefix("nothere"));
+               assertEquals( "No entries should have been removed", sz, 
pm.getNsPrefixMap().keySet().size());
+               
+               // test removing an illegal prefix does not fail
+               assertSame( pm, pm.removeNsPrefix( illegalPrefixes[0] ));
+               assertEquals( "No entries should have been removed", sz, 
pm.getNsPrefixMap().keySet().size());
+               
+               // test removing a real entry
+               assertSame( pm, pm.removeNsPrefix(urnPrefix));
+               assertEquals(null, pm.getNsPrefixURI(urnPrefix));
+               assertEquals(httpURI, pm.getNsPrefixURI(httpPrefix));
+               assertEquals("http://example.com/default";, 
pm.getNsPrefixURI(""));
+               
+               // test remove defalut
+               assertSame( pm, pm.removeNsPrefix(""));
+               assertEquals(null, pm.getNsPrefixURI(""));                      
        }
 
-       /**
-        * Test that the empty prefix does not wipe an existing prefix for the 
same
-        * URI.
-        */
        @Test
-       public void testEmptyDoesNotWipeURI() {
-               PrefixMapping pm = getMapping();
-               pm.setNsPrefix("frodo", ropeURI);
-               pm.setNsPrefix("", ropeURI);
-               assertEquals(ropeURI, pm.getNsPrefixURI("frodo"));
-       }
-
-       /**
-        * Test that adding a new prefix mapping for U does not throw away a 
default
-        * mapping for U.
-        */
-       @Test
-       public void testSameURIKeepsDefault() {
-               PrefixMapping A = getMapping();
-               A.setNsPrefix("", crispURI);
-               A.setNsPrefix("crisp", crispURI);
-               assertEquals(crispURI, A.getNsPrefixURI(""));
-       }
-
-       @Test
-       public void testReturnsSelf() {
-               PrefixMapping A = getMapping();
-               assertSame(A, A.setNsPrefix("crisp", crispURI));
-               assertSame(A, A.setNsPrefixes(A));
-               assertSame(A, A.setNsPrefixes(new HashMap<String, String>()));
-               assertSame(A, A.removeNsPrefix("rhubarb"));
-       }
-
-       @Test
-       public void testRemovePrefix() {
-               String hURI = "http://test.remove.prefixes/prefix#";;
-               String bURI = "http://other.test.remove.prefixes/prefix#";;
-               PrefixMapping A = getMapping();
-               A.setNsPrefix("hr", hURI);
-               A.setNsPrefix("br", bURI);
-               A.removeNsPrefix("hr");
-               assertEquals(null, A.getNsPrefixURI("hr"));
-               assertEquals(bURI, A.getNsPrefixURI("br"));
-       }
-
-       @Test
-       public void testEquality() {
+       public void testSamePrefixMappingAs_PrefixMapping() {
                testEquals("");
                testEquals("", "x=a", false);
                testEquals("x=a", "", false);
@@ -434,18 +334,12 @@ public abstract class AbstractPrefixMapp
        }
 
        @Test
-       public void testAllowNastyNamespace() { // we now allow namespaces to 
end
-                                                                               
        // with non-punctuational characters
-               getMapping().setNsPrefix("abc", "def");
-       }
-
-       @Test
        public void testLock() {
                PrefixMapping A = getMapping();
                assertSame(A, A.lock());
                /* */
                try {
-                       A.setNsPrefix("crisp", crispURI);
+                       A.setNsPrefix("crisp", httpURI);
                        fail("mapping should be frozen");
                } catch (PrefixMapping.JenaLockedException e) {
                        // expected
@@ -472,4 +366,94 @@ public abstract class AbstractPrefixMapp
                        // expected
                }
        }
+       
+       /**
+        * Verify that all items in the before map still exists in the after 
map.
+        * @param before
+        * @param after
+        */
+       private void assertNoUpdateToMap( Map<String,String> before, 
Map<String,String> after)
+       {
+               if (!after.keySet().containsAll(before.keySet()))
+               {
+                       Set<String> diff = new HashSet<String>( before.keySet() 
);
+                       diff.removeAll( after.keySet() );
+                       fail( "Prefixes "+diff+" do not appear in result");
+               }
+               if (!after.values().containsAll(before.values()))
+               {
+                       Set<String> diff = new HashSet<String>( before.values() 
);
+                       diff.removeAll( after.values() );
+                       fail( "URIs "+diff+" do not appear in result");
+               }                       
+       }
+       
+       @Test
+       public void testSetNsPrefix_String_String()
+       {
+               PrefixMapping pm = getConfiguredMapping();
+               Map<String,String> before = null;
+               // test invalid NCName prefix
+               for (String pfx : illegalPrefixes)
+               {
+                       before = pm.getNsPrefixMap();
+                       try {
+                       pm.setNsPrefix( pfx, "http://example.com/bad_prefix/";);
+                       fail( pfx+" should have thrown 
PrefixMapping.IllegalPrefixException");
+                       } catch (PrefixMapping.IllegalPrefixException expected) 
{
+                               // expected
+                       }
+                       assertNoUpdateToMap( before, pm.getNsPrefixMap());
+               }       
+                       
+               before = pm.getNsPrefixMap();
+               // test set default prefix should not change existing mapping
+               assertSame( "setNsPrefix should return PrefixMap", pm, 
pm.setNsPrefix("", httpURI));    
+               assertEquals( httpURI, pm.getNsPrefixURI(""));
+               assertEquals( httpURI, pm.getNsPrefixURI(httpPrefix));
+               assertNoUpdateToMap( before, pm.getNsPrefixMap() );
+               
+               // Test that adding a new prefix mapping for U does not throw 
away a default
+               // mapping for U.
+               before = pm.getNsPrefixMap();
+               assertSame( "setNsPrefix should return PrefixMap", pm, 
pm.setNsPrefix("pfx", httpURI)); 
+               assertEquals( httpURI, pm.getNsPrefixURI(""));
+               assertEquals( httpURI, pm.getNsPrefixURI("pfx"));
+               assertNoUpdateToMap( before, pm.getNsPrefixMap() );
+               
+               // test valid NCName prefix
+               before = pm.getNsPrefixMap();
+               assertSame( "setNsPrefix should return PrefixMap", pm, 
pm.setNsPrefix( "foo", "http://example.com/foo/";));
+               assertEquals( "http://example.com/foo/";, 
pm.getNsPrefixURI("foo"));
+               assertNoUpdateToMap( before, pm.getNsPrefixMap() );
+               
+               // test duplicate prefix is accepted
+               // add a second prefix for the foo url
+               before = pm.getNsPrefixMap();
+               assertSame( "setNsPrefix should return PrefixMap", pm, 
pm.setNsPrefix( "foo2", "http://example.com/foo/";));
+               assertEquals( pm.getNsPrefixURI("foo"), 
pm.getNsPrefixURI("foo2"));
+               assertEquals( "Shoud not change original", 
"http://example.com/foo/";, pm.getNsPrefixURI("foo"));
+               assertNoUpdateToMap( before, pm.getNsPrefixMap() );
+               
+               // test new URI for old prefix overwrites old URI
+               assertSame( "setNsPrefix should return PrefixMap", pm, 
pm.setNsPrefix( "foo2", "http://example.com/bar/";));
+               assertEquals( "http://example.com/bar/";, 
pm.getNsPrefixURI("foo2") );
+               assertEquals( "Should not duplicate URI of changed", 
"http://example.com/foo/";, pm.getNsPrefixURI("foo"));
+               
+               // verify that bad URIs are accepted
+               // they may not be silently ignored but must be accepted
+               before = pm.getNsPrefixMap();
+               assertSame( "setNsPrefix should return PrefixMap", pm, 
pm.setNsPrefix( "bad", "http://example.com/foo";));
+               assertNoUpdateToMap( before, pm.getNsPrefixMap() );
+               
+               // verify null URI throws exception
+               before = pm.getNsPrefixMap();
+               try {
+                       pm.setNsPrefix("xy", null);
+                       fail("shouild trap null URI in setNsPrefix");
+               } catch (NullPointerException e) {
+                       // expected
+               }
+               assertNoUpdateToMap( before, pm.getNsPrefixMap() );
+       }
 }

Modified: 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/testing_framework/TestUtils.java
URL: 
http://svn.apache.org/viewvc/jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/testing_framework/TestUtils.java?rev=1525332&r1=1525331&r2=1525332&view=diff
==============================================================================
--- 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/testing_framework/TestUtils.java
 (original)
+++ 
jena/Experimental/new-test/src/test/java/com/hp/hpl/jena/testing_framework/TestUtils.java
 Sun Sep 22 08:25:49 2013
@@ -30,6 +30,8 @@ import java.util.List;
 import java.util.Set;
 import java.util.StringTokenizer;
 
+import org.slf4j.LoggerFactory;
+
 import static org.junit.Assert.*;
 
 import com.hp.hpl.jena.util.CollectionFactory;
@@ -236,4 +238,26 @@ public class TestUtils {
        public static InputStream getInputStream( String fn ) throws 
IOException {
                return getURL( fn ).openStream();
        }
+       
+       public static void logAssertEquals( Class<?> clazz, String msg, Object 
obj1, Object obj2)
+       {
+               if (obj1 == null && obj2 == null)
+               {
+                       return;
+               }
+               
+               if (obj1 != null)
+               {
+                       if (obj1 == obj2)
+                       {
+                                return;
+                       }
+                       if (obj1.equals(obj2))
+                       {
+                               return;
+                       }
+               }
+               LoggerFactory.getLogger(clazz).warn( String.format( "%s 
expected: %s got: %s", msg, obj1, obj2));
+               System.out.println( String.format( "[WARNING] %s expected: %s 
got: %s", msg, obj1, obj2));
+       }
 }


Reply via email to