Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/systemtests/EngineTestBase.java

raw
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.qpid.proton.systemtests;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.nio.ByteBuffer;
25 import java.util.logging.Logger;
26
27 import org.apache.qpid.proton.amqp.messaging.Target;
28 import org.apache.qpid.proton.engine.Endpoint;
29 import org.apache.qpid.proton.engine.EndpointState;
30
31 public abstract class EngineTestBase
32 {
33 private static final Logger LOGGER = Logger.getLogger(EngineTestBase.class.getName());
34
35 private final TestLoggingHelper _testLoggingHelper = new TestLoggingHelper(LOGGER);
36 private final ProtonContainer _client = new ProtonContainer("clientContainer");
37 private final ProtonContainer _server = new ProtonContainer("serverContainer");
38
39 protected boolean shouldLogPumpedBytes()
40 {
41 return true;
42 }
43
44 protected TestLoggingHelper getTestLoggingHelper()
45 {
46 return _testLoggingHelper;
47 }
48
49 protected ProtonContainer getClient()
50 {
51 return _client;
52 }
53
54 protected ProtonContainer getServer()
55 {
56 return _server;
57 }
58
59 protected void assertClientHasNothingToOutput()
60 {
61 assertEquals(0, getClient().transport.getOutputBuffer().remaining());
62 getClient().transport.outputConsumed();
63 }
64
65 protected void pumpServerToClient()
66 {
67 ByteBuffer serverBuffer = getServer().transport.getOutputBuffer();
68
69 if (shouldLogPumpedBytes())
70 {
71 getTestLoggingHelper().prettyPrint(" <<<" + TestLoggingHelper.SERVER_PREFIX + " ", serverBuffer);
72 }
73 assertTrue("Server expected to produce some output", serverBuffer.hasRemaining());
74
75 ByteBuffer clientBuffer = getClient().transport.getInputBuffer();
76
77 clientBuffer.put(serverBuffer);
78
79 assertEquals("Client expected to consume all server's output", 0, serverBuffer.remaining());
80
81 getClient().transport.processInput().checkIsOk();
82 getServer().transport.outputConsumed();
83 }
84
85 protected void pumpAllServerPendingToClient()
86 {
87 while (getServer().transport.pending() != 0)
88 {
89 ByteBuffer serverBuffer = getServer().transport.getOutputBuffer();
90
91 if (shouldLogPumpedBytes())
92 {
93 getTestLoggingHelper().prettyPrint(" <<<" + TestLoggingHelper.SERVER_PREFIX + " ", serverBuffer);
94 }
95 assertTrue("Server expected to produce some output", serverBuffer.hasRemaining());
96
97 ByteBuffer clientBuffer = getClient().transport.getInputBuffer();
98
99 clientBuffer.put(serverBuffer);
100
101 assertEquals("Client expected to consume all server's output", 0, serverBuffer.remaining());
102
103 getClient().transport.processInput().checkIsOk();
104 getServer().transport.outputConsumed();
105 }
106
107 assertEquals("Client expected to consume all server's pending work", 0, getServer().transport.pending());
108 }
109
110 protected void pumpClientToServer()
111 {
112 ByteBuffer clientBuffer = getClient().transport.getOutputBuffer();
113
114 if (shouldLogPumpedBytes())
115 {
116 getTestLoggingHelper().prettyPrint(TestLoggingHelper.CLIENT_PREFIX + ">>> ", clientBuffer);
117 }
118 assertTrue("Client expected to produce some output", clientBuffer.hasRemaining());
119
120 ByteBuffer serverBuffer = getServer().transport.getInputBuffer();
121
122 serverBuffer.put(clientBuffer);
123
124 assertEquals("Server expected to consume all client's output", 0, clientBuffer.remaining());
125
126 getClient().transport.outputConsumed();
127 getServer().transport.processInput().checkIsOk();
128 }
129
130 protected void doOutputInputCycle() throws Exception
131 {
132 pumpClientToServer();
133
134 pumpServerToClient();
135 }
136
137 protected void assertEndpointState(Endpoint endpoint, EndpointState localState, EndpointState remoteState)
138 {
139 assertEquals(localState, endpoint.getLocalState());
140 assertEquals(remoteState, endpoint.getRemoteState());
141 }
142
143 protected void assertTerminusEquals(org.apache.qpid.proton.amqp.transport.Target expectedTarget, org.apache.qpid.proton.amqp.transport.Target actualTarget)
144 {
145 assertEquals(
146 ((Target)expectedTarget).getAddress(),
147 ((Target)actualTarget).getAddress());
148 }
149 }