📄 abstracttestprefixmapping.java
字号:
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
[See end of file]
$Id: AbstractTestPrefixMapping.java,v 1.27 2007/01/02 11:53:14 andy_seaborne Exp $
*/
package com.hp.hpl.jena.shared.test;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.shared.PrefixMapping.Factory;
import com.hp.hpl.jena.graph.test.*;
import java.util.*;
/**
Test prefix mappings - subclass this test and override getMapping() to
deliver the prefixMapping to be tested.
@author kers
*/
public abstract class AbstractTestPrefixMapping extends GraphTestBase
{
public AbstractTestPrefixMapping( String name )
{ super( name ); }
/**
Subclasses implement to return a new, empty prefixMapping of their
preferred kind.
*/
abstract protected PrefixMapping getMapping();
static final String crispURI = "http://crisp.nosuch.net/";
static final String ropeURI = "scheme:rope/string#";
static final String butterURI = "ftp://ftp.nowhere.at.all/cream#";
/**
The empty prefix is specifically allowed [for the default namespace].
*/
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.
*/
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) { pass(); }
}
}
public void testNullURITrapped()
{
try
{
getMapping().setNsPrefix( "xy", null );
fail( "shouild trap null URI in setNsPrefix" );
}
catch (NullPointerException e)
{ pass(); }
}
/**
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.
*/
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" ) );
}
/**
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.
*/
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 ) );
}
/**
test that we can extract a proper Map from a PrefixMapping
*/
public void testPrefixMappingMap()
{
PrefixMapping ns = getCrispyRope();
Map map = ns.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
*/
public void testPrefixMappingSecret()
{
PrefixMapping ns = getCrispyRope();
Map 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" ) );
}
private PrefixMapping getCrispyRope()
{
PrefixMapping ns = getMapping();
ns.setNsPrefix( "crisp", crispURI);
ns.setNsPrefix( "rope", ropeURI );
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" },
};
public void testExpandPrefix()
{
PrefixMapping ns = getMapping();
ns.setNsPrefix( "crisp", crispURI );
ns.setNsPrefix( "rope", ropeURI );
/* */
for (int i = 0; i < dontChange.length; i += 1)
assertEquals
(
"should be unchanged",
dontChange[i],
ns.expandPrefix( dontChange[i] )
);
/* */
for (int i = 0; i < expansions.length; i += 1)
assertEquals
(
"should expand correctly",
expansions[i][1],
ns.expandPrefix( expansions[i][0] )
);
}
public void testUseEasyPrefix()
{
testUseEasyPrefix( "prefix mapping impl", getMapping() );
testShortForm( "prefix mapping impl", getMapping() );
}
public static void testUseEasyPrefix( String title, PrefixMapping ns )
{
testUsePrefix( title, ns );
testShortForm( title, ns );
}
public static void testUsePrefix( String title, PrefixMapping ns )
{
ns.setNsPrefix( "crisp", crispURI );
ns.setNsPrefix( "butter", butterURI );
assertEquals( title, "", ns.usePrefix( "" ) );
assertEquals( title, ropeURI, ns.usePrefix( ropeURI ) );
assertEquals( title, "crisp:tail", ns.usePrefix( crispURI + "tail" ) );
assertEquals( title, "butter:here:we:are", ns.usePrefix( butterURI + "here:we:are" ) );
}
public static void testShortForm( String title, PrefixMapping ns )
{
ns.setNsPrefix( "crisp", crispURI );
ns.setNsPrefix( "butter", butterURI );
assertEquals( title, "", ns.shortForm( "" ) );
assertEquals( title, ropeURI, ns.shortForm( ropeURI ) );
assertEquals( title, "crisp:tail", ns.shortForm( crispURI + "tail" ) );
assertEquals( title, "butter:here:we:are", ns.shortForm( butterURI + "here:we:are" ) );
}
public void testEasyQName()
{
PrefixMapping ns = getMapping();
String alphaURI = "http://seasonal.song/preamble/";
ns.setNsPrefix( "alpha", alphaURI );
assertEquals( "alpha:rowboat", ns.qnameFor( alphaURI + "rowboat" ) );
}
public void testNoQNameNoPrefix()
{
PrefixMapping ns = getMapping();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -