Adding unit tests, adding easymock library, Fixed problem with URIs with localnames that start with a digit. VIVO-13
This commit is contained in:
parent
535500dd41
commit
7129912e43
3 changed files with 130 additions and 26 deletions
BIN
webapp/lib/easymock-3.2.jar
Normal file
BIN
webapp/lib/easymock-3.2.jar
Normal file
Binary file not shown.
|
@ -242,23 +242,12 @@ public class UrlBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getIndividualProfileUrl(Individual individual, VitroRequest vreq) {
|
public static String getIndividualProfileUrl(Individual individual, VitroRequest vreq) {
|
||||||
return getIndividualProfileUrl(individual, individual.getURI(),vreq);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getIndividualProfileUrl(String individualUri, VitroRequest vreq) {
|
|
||||||
Individual individual = new IndividualImpl(individualUri);
|
|
||||||
return getIndividualProfileUrl(individual, individualUri, vreq);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getIndividualProfileUrl(Individual individual, String individualUri, VitroRequest vreq) {
|
|
||||||
WebappDaoFactory wadf = vreq.getWebappDaoFactory();
|
WebappDaoFactory wadf = vreq.getWebappDaoFactory();
|
||||||
String profileUrl = null;
|
String profileUrl = null;
|
||||||
try {
|
try {
|
||||||
URI uri = new URIImpl(individualUri); // throws exception if individualUri is invalid
|
|
||||||
String namespace = uri.getNamespace();
|
|
||||||
String defaultNamespace = wadf.getDefaultNamespace();
|
|
||||||
|
|
||||||
String localName = individual.getLocalName();
|
String localName = individual.getLocalName();
|
||||||
|
String namespace = individual.getNamespace();
|
||||||
|
String defaultNamespace = wadf.getDefaultNamespace();
|
||||||
|
|
||||||
if (defaultNamespace.equals(namespace)) {
|
if (defaultNamespace.equals(namespace)) {
|
||||||
profileUrl = getUrl(Route.INDIVIDUAL.path() + "/" + localName);
|
profileUrl = getUrl(Route.INDIVIDUAL.path() + "/" + localName);
|
||||||
|
@ -267,7 +256,7 @@ public class UrlBuilder {
|
||||||
log.debug("Found externally linked namespace " + namespace);
|
log.debug("Found externally linked namespace " + namespace);
|
||||||
profileUrl = namespace + localName;
|
profileUrl = namespace + localName;
|
||||||
} else {
|
} else {
|
||||||
ParamMap params = new ParamMap("uri", individualUri);
|
ParamMap params = new ParamMap("uri", individual.getURI());
|
||||||
profileUrl = getUrl("/individual", params);
|
profileUrl = getUrl("/individual", params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,15 +275,46 @@ public class UrlBuilder {
|
||||||
return profileUrl;
|
return profileUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If you already have an Individual object around,
|
||||||
|
* call getIndividualProfileUrl(Individual, VitroRequest)
|
||||||
|
* instead of this method.
|
||||||
|
*/
|
||||||
|
public static String getIndividualProfileUrl(String individualUri, VitroRequest vreq) {
|
||||||
|
return getIndividualProfileUrl(new IndividualImpl(individualUri), vreq);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String getIndividualProfileUrl(
|
||||||
|
String individualUri,
|
||||||
|
String namespace, String localName,
|
||||||
|
String defaultNamespace){
|
||||||
|
String profileUrl = "";
|
||||||
|
try{
|
||||||
|
if ( isUriInDefaultNamespace( individualUri, defaultNamespace) ) {
|
||||||
|
profileUrl = getUrl(Route.INDIVIDUAL.path() + "/" + localName);
|
||||||
|
} else {
|
||||||
|
ParamMap params = new ParamMap("uri", individualUri);
|
||||||
|
profileUrl = getUrl("/individual", params);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return profileUrl;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isUriInDefaultNamespace(String individualUri, VitroRequest vreq) {
|
public static boolean isUriInDefaultNamespace(String individualUri, VitroRequest vreq) {
|
||||||
return isUriInDefaultNamespace(individualUri, vreq.getWebappDaoFactory());
|
return isUriInDefaultNamespace(individualUri, vreq.getWebappDaoFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isUriInDefaultNamespace(String individualUri, WebappDaoFactory wadf) {
|
public static boolean isUriInDefaultNamespace(String individualUri, WebappDaoFactory wadf) {
|
||||||
|
return isUriInDefaultNamespace( individualUri, wadf.getDefaultNamespace());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isUriInDefaultNamespace(String individualUri, String defaultNamespace){
|
||||||
try {
|
try {
|
||||||
URI uri = new URIImpl(individualUri); // throws exception if individualUri is invalid
|
Individual ind = new IndividualImpl(individualUri);
|
||||||
String namespace = uri.getNamespace();
|
String namespace = ind.getNamespace();
|
||||||
String defaultNamespace = wadf.getDefaultNamespace();
|
|
||||||
return defaultNamespace.equals(namespace);
|
return defaultNamespace.equals(namespace);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn(e);
|
log.warn(e);
|
||||||
|
|
|
@ -3,12 +3,21 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import stubs.edu.cornell.mannlib.vitro.webapp.dao.ApplicationDaoStub;
|
||||||
|
import stubs.edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryStub;
|
||||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
|
||||||
|
|
||||||
public class UrlBuilderTest extends AbstractTestClass {
|
public class UrlBuilderTest extends AbstractTestClass {
|
||||||
|
|
||||||
|
@ -78,4 +87,79 @@ public class UrlBuilderTest extends AbstractTestClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetIndividualProfileURI(){
|
||||||
|
VitroRequest vreq = makeMockVitroRequest( "http://example.com/individual/");
|
||||||
|
UrlBuilder.contextPath = "http://example.com";
|
||||||
|
|
||||||
|
String uri = "http://example.com/individual/n2343";
|
||||||
|
String url = UrlBuilder.getIndividualProfileUrl(uri, vreq);
|
||||||
|
Assert.assertEquals("http://example.com/individual/n2343", url);
|
||||||
|
|
||||||
|
uri = "http://example.com/individual/bob";
|
||||||
|
url = UrlBuilder.getIndividualProfileUrl(uri, vreq);
|
||||||
|
Assert.assertEquals("http://example.com/individual/bob",url);
|
||||||
|
|
||||||
|
uri = "http://nondefaultNS.com/individual/n2343";
|
||||||
|
url = UrlBuilder.getIndividualProfileUrl(uri, vreq);
|
||||||
|
Assert.assertEquals("http://example.com/individual?uri=" + URLEncoder.encode(uri), url);
|
||||||
|
|
||||||
|
uri = "http://example.com/individual#n2343";
|
||||||
|
url = UrlBuilder.getIndividualProfileUrl(uri, vreq);
|
||||||
|
Assert.assertEquals("http://example.com/individual?uri=" + URLEncoder.encode(uri), url);
|
||||||
|
|
||||||
|
uri = "http://example.com/individual/5LNCannotStartWithNumber";
|
||||||
|
url = UrlBuilder.getIndividualProfileUrl(uri, vreq);
|
||||||
|
Assert.assertEquals("http://example.com/individual?uri=" + URLEncoder.encode(uri), url);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected VitroRequest makeMockVitroRequest( final String defaultNS){
|
||||||
|
HttpServletRequest req = createMock( HttpServletRequest.class );
|
||||||
|
return new VitroRequest(req){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getParameter(String key){ return null; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WebappDaoFactory getWebappDaoFactory(){
|
||||||
|
return makeMockWDF(defaultNS);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
protected WebappDaoFactoryStub makeMockWDF( String defaultNS){
|
||||||
|
WebappDaoFactoryStub wdf = new WebappDaoFactoryStub();
|
||||||
|
wdf.setDefaultNamespace("http://example.com/individual/");
|
||||||
|
ApplicationDaoStub aDao = new ApplicationDaoStub(){
|
||||||
|
@Override
|
||||||
|
public boolean isExternallyLinkedNamespace(String ns){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
wdf.setApplicationDao( aDao );
|
||||||
|
return wdf;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsUriInDefaultNamespace(){
|
||||||
|
String[][] examples = {
|
||||||
|
{ "http://example.com/individual/n3234", "http://example.com/individual/"},
|
||||||
|
{ "http://example.com/individual#n3234", "http://example.com/individual#"},
|
||||||
|
{ "http://example.com:8080/individual/n3234", "http://example.com:8080/individual/"},
|
||||||
|
{ "http://example.com:8080/individual#n3234", "http://example.com:8080/individual#"}
|
||||||
|
};
|
||||||
|
|
||||||
|
for( String[] example : examples ){
|
||||||
|
Assert.assertTrue("expected '"+ example[0] + "' to be in the default NS of '"+example[1]+"'",
|
||||||
|
UrlBuilder.isUriInDefaultNamespace(example[0], example[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
String[][] counterExamples = {
|
||||||
|
{ "http://example.com/individual/5LNCannotStartWithNumber", "http://example.com/individual/" }
|
||||||
|
};
|
||||||
|
for( String[] example : counterExamples ){
|
||||||
|
Assert.assertFalse("expected '"+ example[0] + "' to NOT be in the default NS of '"+example[1]+"'",
|
||||||
|
UrlBuilder.isUriInDefaultNamespace(example[0], example[1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue