Skip to content

Commit 77fd4cb

Browse files
author
Mark
committed
fixed tests
1 parent c271a29 commit 77fd4cb

File tree

3 files changed

+64
-58
lines changed

3 files changed

+64
-58
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
<dependency>
250250
<groupId>org.json</groupId>
251251
<artifactId>json</artifactId>
252-
<version>20151123</version>
252+
<version>20140107</version>
253253
<scope>test</scope>
254254
</dependency>
255255

src/test/java/com/arangodb/ArangoConfigureTest.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import static org.junit.Assert.assertThat;
2424
import static org.junit.Assert.fail;
2525

26+
import java.io.File;
2627
import java.io.IOException;
2728
import java.net.SocketTimeoutException;
2829
import java.net.URISyntaxException;
2930
import java.net.URL;
30-
import java.nio.file.Paths;
3131
import java.security.KeyManagementException;
3232
import java.security.KeyStoreException;
3333
import java.security.NoSuchAlgorithmException;
@@ -56,12 +56,10 @@ public class ArangoConfigureTest {
5656
/**
5757
* a SSL trust store
5858
*
59-
* create the trust store for the self signed certificate: keytool -import
60-
* -alias "my arangodb server cert" -file UnitTests/server.pem -keystore
61-
* example.truststore
59+
* create the trust store for the self signed certificate: keytool -import -alias "my arangodb server cert" -file
60+
* UnitTests/server.pem -keystore example.truststore
6261
*
63-
* https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/
64-
* apache/http/conn/ssl/SSLSocketFactory.html
62+
* https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/ apache/http/conn/ssl/SSLSocketFactory.html
6563
*/
6664
private static final String SSL_TRUSTSTORE = "/example.truststore";
6765

@@ -70,7 +68,7 @@ public void load_from_property_file() {
7068
// validate file in classpath.
7169
assertThat(getClass().getResource("/arangodb.properties"), is(notNullValue()));
7270

73-
ArangoConfigure configure = new ArangoConfigure();
71+
final ArangoConfigure configure = new ArangoConfigure();
7472
assertThat(configure.getArangoHost().getPort(), is(8529));
7573
assertThat(configure.getArangoHost().getHost(), is(notNullValue()));
7674
assertThat(configure.getDefaultDatabase(), is(nullValue()));
@@ -80,13 +78,13 @@ public void load_from_property_file() {
8078
@Test
8179
public void load_from_proerty_file2() {
8280

83-
ArangoConfigure configure = new ArangoConfigure();
81+
final ArangoConfigure configure = new ArangoConfigure();
8482
configure.loadProperties("/arangodb-test.properties");
8583

8684
assertThat(configure.getRetryCount(), is(10));
8785
assertThat(configure.getDefaultDatabase(), is("mydb2"));
8886

89-
ArangoHost arangoHost = configure.getArangoHost();
87+
final ArangoHost arangoHost = configure.getArangoHost();
9088
assertThat(arangoHost.getPort(), is(9999));
9189
assertThat(arangoHost.getHost(), is(notNullValue()));
9290

@@ -96,17 +94,17 @@ public void load_from_proerty_file2() {
9694
@Test
9795
public void connect_timeout() throws ArangoException {
9896

99-
ArangoConfigure configure = new ArangoConfigure();
97+
final ArangoConfigure configure = new ArangoConfigure();
10098
configure.getArangoHost().setHost("1.0.0.200");
10199
configure.setConnectionTimeout(1); // 1ms
102100
configure.init();
103101

104-
ArangoDriver driver = new ArangoDriver(configure);
102+
final ArangoDriver driver = new ArangoDriver(configure);
105103

106104
try {
107105
driver.getCollections();
108106
fail("did no timeout");
109-
} catch (ArangoException e) {
107+
} catch (final ArangoException e) {
110108
assertThat(e.getCause(), instanceOf(ConnectTimeoutException.class));
111109
}
112110

@@ -118,17 +116,17 @@ public void connect_timeout() throws ArangoException {
118116
@Ignore(value = "this fails some times")
119117
public void so_connect_timeout() throws ArangoException {
120118

121-
ArangoConfigure configure = new ArangoConfigure();
119+
final ArangoConfigure configure = new ArangoConfigure();
122120
configure.setConnectionTimeout(5000);
123121
configure.setTimeout(1); // 1ms
124122
configure.init();
125123

126-
ArangoDriver driver = new ArangoDriver(configure);
124+
final ArangoDriver driver = new ArangoDriver(configure);
127125

128126
try {
129127
driver.getCollections();
130128
fail("did no timeout");
131-
} catch (ArangoException e) {
129+
} catch (final ArangoException e) {
132130
assertThat(e.getCause(), instanceOf(SocketTimeoutException.class));
133131
}
134132

@@ -139,18 +137,18 @@ public void so_connect_timeout() throws ArangoException {
139137
@Test
140138
public void reconnectFallbackArangoHost() throws ArangoException {
141139

142-
ArangoConfigure configure = new ArangoConfigure();
140+
final ArangoConfigure configure = new ArangoConfigure();
143141

144142
// copy default arango host to fallback
145-
ArangoHost arangoHost = configure.getArangoHost();
146-
ArangoHost ah = new ArangoHost(arangoHost.getHost(), arangoHost.getPort());
143+
final ArangoHost arangoHost = configure.getArangoHost();
144+
final ArangoHost ah = new ArangoHost(arangoHost.getHost(), arangoHost.getPort());
147145
configure.addFallbackArangoHost(ah);
148146

149147
// change default port to wrong port
150148
arangoHost.setPort(1025);
151149
configure.init();
152150

153-
ArangoDriver driver = new ArangoDriver(configure);
151+
final ArangoDriver driver = new ArangoDriver(configure);
154152

155153
driver.getCollections();
156154

@@ -163,17 +161,17 @@ public void sslWithSelfSignedCertificateTest() throws ArangoException, KeyManage
163161
NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, URISyntaxException {
164162

165163
// create a sslContext for the self signed certificate
166-
URL resource = this.getClass().getResource(SSL_TRUSTSTORE);
167-
SSLContext sslContext = SSLContexts.custom()
168-
.loadTrustMaterial(Paths.get(resource.toURI()).toFile(), SSL_TRUSTSTORE_PASSWORD.toCharArray()).build();
164+
final URL resource = this.getClass().getResource(SSL_TRUSTSTORE);
165+
final SSLContext sslContext = SSLContexts.custom()
166+
.loadTrustMaterial(new File(resource.toURI()), SSL_TRUSTSTORE_PASSWORD.toCharArray()).build();
169167

170-
ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
168+
final ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
171169
configuration.setSslContext(sslContext);
172170
configuration.init();
173171

174-
ArangoDriver arangoDriver = new ArangoDriver(configuration);
172+
final ArangoDriver arangoDriver = new ArangoDriver(configuration);
175173

176-
ArangoVersion version = arangoDriver.getVersion();
174+
final ArangoVersion version = arangoDriver.getVersion();
177175

178176
Assert.assertNotNull(version);
179177
}

src/test/java/com/arangodb/example/ssl/SslExample.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.arangodb.example.ssl;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.net.URISyntaxException;
2122
import java.net.URL;
22-
import java.nio.file.Paths;
2323
import java.security.KeyManagementException;
2424
import java.security.KeyStoreException;
2525
import java.security.NoSuchAlgorithmException;
@@ -37,6 +37,7 @@
3737
import com.arangodb.ArangoHost;
3838
import com.arangodb.entity.ArangoVersion;
3939
import com.arangodb.http.HttpResponseEntity;
40+
import com.arangodb.util.TestUtils;
4041

4142
/*-
4243
* Example for using a HTTPS connection
@@ -75,67 +76,74 @@ public class SslExample {
7576
@Test
7677
public void httpTest() throws ArangoException {
7778

78-
ArangoConfigure configuration = new ArangoConfigure();
79+
final ArangoConfigure configuration = new ArangoConfigure();
7980
// get host and port from arangodb.properties
8081
// configuration.setArangoHost(new ArangoHost("localhost", 8529));
8182
configuration.init();
8283

83-
ArangoDriver arangoDriver = new ArangoDriver(configuration);
84+
final ArangoDriver arangoDriver = new ArangoDriver(configuration);
8485

85-
ArangoVersion version = arangoDriver.getVersion();
86+
final ArangoVersion version = arangoDriver.getVersion();
8687
Assert.assertNotNull(version);
8788

8889
}
8990

9091
@Test
9192
public void sslConnectionTest() throws ArangoException {
92-
// use HTTPS with java default trust store
93-
94-
ArangoConfigure configuration = new ArangoConfigure();
95-
configuration.setArangoHost(new ArangoHost("www.arangodb.com", 443));
96-
configuration.setUseSsl(true);
97-
configuration.init();
98-
99-
ArangoDriver arangoDriver = new ArangoDriver(configuration);
100-
101-
HttpResponseEntity response = arangoDriver.getHttpManager().doGet("/");
102-
Assert.assertEquals(200, response.getStatusCode());
93+
final String javaVersion = System.getProperty("java.version");
94+
if (TestUtils.compareVersion(javaVersion, "1.7") > -1) {
95+
// use HTTPS with java default trust store
96+
ArangoConfigure configuration = null;
97+
try {
98+
configuration = new ArangoConfigure();
99+
configuration.setArangoHost(new ArangoHost("www.arangodb.com", 443));
100+
configuration.setUseSsl(true);
101+
configuration.init();
102+
final ArangoDriver arangoDriver = new ArangoDriver(configuration);
103+
final HttpResponseEntity response = arangoDriver.getHttpManager().doGet("/");
104+
Assert.assertEquals(200, response.getStatusCode());
105+
} finally {
106+
if (configuration != null) {
107+
configuration.shutdown();
108+
}
109+
}
110+
}
103111
}
104112

105113
@Test
106114
public void sslWithSelfSignedCertificateTest() throws ArangoException, KeyManagementException,
107115
NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, URISyntaxException {
108116

109117
// create a sslContext for the self signed certificate
110-
URL resource = this.getClass().getResource(SSL_TRUSTSTORE);
111-
SSLContext sslContext = SSLContexts.custom()
112-
.loadTrustMaterial(Paths.get(resource.toURI()).toFile(), SSL_TRUSTSTORE_PASSWORD.toCharArray()).build();
118+
final URL resource = this.getClass().getResource(SSL_TRUSTSTORE);
119+
final SSLContext sslContext = SSLContexts.custom()
120+
.loadTrustMaterial(new File(resource.toURI()), SSL_TRUSTSTORE_PASSWORD.toCharArray()).build();
113121

114-
ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
122+
final ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
115123
configuration.setSslContext(sslContext);
116124
configuration.init();
117125

118-
ArangoDriver arangoDriver = new ArangoDriver(configuration);
126+
final ArangoDriver arangoDriver = new ArangoDriver(configuration);
119127

120-
ArangoVersion version = arangoDriver.getVersion();
128+
final ArangoVersion version = arangoDriver.getVersion();
121129
Assert.assertNotNull(version);
122130
}
123131

124132
@Test
125133
public void sslHandshakeExceptionTest() {
126-
ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
134+
final ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
127135
configuration.init();
128136

129-
ArangoDriver arangoDriver = new ArangoDriver(configuration);
137+
final ArangoDriver arangoDriver = new ArangoDriver(configuration);
130138

131139
try {
132140
// java do not trust self signed certificates
133141

134142
arangoDriver.getVersion();
135143
Assert.fail("this should fail");
136144

137-
} catch (ArangoException e) {
138-
Throwable cause = e.getCause();
145+
} catch (final ArangoException e) {
146+
final Throwable cause = e.getCause();
139147
Assert.assertTrue(cause instanceof javax.net.ssl.SSLHandshakeException);
140148
}
141149
}
@@ -145,23 +153,23 @@ public void sslPeerUnverifiedExceptionTest() throws ArangoException, KeyManageme
145153
NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, URISyntaxException {
146154

147155
// create a sslContext for the self signed certificate
148-
URL resource = this.getClass().getResource(SSL_TRUSTSTORE);
149-
SSLContext sslContext = SSLContexts.custom()
150-
.loadTrustMaterial(Paths.get(resource.toURI()).toFile(), SSL_TRUSTSTORE_PASSWORD.toCharArray()).build();
156+
final URL resource = this.getClass().getResource(SSL_TRUSTSTORE);
157+
final SSLContext sslContext = SSLContexts.custom()
158+
.loadTrustMaterial(new File(resource.toURI()), SSL_TRUSTSTORE_PASSWORD.toCharArray()).build();
151159

152-
ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
160+
final ArangoConfigure configuration = new ArangoConfigure("/ssl-arangodb.properties");
153161
// 127.0.0.1 is the wrong name
154162
configuration.getArangoHost().setHost("127.0.0.1");
155163
configuration.setSslContext(sslContext);
156164
configuration.init();
157165

158-
ArangoDriver arangoDriver = new ArangoDriver(configuration);
166+
final ArangoDriver arangoDriver = new ArangoDriver(configuration);
159167

160168
try {
161169
arangoDriver.getVersion();
162170
Assert.fail("this should fail");
163-
} catch (ArangoException e) {
164-
Throwable cause = e.getCause();
171+
} catch (final ArangoException e) {
172+
final Throwable cause = e.getCause();
165173
Assert.assertTrue(cause instanceof javax.net.ssl.SSLPeerUnverifiedException);
166174
}
167175

0 commit comments

Comments
 (0)