Repository: zest-java Updated Branches: refs/heads/develop d900c623f -> 49347430b
Upgrade libraries Had to rewrite some of rest and http libraries tests after upgrading Apache HttpClient to 4.5.x. With no change to production code we are stuck on old versions for some libraries, added comments in libraries.gradle for each one. Removed unused libraries. Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/fee6b682 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/fee6b682 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/fee6b682 Branch: refs/heads/develop Commit: fee6b68259c14633d5af9a6de2d989388edfd9e9 Parents: d900c62 Author: Paul Merlin <[email protected]> Authored: Mon Jun 13 15:00:58 2016 +0200 Committer: Paul Merlin <[email protected]> Committed: Mon Jun 13 15:00:58 2016 +0200 ---------------------------------------------------------------------- build.gradle | 3 +- .../apache/zest/bootstrap/ClassScannerTest.java | 2 +- libraries.gradle | 86 ++++++------- .../zest/library/http/AbstractJettyTest.java | 28 +++-- .../library/http/AbstractSecureJettyTest.java | 115 +++++++++++------- .../library/http/SecureJettyServiceTest.java | 32 ++--- .../zest/library/rest/admin/RestTest.java | 121 +++++++------------ 7 files changed, 183 insertions(+), 204 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/build.gradle ---------------------------------------------------------------------- diff --git a/build.gradle b/build.gradle index 8ebebe3..39e4803 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,7 @@ buildscript { } dependencies { classpath 'gradle.plugin.org.nosphere.apache:creadur-rat-gradle:0.1.2' - classpath 'gradle.plugin.org.nosphere.honker:honker-gradle:0.2.1' + classpath 'gradle.plugin.org.nosphere.honker:honker-gradle:0.2.2' classpath 'org.hibernate.build.gradle:gradle-maven-publish-auth:2.0.1' } } @@ -266,6 +266,7 @@ allprojects { } if( candidate.group == 'org.apache.httpcomponents' || candidate.group == 'net.java.dev.jna' + || candidate.group == 'lucene' || candidate.group == 'jdbm' || candidate.group == 'org.osgi' || candidate.group.startsWith( 'org.restlet' ) ) { http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/core/bootstrap/src/test/java/org/apache/zest/bootstrap/ClassScannerTest.java ---------------------------------------------------------------------- diff --git a/core/bootstrap/src/test/java/org/apache/zest/bootstrap/ClassScannerTest.java b/core/bootstrap/src/test/java/org/apache/zest/bootstrap/ClassScannerTest.java index 02ff347..131dead 100644 --- a/core/bootstrap/src/test/java/org/apache/zest/bootstrap/ClassScannerTest.java +++ b/core/bootstrap/src/test/java/org/apache/zest/bootstrap/ClassScannerTest.java @@ -62,6 +62,6 @@ public class ClassScannerTest @Test public void testClassScannerJar() { - Assert.assertEquals( 138, Iterables.count( findClasses( Test.class ) ) ); + Assert.assertEquals( 185, Iterables.count( findClasses( Test.class ) ) ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/libraries.gradle ---------------------------------------------------------------------- diff --git a/libraries.gradle b/libraries.gradle index cd89bb2..31930f5 100644 --- a/libraries.gradle +++ b/libraries.gradle @@ -19,69 +19,64 @@ */ // Versions of Dependencies; -def asmVersion = '5.0.4' +def asmVersion = '5.1' def bonecpVersion = '0.8.0.RELEASE' -def bouncyVersion = '1.52' +def bouncyVersion = '1.54' def commonsDbcpVersion = '1.4' // Should be moved to commons-dbcp2 def commonsLangVersion = '2.6' // Should be moved to commons-lang3 -def derbyVersion = '10.11.1.1' +def derbyVersion = '10.12.1.1' def dnsJavaVersion = '2.1.7' -def ehcacheVersion = '2.10.0' -def elasticsearchVersion = '1.6.0' -def freemarkerVersion = '2.3.22' +def ehcacheVersion = '2.10.2.2.21' // 3.x exists +def elasticsearchVersion = '1.7.5' // 2.x exists +def freemarkerVersion = '2.3.24-incubating' def geodeVersion = '1.0.0-incubating.M2' -def groovyVersion = '2.4.3' -def h2Version = '1.4.187' -def hazelcastVersion = '3.5' -def httpClientVersion = '4.2.2' // 4.3.4 exists -def jacksonVersion = '2.5.4' -def javascriptVersion = '1.7.7' +def groovyVersion = '2.4.7' +def h2Version = '1.4.192' +def hazelcastVersion = '3.6.3' +def httpClientVersion = '4.5.2' +def jacksonVersion = '2.7.5' +def javascriptVersion = '1.7.7.1' def javasqlgeneratorVersion = '0.3.2' -def jcloudsVersion = '1.9.0' +def jcloudsVersion = '1.9.2' def jdbmVersion = '2.4' -def jedisVersion = '2.7.2' -def jettyVersion = '9.2.11.v20150529' // 9.3.x Tests fail! -def jgoodiesLooksVersion = '2.5.3' -def jodaMoneyVersion = '0.10.0' +def jedisVersion = '2.8.1' +def jettyVersion = '9.2.17.v20160517' // 9.3.x Tests fail! +def jgoodiesLooksVersion = '2.7.0' def jtaVersion = '1.1' def leveldbVersion = '0.7' def leveldbJniVersion = '1.8' -def liquibaseVersion = '3.4.0' +def liquibaseVersion = '3.5.1' def logbackVersion = '1.1.3' -def mongodbVersion = '2.13.2' // 3.0.x Fails to compile! -def mysqlVersion = '5.1.35' +def mongodbVersion = '2.14.2' // 3.0.x Fails to compile! +def mysqlVersion = '5.1.39' // 6.x exists def orgJsonVersion = '20130213' def osgiVersion = '4.2.0' // 4.3.0 Fails to compile! - 5.0.0 exists -def pdfboxVersion = '1.8.5' -def postgresqlVersion = '9.4-1201-jdbc41' -def prefuseVersion = '0.21' -def restletVersion = '2.3.4' -def rdfVersion = '2.7.9' +def pdfboxVersion = '1.8.12' // 2.x exists +def postgresqlVersion = '9.4.1208' +def prefuseVersion = '1.0.1' +def restletVersion = '2.3.7' +def rdfVersion = '2.7.16' // 2.8.x change query results!! 4.x exists def riakVersion = '1.4.4' // 2.0.x Fails to compile! -def scalaVersion = '2.11.6' +def scalaVersion = '2.11.8' def servletVersion = '3.1.0' -def shiroVersion = '1.2.3' +def shiroVersion = '1.2.4' // 1.2.5 exists but has wrong POM def skedVersion = '2.1' -def slf4jVersion = '1.7.12' -def solrVersion = "1.4.1" // 4.8.1 Fails to compile! -def springVersion = '4.1.7.RELEASE' -def spymemcachedVersion = '2.12.0' -def sqliteVersion = '3.8.10.1' +def slf4jVersion = '1.7.21' +def solrVersion = "1.4.1" // 4.x Fails to compile! +def springVersion = '4.3.0.RELEASE' +def spymemcachedVersion = '2.12.1' +def sqliteVersion = '3.8.11.2' def velocityVersion = '1.7' -def wicketVersion = '1.5.8' // 6.15.0 Samples fails to compile! +def wicketVersion = '1.5.8' // 6.x Samples fails to compile! def wicketStatelessVersion = '1.5.0' def woodstoxVersion = '4.4.1' -def yammerMetricsVersion = '2.2.0' +def yammerMetricsVersion = '2.2.0' // 3.x exists, relocated to io.dropwizard -def antVersion = '1.9.5' -def awaitilityVersion = '1.6.3' -def easyMockVersion = '3.3.1' -def jmockVersion = '2.8.1' -def junitVersion = '4.11' +def antVersion = '1.9.7' +def awaitilityVersion = '1.7.0' +def easyMockVersion = '3.4' +def junitVersion = '4.12' def mockitoVersion = '1.10.19' -def testNgVersion = '6.9.4' -def restAssuredVersion= '2.4.1' -def xmlunitVersion = '1.6' // build a map of the dependency artifacts to use. Allows centralized definition of the version of artifacts to // use. In that respect it serves a role similar to <dependencyManagement> in Maven @@ -191,7 +186,6 @@ rootProject.ext { // Library & Extension dependencies jackson_mapper: "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion", - jodamoney: "org.joda:joda-money:$jodaMoneyVersion", ehcache: "net.sf.ehcache:ehcache:$ehcacheVersion", elasticsearch: "org.elasticsearch:elasticsearch:$elasticsearchVersion", geode: "org.apache.geode:geode-core:$geodeVersion", @@ -235,12 +229,8 @@ rootProject.ext { junit: "junit:junit:$junitVersion", awaitility: "com.jayway.awaitility:awaitility:$awaitilityVersion", easymock: "org.easymock:easymock:$easyMockVersion", - jmock: "org.jmock:jmock:$jmockVersion", mockito: "org.mockito:mockito-core:$mockitoVersion", - rest_assured: "com.jayway.restassured:rest-assured:$restAssuredVersion", - testng: "org.testng:testng:$testNgVersion", - xmlunit: "xmlunit:xmlunit:$xmlunitVersion", - + // Tests dependencies derby: "org.apache.derby:derby:$derbyVersion", derbyclient: "org.apache.derby:derbyclient:$derbyVersion", http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java index 8c24cd0..3cec81b 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java @@ -24,28 +24,27 @@ import java.security.GeneralSecurityException; import java.security.Security; import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; -import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; +import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.apache.zest.test.AbstractZestTest; public abstract class AbstractJettyTest - extends AbstractZestTest + extends AbstractZestTest { - protected static final int HTTP_PORT = 8041; - protected HttpClient defaultHttpClient; + protected CloseableHttpClient defaultHttpClient; protected ResponseHandler<String> stringResponseHandler = new ResponseHandler<String>() { - + @Override public String handleResponse( HttpResponse hr ) - throws ClientProtocolException, IOException + throws IOException { return EntityUtils.toString( hr.getEntity(), "UTF-8" ); } @@ -61,10 +60,19 @@ public abstract class AbstractJettyTest @Before public void before() - throws GeneralSecurityException, IOException + throws GeneralSecurityException, IOException { // Default HTTP Client - defaultHttpClient = new DefaultHttpClient(); + defaultHttpClient = HttpClients.createDefault(); } + @After + public void after() + throws IOException + { + if( defaultHttpClient != null ) + { + defaultHttpClient.close(); + } + } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java index 1cf0abf..75d87c9 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java @@ -26,15 +26,14 @@ import java.security.GeneralSecurityException; import java.security.KeyStore; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManagerFactory; -import org.apache.http.client.HttpClient; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.AllowAllHostnameVerifier; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -45,10 +44,8 @@ import org.junit.BeforeClass; * Use HttpClient in order to easily use different {@link SSLContext}s between server and client. */ public abstract class AbstractSecureJettyTest - extends AbstractJettyTest + extends AbstractJettyTest { - - private static final String HTTPS = "https"; protected static final int HTTPS_PORT = 8441; protected static final String KS_PASSWORD = "changeit"; protected static final String CLIENT_KEYSTORE_PATH = "src/test/resources/org/apache/zest/library/http/zest-lib-http-unittests-client-cert.jceks"; @@ -57,39 +54,39 @@ public abstract class AbstractSecureJettyTest protected static final File SERVER_KEYSTORE_FILE = new File( SERVER_KEYSTORE_PATH ); protected static final String TRUSTSTORE_PATH = "src/test/resources/org/apache/zest/library/http/zest-lib-http-unittests-ca.jceks"; protected static final File TRUSTSTORE_FILE = new File( TRUSTSTORE_PATH ); + // These two clients use a HostnameVerifier that don't do any check, don't do this in production code - protected HttpClient trustHttpClient; - protected HttpClient mutualHttpClient; + protected CloseableHttpClient trustHttpClient; + protected CloseableHttpClient mutualHttpClient; @Before public void beforeSecure() - throws GeneralSecurityException, IOException + throws GeneralSecurityException, IOException { // Trust HTTP Client - KeyStore truststore = KeyStore.getInstance( "JCEKS" ); - truststore.load( new FileInputStream( TRUSTSTORE_FILE ), KS_PASSWORD.toCharArray() ); - - AllowAllHostnameVerifier verifier = new AllowAllHostnameVerifier(); - - DefaultHttpClient trustClient = new DefaultHttpClient(); - SSLSocketFactory trustSslFactory = new SSLSocketFactory( truststore ); - trustSslFactory.setHostnameVerifier( verifier ); - SchemeRegistry trustSchemeRegistry = trustClient.getConnectionManager().getSchemeRegistry(); - trustSchemeRegistry.unregister( HTTPS ); - trustSchemeRegistry.register( new Scheme( HTTPS, HTTPS_PORT, trustSslFactory ) ); - trustHttpClient = trustClient; - + trustHttpClient = HttpClients.custom() + .setSSLHostnameVerifier( NoopHostnameVerifier.INSTANCE ) + .setSSLContext( buildTrustSSLContext() ) + .build(); // Mutual HTTP Client - KeyStore keystore = KeyStore.getInstance( "JCEKS" ); - keystore.load( new FileInputStream( CLIENT_KEYSTORE_FILE ), KS_PASSWORD.toCharArray() ); + mutualHttpClient = HttpClients.custom() + .setSSLHostnameVerifier( NoopHostnameVerifier.INSTANCE ) + .setSSLContext( buildMutualSSLContext() ) + .build(); + } - DefaultHttpClient mutualClient = new DefaultHttpClient(); - SSLSocketFactory mutualSslFactory = new SSLSocketFactory( keystore, KS_PASSWORD, truststore ); - mutualSslFactory.setHostnameVerifier( verifier ); - SchemeRegistry mutualSchemeRegistry = mutualClient.getConnectionManager().getSchemeRegistry(); - mutualSchemeRegistry.unregister( HTTPS ); - mutualSchemeRegistry.register( new Scheme( HTTPS, HTTPS_PORT, mutualSslFactory ) ); - mutualHttpClient = mutualClient; + @After + public void afterSecure() + throws IOException + { + if( trustHttpClient != null ) + { + trustHttpClient.close(); + } + if( mutualHttpClient != null ) + { + mutualHttpClient.close(); + } } private static HostnameVerifier defaultHostnameVerifier; @@ -97,26 +94,19 @@ public abstract class AbstractSecureJettyTest @BeforeClass public static void beforeSecureClass() - throws IOException, GeneralSecurityException + throws IOException, GeneralSecurityException { defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier(); defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory(); HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier() { - + @Override public boolean verify( String string, SSLSession ssls ) { return true; } - } ); - KeyStore truststore = KeyStore.getInstance( "JCEKS" ); - truststore.load( new FileInputStream( TRUSTSTORE_FILE ), KS_PASSWORD.toCharArray() ); - SSLContext sslCtx = SSLContext.getInstance( "TLS" ); - TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance( getX509Algorithm() ); - caTrustManagerFactory.init( truststore ); - sslCtx.init( null, caTrustManagerFactory.getTrustManagers(), null ); - HttpsURLConnection.setDefaultSSLSocketFactory( sslCtx.getSocketFactory() ); + HttpsURLConnection.setDefaultSSLSocketFactory( buildTrustSSLContext().getSocketFactory() ); } @AfterClass @@ -131,4 +121,41 @@ public abstract class AbstractSecureJettyTest return System.getProperty( "java.vendor" ).contains( "IBM" ) ? "IbmX509" : "SunX509"; } + private static SSLContext buildTrustSSLContext() + throws IOException, GeneralSecurityException + { + SSLContext sslCtx = SSLContext.getInstance( "TLS" ); + TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance( getX509Algorithm() ); + caTrustManagerFactory.init( loadTrustStore() ); + sslCtx.init( null, caTrustManagerFactory.getTrustManagers(), null ); + return sslCtx; + } + + private static SSLContext buildMutualSSLContext() + throws IOException, GeneralSecurityException + { + SSLContext sslCtx = SSLContext.getInstance( "TLS" ); + KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance( getX509Algorithm() ); + keyManagerFactory.init( loadKeyStore(), KS_PASSWORD.toCharArray() ); + TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance( getX509Algorithm() ); + caTrustManagerFactory.init( loadTrustStore() ); + sslCtx.init( keyManagerFactory.getKeyManagers(), caTrustManagerFactory.getTrustManagers(), null ); + return sslCtx; + } + + private static KeyStore loadTrustStore() + throws IOException, GeneralSecurityException + { + KeyStore truststore = KeyStore.getInstance( "JCEKS" ); + truststore.load( new FileInputStream( TRUSTSTORE_FILE ), KS_PASSWORD.toCharArray() ); + return truststore; + } + + private static KeyStore loadKeyStore() + throws IOException, GeneralSecurityException + { + KeyStore keystore = KeyStore.getInstance( "JCEKS" ); + keystore.load( new FileInputStream( CLIENT_KEYSTORE_FILE ), KS_PASSWORD.toCharArray() ); + return keystore; + } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java index df4e0c5..68352b8 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java @@ -20,7 +20,7 @@ package org.apache.zest.library.http; import java.io.IOException; -import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLHandshakeException; import org.apache.http.NoHttpResponseException; import org.apache.http.client.methods.HttpGet; import org.junit.Test; @@ -65,37 +65,23 @@ public class SecureJettyServiceTest // END SNIPPET: assemblyssl } - @Test + @Test( expected = NoHttpResponseException.class ) // This test exists for demonstration purpose only, it do not test usefull things but it's on purpose public void testNoSSL() throws IOException { - try - { - HttpGet get = new HttpGet( "http://127.0.0.1:8441/hello" ); - defaultHttpClient.execute( get ); - fail( "We could reach the HTTPS connector using a HTTP url, that's no good" ); - } - catch( NoHttpResponseException ex ) - { - // Expected - } + HttpGet get = new HttpGet( "http://127.0.0.1:8441/hello" ); + defaultHttpClient.execute( get ); + fail( "We could reach the HTTPS connector using a HTTP url, that's no good" ); } - @Test - // This test exists for demonstration purpose only, it do not test usefull things but it's on purpose + @Test( expected = SSLHandshakeException.class ) + // This test exists for demonstration purpose only, it do not test useful things but it's on purpose public void testNoTruststore() throws IOException { - try - { - defaultHttpClient.execute( new HttpGet( "https://127.0.0.1:8441/hello" ) ); - fail( "We could reach the HTTPS connector without proper truststore, this should not happen" ); - } - catch( SSLPeerUnverifiedException ex ) - { - // Expected - } + defaultHttpClient.execute( new HttpGet( "https://127.0.0.1:8441/hello" ) ); + fail( "We could reach the HTTPS connector without proper truststore, this should not happen" ); } @Test http://git-wip-us.apache.org/repos/asf/zest-java/blob/fee6b682/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java ---------------------------------------------------------------------- diff --git a/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java b/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java index 0d4013d..184de4a 100644 --- a/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java +++ b/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java @@ -20,16 +20,22 @@ package org.apache.zest.library.rest.admin; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.DeleteMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; import org.apache.zest.api.association.Association; import org.apache.zest.api.common.Optional; import org.apache.zest.api.entity.EntityBuilder; @@ -130,12 +136,12 @@ public class RestTest extends AbstractZestTest // System.out.println( rdf.replaceAll( "\n", "\\\\n" ).replaceAll( "\"", "\\\\\"" ) ); assertThat( "Incorrect RDF produced", rdf, anyOf( // Open JDK 8 & Valid - equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n xmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n <lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n <firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n <identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n <mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ), - equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n xmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n <identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n <firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n <lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n <mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ), + equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n\txmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n\t<lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n\t<firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n\t<identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n\t<mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ), + equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n\txmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n\t<identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n\t<firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n\t<lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n\t<mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ), // Sun JDK 6 / Oracle JDK 7 & Valid - equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n xmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n <firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n <lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n <identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n <mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ), + equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n\txmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n\t<firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n\t<lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n\t<identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n\t<mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ), // IBM JDK 6 & Valid - equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n xmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n <identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n <lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n <firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n <mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ) ) ); + equalTo( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rdf:RDF\n\txmlns:zest=\"http://zest.apache.org/rdf/model/1.0/\"\n\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n\txmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\">\n<org.apache.zest.library.rest.admin.RestTest-PersonEntity xmlns=\"urn:zest:type:\" rdf:about=\"urn:zest:entity:P1\">\n\t<identity xmlns=\"urn:zest:type:org.apache.zest.api.entity.Identity#\">P1</identity>\n\t<lastname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Doe</lastname>\n\t<firstname xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\">Joe</firstname>\n\t<mother xmlns=\"urn:zest:type:org.apache.zest.library.rest.admin.RestTest-Person#\" rdf:resource=\"urn:zest:entity:P2\"/>\n</org.apache.zest.library.rest.admin.RestTest-PersonEntity>\n\n</rdf:RDF>" ) ) ); } @Test @@ -213,105 +219,66 @@ public class RestTest extends AbstractZestTest public String getEntity( String identity ) throws IOException { - HttpClient client = new HttpClient(); - GetMethod method = new GetMethod( "http://localhost:8182/entity/" + identity + ".rdf" ); - method.addRequestHeader( "Accept", "application/rdf+xml" ); - try + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet method = new HttpGet( "http://localhost:8182/entity/" + identity + ".rdf" ); + method.addHeader( "Accept", "application/rdf+xml" ); + try( CloseableHttpResponse response = client.execute( method ) ) { - int status = client.executeMethod( method ); - if( status != 200 ) + if( response.getStatusLine().getStatusCode() != 200 ) { - throw new RuntimeException( "EntityResource returned status code: '" + status + "' and message: '" + method - .getStatusText() + "'" ); + throw new RuntimeException( "EntityResource returned status: " + response.getStatusLine() ); } - InputStream input = method.getResponseBodyAsStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - copyStream( input, baos ); - return baos.toString( "UTF-8" ); - } - finally - { - method.releaseConnection(); + return EntityUtils.toString( response.getEntity(), StandardCharsets.UTF_8 ); } } public void putEntity( String identity, Map<String, String> params ) throws IOException { - HttpClient client = new HttpClient(); - PostMethod method = new PostMethod( "http://localhost:8182/entity/" + identity ); + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost method = new HttpPost( "http://localhost:8182/entity/" + identity ); + List<NameValuePair> parameters = new ArrayList<>(); for( Map.Entry<String, String> entry : params.entrySet() ) { - method.addParameter( entry.getKey(), entry.getValue() ); + parameters.add( new BasicNameValuePair( entry.getKey(), entry.getValue() ) ); } - try + method.setEntity( new UrlEncodedFormEntity( parameters ) ); + try( CloseableHttpResponse response = client.execute( method ) ) { - int status = client.executeMethod( method ); - if( status != 205 ) + if( response.getStatusLine().getStatusCode() != 205 ) { - throw new RuntimeException( "EntityResource returned status code: '" + status + "' and message: '" + method - .getStatusText() + "'" ); + throw new RuntimeException( "EntityResource returned status: " + response.getStatusLine() ); } } - finally - { - method.releaseConnection(); - } } public void deleteEntity( String identity ) throws IOException { - HttpClient client = new HttpClient(); - DeleteMethod method = new DeleteMethod( "http://localhost:8182/entity/" + identity ); - try + CloseableHttpClient client = HttpClients.createDefault(); + HttpDelete method = new HttpDelete( "http://localhost:8182/entity/" + identity ); + try( CloseableHttpResponse response = client.execute( method ) ) { - int status = client.executeMethod( method ); - if( status != 204 ) + if( response.getStatusLine().getStatusCode() != 204 ) { - throw new RuntimeException( "EntityResource returned status code: '" + status + "' and message: '" + method - .getStatusText() + "'" ); + throw new RuntimeException( "EntityResource returned status: " + response.getStatusLine() ); } } - finally - { - method.releaseConnection(); - } } public String getEntities() throws IOException { - HttpClient client = new HttpClient(); - GetMethod method = new GetMethod( "http://localhost:8182/entity.rdf" ); - method.addRequestHeader( "Accept", "application/rdf+xml" ); - try + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet method = new HttpGet( "http://localhost:8182/entity.rdf" ); + method.addHeader( "Accept", "application/rdf+xml" ); + try( CloseableHttpResponse response = client.execute( method ) ) { - int status = client.executeMethod( method ); - if( status != 200 ) + if( response.getStatusLine().getStatusCode() != 200 ) { - throw new RuntimeException( "EntityResource returned status code: '" + status + "' and message: '" + method - .getStatusText() + "'" ); + throw new RuntimeException( "EntityResource returned status: " + response.getStatusLine() ); } - InputStream input = method.getResponseBodyAsStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - copyStream( input, baos ); - return baos.toString( "UTF-8" ); - } - finally - { - method.releaseConnection(); - } - } - - private void copyStream( InputStream in, OutputStream baos ) - throws IOException - { - int data = in.read(); - while( data != -1 ) - { - baos.write( data ); - data = in.read(); + return EntityUtils.toString( response.getEntity(), StandardCharsets.UTF_8 ); } } }
