Skip to content

Commit 516fe11

Browse files
committed
Extract the common code from AbstractAxis2Server and ClientHelper.
1 parent 0ecf612 commit 516fe11

3 files changed

Lines changed: 66 additions & 37 deletions

File tree

‎modules/testutils/src/main/java/org/apache/axis2/testutils/AbstractAxis2Server.java‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,20 @@
2323
import org.apache.axis2.AxisFault;
2424
import org.apache.axis2.addressing.EndpointReference;
2525
import org.apache.axis2.context.ConfigurationContext;
26-
import org.apache.axis2.context.ConfigurationContextFactory;
2726
import org.apache.axis2.engine.AxisConfiguration;
28-
import org.junit.rules.ExternalResource;
2927

30-
public abstract class AbstractAxis2Server extends ExternalResource {
31-
private final String repositoryPath;
28+
public abstract class AbstractAxis2Server extends AbstractConfigurationContextRule {
3229
private final AxisServiceFactory[] serviceFactories;
33-
private ConfigurationContext configurationContext;
3430

3531
public AbstractAxis2Server(String repositoryPath, AxisServiceFactory... serviceFactories) {
36-
this.repositoryPath = repositoryPath;
32+
super(repositoryPath);
3733
this.serviceFactories = serviceFactories;
3834
}
3935

40-
final String getRepositoryPath() {
41-
return repositoryPath;
42-
}
43-
44-
public final ConfigurationContext getConfigurationContext() {
45-
if (configurationContext == null) {
46-
throw new IllegalStateException();
47-
}
48-
return configurationContext;
49-
}
50-
5136
@Override
5237
protected void before() throws Throwable {
53-
configurationContext =
54-
ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryPath);
38+
super.before();
39+
ConfigurationContext configurationContext = getConfigurationContext();
5540
AxisConfiguration axisConfiguration = configurationContext.getAxisConfiguration();
5641
for (AxisServiceFactory serviceFactory : serviceFactories) {
5742
axisConfiguration.addService(serviceFactory.createService(axisConfiguration));
@@ -62,7 +47,7 @@ protected void before() throws Throwable {
6247
@Override
6348
protected void after() {
6449
stopServer();
65-
configurationContext = null;
50+
super.after();
6651
}
6752

6853
protected abstract void startServer(ConfigurationContext configurationContext) throws Throwable;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.axis2.testutils;
20+
21+
import org.apache.axis2.context.ConfigurationContext;
22+
import org.apache.axis2.context.ConfigurationContextFactory;
23+
import org.junit.rules.ExternalResource;
24+
25+
public abstract class AbstractConfigurationContextRule extends ExternalResource {
26+
private final String repositoryPath;
27+
private ConfigurationContext configurationContext;
28+
29+
public AbstractConfigurationContextRule(String repositoryPath) {
30+
this.repositoryPath = repositoryPath;
31+
}
32+
33+
final String getRepositoryPath() {
34+
return repositoryPath;
35+
}
36+
37+
public final ConfigurationContext getConfigurationContext() {
38+
if (configurationContext == null) {
39+
throw new IllegalStateException();
40+
}
41+
return configurationContext;
42+
}
43+
44+
@Override
45+
protected void before() throws Throwable {
46+
configurationContext =
47+
ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryPath);
48+
}
49+
50+
@Override
51+
protected void after() {
52+
configurationContext = null;
53+
}
54+
}

‎modules/testutils/src/main/java/org/apache/axis2/testutils/ClientHelper.java‎

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@
3030
import org.apache.axis2.client.ServiceClient;
3131
import org.apache.axis2.client.Stub;
3232
import org.apache.axis2.context.ConfigurationContext;
33-
import org.apache.axis2.context.ConfigurationContextFactory;
34-
import org.junit.rules.ExternalResource;
3533

36-
public class ClientHelper extends ExternalResource {
34+
public class ClientHelper extends AbstractConfigurationContextRule {
3735
private final AbstractAxis2Server server;
38-
private final String repositoryPath;
39-
private ConfigurationContext configurationContext;
4036

4137
public ClientHelper(AbstractAxis2Server server, String repositoryPath) {
38+
super(repositoryPath);
4239
this.server = server;
43-
this.repositoryPath = repositoryPath;
4440
}
4541

4642
public ClientHelper(AbstractAxis2Server server) {
@@ -49,21 +45,15 @@ public ClientHelper(AbstractAxis2Server server) {
4945

5046
@Override
5147
protected final void before() throws Throwable {
52-
configurationContext =
53-
ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryPath);
48+
super.before();
5449
SSLContext sslContext = server.getClientSSLContext();
5550
if (sslContext != null) {
56-
configurationContext.setProperty(SSLContext.class.getName(), sslContext);
51+
getConfigurationContext().setProperty(SSLContext.class.getName(), sslContext);
5752
}
5853
}
5954

60-
@Override
61-
protected final void after() {
62-
configurationContext = null;
63-
}
64-
6555
public final ServiceClient createServiceClient(String serviceName) throws Exception {
66-
ServiceClient serviceClient = new ServiceClient(configurationContext, null);
56+
ServiceClient serviceClient = new ServiceClient(getConfigurationContext(), null);
6757
serviceClient.getOptions().setTo(server.getEndpointReference(serviceName));
6858
configureServiceClient(serviceClient);
6959
return serviceClient;
@@ -84,7 +74,7 @@ protected URLConnection openConnection(URL url) throws IOException {
8474
} else {
8575
handler = null;
8676
}
87-
ServiceClient serviceClient = new ServiceClient(configurationContext,
77+
ServiceClient serviceClient = new ServiceClient(getConfigurationContext(),
8878
new URL(null, server.getEndpoint(serviceName) + "?wsdl", handler), wsdlServiceName, portName);
8979
configureServiceClient(serviceClient);
9080
return serviceClient;
@@ -93,7 +83,7 @@ protected URLConnection openConnection(URL url) throws IOException {
9383
public final <T extends Stub> T createStub(Class<T> type, String serviceName) throws Exception {
9484
T stub = type
9585
.getConstructor(ConfigurationContext.class, String.class)
96-
.newInstance(configurationContext, server.getEndpoint(serviceName));
86+
.newInstance(getConfigurationContext(), server.getEndpoint(serviceName));
9787
configureServiceClient(stub._getServiceClient());
9888
return stub;
9989
}

0 commit comments

Comments
 (0)