View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.pool2.impl;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.lang.ref.SoftReference;
22  
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for PooledSoftReference.
28   */
29  class TestPooledSoftReference {
30  
31      private static final String REFERENT = "test";
32      private static final String REFERENT2 = "test2";
33      PooledSoftReference<String> ref;
34  
35      @BeforeEach
36      public void setUp() {
37          final SoftReference<String> softRef = new SoftReference<>(REFERENT);
38          ref = new PooledSoftReference<>(softRef);
39      }
40  
41      @Test
42      void testPooledSoftReference() {
43          assertEquals(REFERENT, ref.getObject());
44  
45          SoftReference<String> softRef = ref.getReference();
46          assertEquals(REFERENT, softRef.get());
47          softRef.clear();
48  
49          softRef = new SoftReference<>(REFERENT2);
50          ref.setReference(softRef);
51  
52          assertEquals(REFERENT2, ref.getObject());
53  
54          softRef = ref.getReference();
55          assertEquals(REFERENT2, softRef.get());
56          softRef.clear();
57      }
58  
59      @Test
60      void testToString() {
61          final String expected = "Referenced Object: test, State: IDLE";
62          assertEquals(expected, ref.toString());
63      }
64  
65  }