├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── build.gradle
├── config
└── checkstyle
│ └── checkstyle.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
└── java
│ └── jetcd
│ ├── EtcdApi.java
│ ├── EtcdClient.java
│ ├── EtcdClientFactory.java
│ ├── EtcdClientImpl.java
│ ├── EtcdException.java
│ └── EtcdResponse.java
└── test
└── java
└── jetcd
└── EtcdClientTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | .idea
3 | *.iml
4 | out
5 | build
6 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | install:
3 | - git clone https://github.com/coreos/etcd
4 | - pushd etcd && git checkout v0.4.6 && ./build && popd
5 | before_script:
6 | - etcd/bin/etcd &
7 | after_script:
8 | - pkill -f etcd
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction, and
10 | distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by the copyright
13 | owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all other entities
16 | that control, are controlled by, or are under common control with that entity.
17 | For the purposes of this definition, "control" means (i) the power, direct or
18 | indirect, to cause the direction or management of such entity, whether by
19 | contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20 | outstanding shares, or (iii) beneficial ownership of such entity.
21 |
22 | "You" (or "Your") shall mean an individual or Legal Entity exercising
23 | permissions granted by this License.
24 |
25 | "Source" form shall mean the preferred form for making modifications, including
26 | but not limited to software source code, documentation source, and configuration
27 | files.
28 |
29 | "Object" form shall mean any form resulting from mechanical transformation or
30 | translation of a Source form, including but not limited to compiled object code,
31 | generated documentation, and conversions to other media types.
32 |
33 | "Work" shall mean the work of authorship, whether in Source or Object form, made
34 | available under the License, as indicated by a copyright notice that is included
35 | in or attached to the work (an example is provided in the Appendix below).
36 |
37 | "Derivative Works" shall mean any work, whether in Source or Object form, that
38 | is based on (or derived from) the Work and for which the editorial revisions,
39 | annotations, elaborations, or other modifications represent, as a whole, an
40 | original work of authorship. For the purposes of this License, Derivative Works
41 | shall not include works that remain separable from, or merely link (or bind by
42 | name) to the interfaces of, the Work and Derivative Works thereof.
43 |
44 | "Contribution" shall mean any work of authorship, including the original version
45 | of the Work and any modifications or additions to that Work or Derivative Works
46 | thereof, that is intentionally submitted to Licensor for inclusion in the Work
47 | by the copyright owner or by an individual or Legal Entity authorized to submit
48 | on behalf of the copyright owner. For the purposes of this definition,
49 | "submitted" means any form of electronic, verbal, or written communication sent
50 | to the Licensor or its representatives, including but not limited to
51 | communication on electronic mailing lists, source code control systems, and
52 | issue tracking systems that are managed by, or on behalf of, the Licensor for
53 | the purpose of discussing and improving the Work, but excluding communication
54 | that is conspicuously marked or otherwise designated in writing by the copyright
55 | owner as "Not a Contribution."
56 |
57 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
58 | of whom a Contribution has been received by Licensor and subsequently
59 | incorporated within the Work.
60 |
61 | 2. Grant of Copyright License.
62 |
63 | Subject to the terms and conditions of this License, each Contributor hereby
64 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
65 | irrevocable copyright license to reproduce, prepare Derivative Works of,
66 | publicly display, publicly perform, sublicense, and distribute the Work and such
67 | Derivative Works in Source or Object form.
68 |
69 | 3. Grant of Patent License.
70 |
71 | Subject to the terms and conditions of this License, each Contributor hereby
72 | grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
73 | irrevocable (except as stated in this section) patent license to make, have
74 | made, use, offer to sell, sell, import, and otherwise transfer the Work, where
75 | such license applies only to those patent claims licensable by such Contributor
76 | that are necessarily infringed by their Contribution(s) alone or by combination
77 | of their Contribution(s) with the Work to which such Contribution(s) was
78 | submitted. If You institute patent litigation against any entity (including a
79 | cross-claim or counterclaim in a lawsuit) alleging that the Work or a
80 | Contribution incorporated within the Work constitutes direct or contributory
81 | patent infringement, then any patent licenses granted to You under this License
82 | for that Work shall terminate as of the date such litigation is filed.
83 |
84 | 4. Redistribution.
85 |
86 | You may reproduce and distribute copies of the Work or Derivative Works thereof
87 | in any medium, with or without modifications, and in Source or Object form,
88 | provided that You meet the following conditions:
89 |
90 | You must give any other recipients of the Work or Derivative Works a copy of
91 | this License; and
92 | You must cause any modified files to carry prominent notices stating that You
93 | changed the files; and
94 | You must retain, in the Source form of any Derivative Works that You distribute,
95 | all copyright, patent, trademark, and attribution notices from the Source form
96 | of the Work, excluding those notices that do not pertain to any part of the
97 | Derivative Works; and
98 | If the Work includes a "NOTICE" text file as part of its distribution, then any
99 | Derivative Works that You distribute must include a readable copy of the
100 | attribution notices contained within such NOTICE file, excluding those notices
101 | that do not pertain to any part of the Derivative Works, in at least one of the
102 | following places: within a NOTICE text file distributed as part of the
103 | Derivative Works; within the Source form or documentation, if provided along
104 | with the Derivative Works; or, within a display generated by the Derivative
105 | Works, if and wherever such third-party notices normally appear. The contents of
106 | the NOTICE file are for informational purposes only and do not modify the
107 | License. You may add Your own attribution notices within Derivative Works that
108 | You distribute, alongside or as an addendum to the NOTICE text from the Work,
109 | provided that such additional attribution notices cannot be construed as
110 | modifying the License.
111 | You may add Your own copyright statement to Your modifications and may provide
112 | additional or different license terms and conditions for use, reproduction, or
113 | distribution of Your modifications, or for any such Derivative Works as a whole,
114 | provided Your use, reproduction, and distribution of the Work otherwise complies
115 | with the conditions stated in this License.
116 |
117 | 5. Submission of Contributions.
118 |
119 | Unless You explicitly state otherwise, any Contribution intentionally submitted
120 | for inclusion in the Work by You to the Licensor shall be under the terms and
121 | conditions of this License, without any additional terms or conditions.
122 | Notwithstanding the above, nothing herein shall supersede or modify the terms of
123 | any separate license agreement you may have executed with Licensor regarding
124 | such Contributions.
125 |
126 | 6. Trademarks.
127 |
128 | This License does not grant permission to use the trade names, trademarks,
129 | service marks, or product names of the Licensor, except as required for
130 | reasonable and customary use in describing the origin of the Work and
131 | reproducing the content of the NOTICE file.
132 |
133 | 7. Disclaimer of Warranty.
134 |
135 | Unless required by applicable law or agreed to in writing, Licensor provides the
136 | Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
137 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
138 | including, without limitation, any warranties or conditions of TITLE,
139 | NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
140 | solely responsible for determining the appropriateness of using or
141 | redistributing the Work and assume any risks associated with Your exercise of
142 | permissions under this License.
143 |
144 | 8. Limitation of Liability.
145 |
146 | In no event and under no legal theory, whether in tort (including negligence),
147 | contract, or otherwise, unless required by applicable law (such as deliberate
148 | and grossly negligent acts) or agreed to in writing, shall any Contributor be
149 | liable to You for damages, including any direct, indirect, special, incidental,
150 | or consequential damages of any character arising as a result of this License or
151 | out of the use or inability to use the Work (including but not limited to
152 | damages for loss of goodwill, work stoppage, computer failure or malfunction, or
153 | any and all other commercial damages or losses), even if such Contributor has
154 | been advised of the possibility of such damages.
155 |
156 | 9. Accepting Warranty or Additional Liability.
157 |
158 | While redistributing the Work or Derivative Works thereof, You may choose to
159 | offer, and charge a fee for, acceptance of support, warranty, indemnity, or
160 | other liability obligations and/or rights consistent with this License. However,
161 | in accepting such obligations, You may act only on Your own behalf and on Your
162 | sole responsibility, not on behalf of any other Contributor, and only if You
163 | agree to indemnify, defend, and hold each Contributor harmless for any liability
164 | incurred by, or claims asserted against, such Contributor by reason of your
165 | accepting any such warranty or additional liability.
166 |
167 | END OF TERMS AND CONDITIONS
168 |
169 | APPENDIX: How to apply the Apache License to your work
170 |
171 | To apply the Apache License to your work, attach the following boilerplate
172 | notice, with the fields enclosed by brackets "[]" replaced with your own
173 | identifying information. (Don't include the brackets!) The text should be
174 | enclosed in the appropriate comment syntax for the file format. We also
175 | recommend that a file or class name and description of purpose be included on
176 | the same "printed page" as the copyright notice for easier identification within
177 | third-party archives.
178 |
179 | Copyright [yyyy] [name of copyright owner]
180 |
181 | Licensed under the Apache License, Version 2.0 (the "License");
182 | you may not use this file except in compliance with the License.
183 | You may obtain a copy of the License at
184 |
185 | http://www.apache.org/licenses/LICENSE-2.0
186 |
187 | Unless required by applicable law or agreed to in writing, software
188 | distributed under the License is distributed on an "AS IS" BASIS,
189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190 | See the License for the specific language governing permissions and
191 | limitations under the License.
192 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | jetcd: A Java client for etcd
2 | =============================
3 |
4 | [](https://travis-ci.org/diwakergupta/jetcd)
5 |
6 | Download
7 | --------
8 |
9 |
10 | net.floatingsun
11 | jetcd
12 | 0.3.0
13 |
14 |
15 | Usage
16 | -----
17 |
18 | // Connects to "http://127.0.0.1:4001" by default
19 | EtcdClient client = EtcdClientFactory.newInstance();
20 | // Use EtcdClientFactory.newInstance(serverUrl) to override
21 |
22 | client.setKey("hello", "world");
23 | client.getKey("hello") // returns "world"
24 | client.deleteKey("hello")
25 |
26 | TODO
27 | ----
28 |
29 | * multiple servers with redirect
30 | * retries and better failure handling
31 | * better unification of 'getKey' and 'list'
32 | * support for watch
33 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id "com.github.ben-manes.versions" version "0.5"
3 | id "com.bmuschko.nexus" version "2.1.1"
4 | }
5 |
6 | apply plugin: 'checkstyle'
7 | apply plugin: 'java'
8 |
9 | group = 'net.floatingsun'
10 | version = '0.4.0-SNAPSHOT'
11 | sourceCompatibility = 1.7
12 | targetCompatibility = 1.7
13 |
14 | repositories {
15 | mavenCentral()
16 | }
17 |
18 | dependencies {
19 | compile 'org.slf4j:slf4j-api:1.7.7'
20 | compile 'com.squareup.retrofit:retrofit:1.7.1'
21 | compile 'com.squareup.retrofit:converter-jackson:1.7.0'
22 | compile 'com.google.guava:guava:18.0'
23 |
24 | testCompile 'junit:junit:4.11'
25 | testCompile 'org.assertj:assertj-core:1.7.0'
26 | }
27 |
28 | modifyPom {
29 | project {
30 | name 'jetcd'
31 | description 'Java client for etcd'
32 | url 'https://github.com/diwakergupta/jetcd'
33 | inceptionYear '2013'
34 |
35 | scm {
36 | url 'https://github.com/diwakergupta/jetcd'
37 | connection 'scm:https://github.com/diwakergupta/jetcd'
38 | developerConnection 'scm:git@github.com:diwakergupta/jetcd.git'
39 | }
40 |
41 | licenses {
42 | license {
43 | name 'The Apache Software License, Version 2.0'
44 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
45 | distribution 'repo'
46 | }
47 | }
48 |
49 | developers {
50 | developer {
51 | id 'diwakergupta'
52 | name 'Diwaker Gupta'
53 | email 'diwaker@floatingsun.net'
54 | }
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/config/checkstyle/checkstyle.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
129 |
130 |
131 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
652 |
653 |
654 |
655 |
656 |
657 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/diwakergupta/jetcd/db0a44a7d7adef3ec59d2806fdcae1f63c3e0b00/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Oct 22 17:08:28 PDT 2014
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'jetcd'
2 |
--------------------------------------------------------------------------------
/src/main/java/jetcd/EtcdApi.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import retrofit.client.Response;
20 | import retrofit.http.DELETE;
21 | import retrofit.http.Field;
22 | import retrofit.http.FormUrlEncoded;
23 | import retrofit.http.GET;
24 | import retrofit.http.PUT;
25 | import retrofit.http.Path;
26 | import retrofit.http.Query;
27 |
28 | interface EtcdApi {
29 | @FormUrlEncoded
30 | @PUT("/v2/keys/{key}")
31 | EtcdResponse set(@Path("key") String key,
32 | @Field("value") String value,
33 | @Field("ttl") Integer ttl,
34 | @Field("dir") Boolean dir,
35 | @Query("prevValue") String prevValue) throws EtcdException;
36 |
37 | @GET("/version")
38 | Response version();
39 |
40 | @GET("/v2/keys/{key}")
41 | EtcdResponse get(@Path("key") String key) throws EtcdException;
42 |
43 | @DELETE("/v2/keys/{key}")
44 | EtcdResponse delete(@Path("key") String key) throws EtcdException;
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/jetcd/EtcdClient.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import java.util.Map;
20 |
21 | /** The Etcd client API. */
22 | public interface EtcdClient {
23 | /**
24 | * @return Version of the etcd instance
25 | * @throws EtcdException in case of an error
26 | */
27 | String version() throws EtcdException;
28 |
29 | /**
30 | * Retrieve the value of the given key, if set.
31 | *
32 | * @param key Key to look up
33 | * @return value Value for the given key
34 | * @throws EtcdException in case of an error (e.g. key doesn't exist)
35 | */
36 | String get(String key) throws EtcdException;
37 |
38 | /**
39 | * Set value for the given key.
40 | *
41 | * @param key Key to set value for
42 | * @param value New value for the key
43 | * @throws EtcdException in case of an error
44 | */
45 | void set(String key, String value) throws EtcdException;
46 |
47 | /**
48 | * Set value for the given key with given TTL.
49 | *
50 | * @param key Key to set value for
51 | * @param value New value for the key
52 | * @param ttl Key will expire after these many seconds
53 | * @throws EtcdException in case of an error
54 | */
55 | void set(String key, String value, int ttl) throws EtcdException;
56 |
57 | /**
58 | * Delete value for the given key.
59 | *
60 | * @param key Key to delete value for
61 | * @throws EtcdException in case of an error
62 | */
63 | void delete(String key) throws EtcdException;
64 |
65 | /**
66 | * List directory at given path.
67 | *
68 | * @param path Given path
69 | * @return Map of key,value pairs under the path
70 | * @throws EtcdException in case of an error
71 | */
72 | Map list(String path) throws EtcdException;
73 |
74 | /**
75 | * Compare the value at a given key and swap with newValue if the oldValue
76 | * matches.
77 | *
78 | * @param key Key to test/set value at
79 | * @param oldValue Old value
80 | * @param newValue New value
81 | * @throws EtcdException in case of an error
82 | */
83 | void compareAndSwap(String key, String oldValue, String newValue)
84 | throws EtcdException;
85 | }
86 |
--------------------------------------------------------------------------------
/src/main/java/jetcd/EtcdClientFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import com.google.common.base.Optional;
20 |
21 | import retrofit.client.Client;
22 |
23 | /** Factory for Etcd clients. */
24 | public final class EtcdClientFactory {
25 | private EtcdClientFactory() {
26 | // Factory
27 | }
28 |
29 | public static EtcdClient newInstance() {
30 | return newInstance("http://127.0.0.1:4001");
31 | }
32 |
33 | public static EtcdClient newInstance(final String server) {
34 | return new EtcdClientImpl(Optional.absent(), server);
35 | }
36 |
37 | public static EtcdClient newInstance(final Client client,
38 | final String server) {
39 | return new EtcdClientImpl(Optional.of(client), server);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/jetcd/EtcdClientImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import java.io.IOException;
20 | import java.io.InputStreamReader;
21 | import java.util.Map;
22 |
23 | import com.fasterxml.jackson.databind.DeserializationFeature;
24 | import com.fasterxml.jackson.databind.ObjectMapper;
25 | import com.google.common.base.Charsets;
26 | import com.google.common.base.Optional;
27 | import com.google.common.base.Preconditions;
28 | import com.google.common.base.Strings;
29 | import com.google.common.collect.ImmutableMap;
30 | import com.google.common.io.CharStreams;
31 |
32 | import retrofit.ErrorHandler;
33 | import retrofit.RestAdapter;
34 | import retrofit.RetrofitError;
35 | import retrofit.client.Client;
36 | import retrofit.converter.JacksonConverter;
37 |
38 |
39 | final class EtcdClientImpl implements EtcdClient {
40 | private static final ObjectMapper objectMapper = new ObjectMapper()
41 | .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
42 | private final EtcdApi etcd;
43 |
44 | EtcdClientImpl(final Optional client, final String server) {
45 | RestAdapter.Builder builder = new RestAdapter.Builder()
46 | .setConverter(new JacksonConverter(objectMapper))
47 | .setEndpoint(server)
48 | .setErrorHandler(new EtcdErrorHandler());
49 | if (client.isPresent()) {
50 | builder.setClient(client.get());
51 | }
52 | etcd = builder.build().create(EtcdApi.class);
53 | }
54 |
55 | @Override
56 | public String version() throws EtcdException {
57 | try (InputStreamReader reader = new InputStreamReader(
58 | etcd.version().getBody().in(), Charsets.UTF_8)) {
59 | return CharStreams.toString(reader);
60 | } catch (IOException e) {
61 | return "ERROR: " + e.getMessage();
62 | }
63 | }
64 |
65 | @Override
66 | public String get(final String key) throws EtcdException {
67 | Preconditions.checkArgument(!Strings.isNullOrEmpty(key));
68 | return etcd.get(key).getNode().getValue();
69 | }
70 |
71 | @Override
72 | public void set(final String key, final String value) throws EtcdException {
73 | Preconditions.checkArgument(!Strings.isNullOrEmpty(key));
74 | Preconditions.checkArgument(!Strings.isNullOrEmpty(value));
75 | etcd.set(key, value, null, null, null);
76 | }
77 |
78 | @Override
79 | public void set(final String key, final String value, final int ttl)
80 | throws EtcdException {
81 | Preconditions.checkArgument(!Strings.isNullOrEmpty(key));
82 | Preconditions.checkArgument(!Strings.isNullOrEmpty(value));
83 | Preconditions.checkArgument(ttl > 0);
84 | etcd.set(key, value, ttl, null, null);
85 | }
86 |
87 | @Override
88 | public void delete(final String key) throws EtcdException {
89 | Preconditions.checkArgument(!Strings.isNullOrEmpty(key));
90 | etcd.delete(key);
91 | }
92 |
93 | @Override
94 | public Map list(final String path) throws EtcdException {
95 | Preconditions.checkArgument(!Strings.isNullOrEmpty(path));
96 | ImmutableMap.Builder builder = ImmutableMap.builder();
97 | for (EtcdResponse.Node node : etcd.get(path).getNode().getNodes()) {
98 | builder.put(node.getKey(), node.getValue());
99 | }
100 | return builder.build();
101 | }
102 |
103 | @Override
104 | public void compareAndSwap(final String key, final String oldValue,
105 | final String newValue) throws EtcdException {
106 | Preconditions.checkArgument(!Strings.isNullOrEmpty(key));
107 | Preconditions.checkArgument(!Strings.isNullOrEmpty(oldValue));
108 | Preconditions.checkArgument(!Strings.isNullOrEmpty(newValue));
109 | Preconditions.checkArgument(!oldValue.equals(newValue));
110 |
111 | EtcdResponse response = etcd.set(key, newValue, null, null, oldValue);
112 | response.getNode().getValue();
113 | }
114 |
115 | private static final class EtcdErrorHandler implements ErrorHandler {
116 | @Override
117 | public Throwable handleError(final RetrofitError cause) {
118 | if (cause.getResponse() == null) {
119 | return cause;
120 | }
121 | return (EtcdException) cause.getBodyAs(EtcdException.class);
122 | }
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/src/main/java/jetcd/EtcdException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import com.fasterxml.jackson.annotation.JsonProperty;
20 |
21 | /** An Etcd Exception. */
22 | public final class EtcdException extends Exception {
23 | private final int errorCode;
24 | private final String cause;
25 | private final String message;
26 | private final long index;
27 |
28 | public EtcdException(@JsonProperty("errorCode") final int errorCode,
29 | @JsonProperty("cause") final String cause,
30 | @JsonProperty("message") final String message,
31 | @JsonProperty("index") final long index) {
32 | this.errorCode = errorCode;
33 | this.cause = cause;
34 | this.message = message;
35 | this.index = index;
36 | }
37 |
38 | public int getErrorCode() {
39 | return errorCode;
40 | }
41 |
42 | @Override
43 | public String toString() {
44 | return cause;
45 | }
46 |
47 | @Override
48 | public String getMessage() {
49 | return message;
50 | }
51 |
52 | public long getIndex() {
53 | return index;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/jetcd/EtcdResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import java.util.List;
20 |
21 | import com.fasterxml.jackson.annotation.JsonProperty;
22 |
23 | /** An Etcd Response. */
24 | public final class EtcdResponse {
25 | private final String action;
26 | private final Node node;
27 |
28 | EtcdResponse(@JsonProperty("action") final String action,
29 | @JsonProperty("node") final Node node) {
30 | this.action = action;
31 | this.node = node;
32 | }
33 |
34 | public String getAction() {
35 | return action;
36 | }
37 |
38 | public Node getNode() {
39 | return node;
40 | }
41 |
42 | static final class Node {
43 | private final String key;
44 | private final String value;
45 | private final long createdIndex;
46 | private final long modifiedIndex;
47 | private final long ttl;
48 | private final List nodes;
49 |
50 | Node(@JsonProperty("key") final String key,
51 | @JsonProperty("value") final String value,
52 | @JsonProperty("createdIndex") final long createdIndex,
53 | @JsonProperty("modifiedIndex") final long modifiedIndex,
54 | @JsonProperty("ttl") final long ttl,
55 | @JsonProperty("nodes") final List nodes) {
56 | this.key = key;
57 | this.value = value;
58 | this.createdIndex = createdIndex;
59 | this.modifiedIndex = modifiedIndex;
60 | this.ttl = ttl;
61 | this.nodes = nodes;
62 | }
63 |
64 | public String getKey() {
65 | return key;
66 | }
67 |
68 | public long getCreatedIndex() {
69 | return createdIndex;
70 | }
71 |
72 | public long getModifiedIndex() {
73 | return modifiedIndex;
74 | }
75 |
76 | public long getTtl() {
77 | return ttl;
78 | }
79 |
80 | public String getValue() {
81 | return value;
82 | }
83 |
84 | public List getNodes() {
85 | return nodes;
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/test/java/jetcd/EtcdClientTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2013 Diwaker Gupta
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 | package jetcd;
18 |
19 | import static org.assertj.core.api.Assertions.assertThat;
20 | import static org.junit.Assert.fail;
21 |
22 | import org.junit.Before;
23 | import org.junit.Test;
24 |
25 | import retrofit.RetrofitError;
26 |
27 | /** Tests for EtcdClient. */
28 | public final class EtcdClientTest {
29 | private final EtcdClient client = EtcdClientFactory.newInstance();
30 |
31 | @Before
32 | public void setUp() throws EtcdException {
33 | // Setup some keys
34 | try {
35 | client.set("hello", "world");
36 | } catch (Exception e) {
37 | fail();
38 | }
39 | }
40 |
41 | @Test
42 | public void testVersion() throws EtcdException {
43 | assertThat(client.version()).startsWith("etcd");
44 | }
45 |
46 | @Test
47 | public void testGet() throws EtcdException {
48 | client.get("hello");
49 | try {
50 | client.get("non-existent");
51 | fail();
52 | } catch (EtcdException e) {
53 | assertThat(e.getErrorCode()).isEqualTo(100);
54 | }
55 | }
56 |
57 | @Test
58 | public void testFailureWithoutEtcdServer() throws EtcdException {
59 | EtcdClient nonExistingClient = EtcdClientFactory.newInstance(
60 | "http://127.0.0.1:9999");
61 | try {
62 | nonExistingClient.get("hello");
63 | fail();
64 | } catch (RetrofitError e) {
65 | assertThat(e.getResponse()).isNull();
66 | assertThat(e.getKind()).isEqualTo(RetrofitError.Kind.NETWORK);
67 | }
68 | }
69 |
70 | @Test
71 | public void testSet() throws EtcdException {
72 | client.set("newKey", "newValue");
73 | assertThat(client.get("newKey")).isEqualTo("newValue");
74 |
75 | // Set a pre-existing key
76 | client.set("hello", "newValue");
77 | assertThat(client.get("hello")).isEqualTo("newValue");
78 | }
79 |
80 | @Test
81 | public void testSetWithTtl() throws Exception {
82 | client.set("newKey", "newValue", 1);
83 | assertThat(client.get("newKey")).isEqualTo("newValue");
84 |
85 | // Wait for key to expire
86 | Thread.sleep(1500);
87 | try {
88 | client.get("newKey");
89 | fail();
90 | } catch (EtcdException e) {
91 | assertThat(e.getErrorCode()).isEqualTo(100);
92 | }
93 | }
94 |
95 | @Test
96 | public void testDelete() throws EtcdException {
97 | client.delete("hello");
98 | try {
99 | client.get("hello");
100 | fail();
101 | } catch (EtcdException e) {
102 | assertThat(e.getErrorCode()).isEqualTo(100);
103 | }
104 |
105 | try {
106 | client.delete("non-existent");
107 | fail();
108 | } catch (EtcdException e) {
109 | assertThat(e.getErrorCode()).isEqualTo(100);
110 | }
111 | }
112 |
113 | @Test
114 | public void testList() throws EtcdException {
115 | client.set("b/bar", "baz");
116 | client.set("b/foo", "baz");
117 | assertThat(client.list("b")).hasSize(2)
118 | .containsEntry("/b/bar", "baz").containsEntry("/b/foo", "baz");
119 | }
120 |
121 | @Test
122 | public void testCompareAndSwap() throws EtcdException {
123 | client.compareAndSwap("hello", "world", "new value");
124 | assertThat(client.get("hello")).isEqualTo("new value");
125 |
126 | try {
127 | client.compareAndSwap("hello", "bad old", "world");
128 | fail();
129 | } catch (EtcdException e) {
130 | assertThat(e.getErrorCode()).isEqualTo(101);
131 | }
132 | }
133 | }
134 |
--------------------------------------------------------------------------------