1+ /*
2+ * Copyright 2010-2013, CloudBees Inc.
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+ package com .cloudbees .syslog .util ;
17+
18+ import org .hamcrest .Matchers ;
19+ import org .junit .Assert ;
20+ import org .junit .Test ;
21+
22+ import javax .annotation .Nullable ;
23+ import java .util .concurrent .TimeUnit ;
24+
25+ public class CachingReferenceTest {
26+ /**
27+ * Test that the locks are properly released.
28+ */
29+ @ Test
30+ public void test_return_value () {
31+
32+ CachingReference <String > cachingReference = new CachingReference <String >(5 , TimeUnit .SECONDS ) {
33+ @ Nullable
34+ @ Override
35+ protected String newObject () {
36+ return "value" ;
37+ }
38+ };
39+
40+ String actual = cachingReference .get ();
41+ Assert .assertThat (actual , Matchers .equalTo ("value" ));
42+ }
43+
44+ /**
45+ * Test that the locks are properly released.
46+ */
47+ @ Test (expected = MyRuntimeException .class )
48+ public void test_throw_exception_in_get_object () {
49+
50+ CachingReference <String > cachingReference = new CachingReference <String >(5 , TimeUnit .SECONDS ) {
51+ @ Nullable
52+ @ Override
53+ protected String newObject () {
54+ throw new MyRuntimeException ();
55+ }
56+ };
57+
58+ cachingReference .get ();
59+ }
60+
61+
62+ private static class MyRuntimeException extends RuntimeException {
63+ public MyRuntimeException () {
64+ super ();
65+ }
66+
67+ public MyRuntimeException (Throwable cause ) {
68+ super (cause );
69+ }
70+
71+ public MyRuntimeException (String message ) {
72+ super (message );
73+ }
74+
75+ public MyRuntimeException (String message , Throwable cause ) {
76+ super (message , cause );
77+ }
78+
79+ protected MyRuntimeException (String message , Throwable cause , boolean enableSuppression , boolean writableStackTrace ) {
80+ super (message , cause , enableSuppression , writableStackTrace );
81+ }
82+ }
83+ }
0 commit comments