1+ /*
2+ * Copyright © 2014 Lable (info@lable.nl)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ /*
18+ * Copyright © 2015 Lable (info@lable.nl)
19+ *
20+ * Licensed under the Apache License, Version 2.0 (the "License");
21+ * you may not use this file except in compliance with the License.
22+ * You may obtain a copy of the License at
23+ *
24+ * http://www.apache.org/licenses/LICENSE-2.0
25+ *
26+ * Unless required by applicable law or agreed to in writing, software
27+ * distributed under the License is distributed on an "AS IS" BASIS,
28+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+ * See the License for the specific language governing permissions and
30+ * limitations under the License.
31+ */
32+ package org .lable .oss .uniqueid .etcd ;
33+
34+ import io .etcd .jetcd .launcher .Etcd ;
35+ import io .etcd .jetcd .launcher .EtcdCluster ;
36+ import org .junit .rules .TestRule ;
37+ import org .junit .runner .Description ;
38+ import org .junit .runners .model .Statement ;
39+
40+ import java .net .URI ;
41+ import java .util .List ;
42+
43+ /**
44+ * The jetcd library dropped support for JUnit4, so we wrap the cluster ourselves.
45+ */
46+ public class EtcdTestCluster implements TestRule {
47+
48+ private final String clusterName ;
49+ private final int nodes ;
50+ private final boolean ssl ;
51+ private EtcdCluster cluster ;
52+
53+ public EtcdTestCluster (String clusterName , int nodes ) {
54+ this (clusterName , nodes , false );
55+ }
56+
57+ public EtcdTestCluster (String clusterName , int nodes , boolean ssl ) {
58+ this .clusterName = clusterName ;
59+ this .nodes = nodes ;
60+ this .ssl = ssl ;
61+ }
62+
63+ @ Override
64+ public Statement apply (Statement base , Description description ) {
65+ return new Statement () {
66+ @ Override
67+ public void evaluate () throws Throwable {
68+ cluster = Etcd .builder ()
69+ .withClusterName (clusterName )
70+ .withNodes (nodes )
71+ .withSsl (ssl )
72+ .build ();
73+
74+ cluster .start ();
75+ try {
76+ base .evaluate ();
77+ } finally {
78+ cluster .close ();
79+ cluster = null ;
80+ }
81+ }
82+ };
83+ }
84+
85+ public List <URI > getClientEndpoints () {
86+ return cluster .clientEndpoints ();
87+ }
88+ }
0 commit comments