/
AxisTestClient.java
158 lines (145 loc) · 7.15 KB
/
AxisTestClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.axis2.transport.testkit.axis2.client;
import javax.mail.internet.ContentType;
import javax.xml.namespace.QName;
import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.apache.axiom.attachments.Attachments;
import org.apache.axis2.Constants;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.TransportSender;
import org.apache.axis2.transport.base.BaseConstants;
import org.apache.axis2.transport.base.ManagementSupport;
import org.apache.axis2.transport.testkit.MessageExchangeValidator;
import org.apache.axis2.transport.testkit.axis2.util.MessageLevelMetricsCollectorImpl;
import org.apache.axis2.transport.testkit.channel.Channel;
import org.apache.axis2.transport.testkit.client.ClientOptions;
import org.apache.axis2.transport.testkit.client.TestClient;
import org.apache.axis2.transport.testkit.message.AxisMessage;
import org.apache.axis2.transport.testkit.name.Name;
import org.apache.axis2.transport.testkit.tests.Setup;
import org.apache.axis2.transport.testkit.tests.TearDown;
import org.apache.axis2.transport.testkit.tests.Transient;
import org.apache.axis2.transport.testkit.util.ContentTypeUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@Name("axis")
public class AxisTestClient implements TestClient, MessageExchangeValidator {
private static final Log log = LogFactory.getLog(AxisTestClient.class);
private @Transient AxisTestClientConfigurator[] configurators;
private @Transient TransportSender sender;
protected @Transient ServiceClient serviceClient;
protected @Transient Options axisOptions;
private long messagesSent;
private long bytesSent;
private MessageLevelMetricsCollectorImpl metrics;
@Setup @SuppressWarnings("unused")
private void setUp(AxisTestClientContext context, Channel channel, AxisTestClientConfigurator[] configurators) throws Exception {
this.configurators = configurators;
sender = context.getSender();
serviceClient = new ServiceClient(context.getConfigurationContext(), null);
axisOptions = new Options();
axisOptions.setTo(channel.getEndpointReference());
serviceClient.setOptions(axisOptions);
}
@TearDown @SuppressWarnings("unused")
private void tearDown() throws Exception {
serviceClient.cleanup();
}
public ContentType getContentType(ClientOptions options, ContentType contentType) {
// TODO: this may be incorrect in some cases
String charset = options.getCharset();
if (charset == null) {
return contentType;
} else {
return ContentTypeUtil.addCharset(contentType, options.getCharset());
}
}
public void beforeSend() throws Exception {
if (sender instanceof ManagementSupport) {
ManagementSupport sender = (ManagementSupport)this.sender;
messagesSent = sender.getMessagesSent();
bytesSent = sender.getBytesSent();
metrics = new MessageLevelMetricsCollectorImpl();
} else {
metrics = null;
}
}
protected MessageContext send(ClientOptions options, AxisMessage message, QName operationQName,
boolean block, String resultMessageLabel) throws Exception {
OperationClient mepClient = serviceClient.createClient(operationQName);
MessageContext mc = new MessageContext();
mc.setProperty(Constants.Configuration.MESSAGE_TYPE, message.getMessageType());
mc.setEnvelope(message.getEnvelope());
Attachments attachments = message.getAttachments();
if (attachments != null) {
mc.setAttachmentMap(attachments);
mc.setDoingSwA(true);
mc.setProperty(Constants.Configuration.ENABLE_SWA, true);
}
for (AxisTestClientConfigurator configurator : configurators) {
configurator.setupRequestMessageContext(mc);
}
mc.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, options.getCharset());
mc.setServiceContext(serviceClient.getServiceContext());
if (metrics != null) {
mc.setProperty(BaseConstants.METRICS_COLLECTOR, metrics);
}
mepClient.addMessageContext(mc);
mepClient.execute(block);
mepClient.complete(mc);
return resultMessageLabel == null ? null : mepClient.getMessageContext(resultMessageLabel);
}
public void afterReceive() throws Exception {
if (sender instanceof ManagementSupport) {
ManagementSupport sender = (ManagementSupport)this.sender;
synchronized (metrics) {
long start = System.currentTimeMillis();
while (true) {
try {
Assert.assertEquals(1, metrics.getMessagesSent());
Assert.assertEquals(messagesSent+1, sender.getMessagesSent());
long thisBytesSent = metrics.getBytesSent();
Assert.assertTrue("No increase in bytes sent in message level metrics", thisBytesSent != 0);
long newBytesSent = sender.getBytesSent();
Assert.assertTrue("No increase in bytes sent in transport level metrics", newBytesSent > bytesSent);
Assert.assertEquals("Mismatch between message and transport level metrics", thisBytesSent, newBytesSent - bytesSent);
break;
} catch (AssertionFailedError ex) {
// SYNAPSE-491: Maybe the transport sender didn't finish updating the
// metrics yet. We give it a couple of seconds to do so.
long remaining = start + 5000 - System.currentTimeMillis();
if (remaining < 0) {
throw ex;
} else {
log.debug("The transport sender didn't update the metrics yet ("
+ ex.getMessage() + "). Waiting for " + remaining + " ms.");
metrics.wait(remaining);
}
}
}
}
log.debug("Message level metrics check OK");
}
}
}