1- // Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
1+ // Copyright (c) 2007-2019 Pivotal Software, Inc. All rights reserved.
22//
33// This software, the RabbitMQ Java client library, is triple-licensed under the
44// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
1515
1616package com .rabbitmq .client .test ;
1717
18- import com .rabbitmq .client .AMQP ;
19- import com .rabbitmq .client .Channel ;
20- import com .rabbitmq .client .Connection ;
21- import com .rabbitmq .client .ConnectionFactory ;
22- import com .rabbitmq .client .DefaultConsumer ;
23- import com .rabbitmq .client .Envelope ;
24- import com .rabbitmq .client .Recoverable ;
25- import com .rabbitmq .client .RecoverableConnection ;
26- import com .rabbitmq .client .RecoveryListener ;
27- import com .rabbitmq .client .ShutdownSignalException ;
18+ import com .rabbitmq .client .*;
2819import com .rabbitmq .client .impl .NetworkConnection ;
2920import com .rabbitmq .client .impl .recovery .AutorecoveringConnection ;
3021import com .rabbitmq .tools .Host ;
22+ import org .junit .AssumptionViolatedException ;
23+ import org .junit .rules .TestRule ;
24+ import org .junit .runner .Description ;
25+ import org .junit .runners .model .Statement ;
3126import org .slf4j .LoggerFactory ;
3227
3328import javax .net .ssl .SSLContext ;
4641
4742public class TestUtils {
4843
49- public static final boolean USE_NIO = System .getProperty ("use.nio" ) == null ? false : true ;
44+ public static final boolean USE_NIO = System .getProperty ("use.nio" ) != null ;
5045
5146 public static ConnectionFactory connectionFactory () {
5247 ConnectionFactory connectionFactory = new ConnectionFactory ();
@@ -79,7 +74,7 @@ public static SSLContext getSSLContext() throws NoSuchAlgorithmException {
7974
8075 // pick the first protocol available, preferring TLSv1.2, then TLSv1,
8176 // falling back to SSLv3 if running on an ancient/crippled JDK
82- for (String proto : Arrays .asList ("TLSv1.2" , "TLSv1" , "SSLv3" )) {
77+ for (String proto : Arrays .asList ("TLSv1.2" , "TLSv1" , "SSLv3" )) {
8378 try {
8479 c = SSLContext .getInstance (proto );
8580 return c ;
@@ -90,31 +85,49 @@ public static SSLContext getSSLContext() throws NoSuchAlgorithmException {
9085 throw new NoSuchAlgorithmException ();
9186 }
9287
88+ public static TestRule atLeast38 () {
89+ return new BrokerVersionTestRule ("3.8.0" );
90+ }
91+
9392 public static boolean isVersion37orLater (Connection connection ) {
93+ return atLeastVersion ("3.7.0" , connection );
94+ }
95+
96+ public static boolean isVersion38orLater (Connection connection ) {
97+ return atLeastVersion ("3.8.0" , connection );
98+ }
99+
100+ private static boolean atLeastVersion (String expectedVersion , Connection connection ) {
94101 String currentVersion = null ;
95102 try {
96- currentVersion = connection .getServerProperties ().get ("version" ).toString ();
97- // versions built from source: 3.7.0+rc.1.4.gedc5d96
98- if (currentVersion .contains ("+" )) {
99- currentVersion = currentVersion .substring (0 , currentVersion .indexOf ("+" ));
100- }
101- // alpha (snapshot) versions: 3.7.0~alpha.449-1
102- if (currentVersion .contains ("~" )) {
103- currentVersion = currentVersion .substring (0 , currentVersion .indexOf ("~" ));
104- }
105- // alpha (snapshot) versions: 3.7.1-alpha.40
106- if (currentVersion .contains ("-" )) {
107- currentVersion = currentVersion .substring (0 , currentVersion .indexOf ("-" ));
108- }
109- return "0.0.0" .equals (currentVersion ) || versionCompare (currentVersion , "3.7.0" ) >= 0 ;
103+ currentVersion = currentVersion (
104+ connection .getServerProperties ().get ("version" ).toString ()
105+ );
106+ return "0.0.0" .equals (currentVersion ) || versionCompare (currentVersion , expectedVersion ) >= 0 ;
110107 } catch (RuntimeException e ) {
111108 LoggerFactory .getLogger (TestUtils .class ).warn ("Unable to parse broker version {}" , currentVersion , e );
112109 throw e ;
113110 }
114111 }
115112
113+ private static String currentVersion (String currentVersion ) {
114+ // versions built from source: 3.7.0+rc.1.4.gedc5d96
115+ if (currentVersion .contains ("+" )) {
116+ currentVersion = currentVersion .substring (0 , currentVersion .indexOf ("+" ));
117+ }
118+ // alpha (snapshot) versions: 3.7.0~alpha.449-1
119+ if (currentVersion .contains ("~" )) {
120+ currentVersion = currentVersion .substring (0 , currentVersion .indexOf ("~" ));
121+ }
122+ // alpha (snapshot) versions: 3.7.1-alpha.40
123+ if (currentVersion .contains ("-" )) {
124+ currentVersion = currentVersion .substring (0 , currentVersion .indexOf ("-" ));
125+ }
126+ return currentVersion ;
127+ }
128+
116129 public static boolean sendAndConsumeMessage (String exchange , String routingKey , String queue , Connection c )
117- throws IOException , TimeoutException , InterruptedException {
130+ throws IOException , TimeoutException , InterruptedException {
118131 Channel ch = c .createChannel ();
119132 try {
120133 ch .confirmSelect ();
@@ -249,4 +262,28 @@ public static int randomNetworkPort() throws IOException {
249262 socket .close ();
250263 return port ;
251264 }
265+
266+ private static class BrokerVersionTestRule implements TestRule {
267+
268+ private final String version ;
269+
270+ public BrokerVersionTestRule (String version ) {
271+ this .version = version ;
272+ }
273+
274+ @ Override
275+ public Statement apply (Statement base , Description description ) {
276+ return new Statement () {
277+ @ Override
278+ public void evaluate () throws Throwable {
279+ try (Connection c = TestUtils .connectionFactory ().newConnection ()) {
280+ if (!TestUtils .atLeastVersion (version , c )) {
281+ throw new AssumptionViolatedException ("Broker version < " + version + ", skipping." );
282+ }
283+ }
284+ base .evaluate ();
285+ }
286+ };
287+ }
288+ }
252289}
0 commit comments