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    *     http://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.configuration2;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.verifyNoMoreInteractions;
27  import static org.mockito.Mockito.when;
28  
29  import java.util.Collections;
30  
31  import org.apache.commons.configuration2.SynchronizerTestImpl.Methods;
32  import org.apache.commons.configuration2.io.FileHandler;
33  import org.apache.commons.configuration2.sync.LockMode;
34  import org.apache.commons.configuration2.sync.NoOpSynchronizer;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * A test class for the synchronization capabilities of {@code AbstractConfiguration}. This class mainly checks the
40   * collaboration between a configuration object and its {@code Synchronizer}.
41   */
42  public class TestAbstractConfigurationSynchronization {
43      /** Constant for the test property accessed by all tests. */
44      private static final String PROP = "configuration.loaded";
45  
46      /**
47       * Prepares a mock configuration for a copy operation.
48       *
49       * @return the mock configuration
50       */
51      private static Configuration prepareConfigurationMockForCopy() {
52          final Configuration config2 = mock(Configuration.class);
53  
54          when(config2.getKeys()).thenReturn(Collections.<String>emptySet().iterator());
55  
56          return config2;
57      }
58  
59      /**
60       * Verifies a mock configuration after a copy operation.
61       *
62       * @param mock the mock configuration
63       */
64      private static void verifyConfigurationMockAfterCopy(final Configuration mock) {
65          verify(mock).lock(LockMode.READ);
66          verify(mock).getKeys();
67          verify(mock).unlock(LockMode.READ);
68      }
69  
70      /** The synchronizer used for testing. */
71      private SynchronizerTestImpl sync;
72  
73      /** A test configuration. */
74      private AbstractConfiguration config;
75  
76      @BeforeEach
77      public void setUp() throws Exception {
78          // any concrete class will do
79          final PropertiesConfiguration c = new PropertiesConfiguration();
80          new FileHandler(c).load(ConfigurationAssert.getTestFile("test.properties"));
81          sync = new SynchronizerTestImpl();
82          c.setSynchronizer(sync);
83          config = c;
84      }
85  
86      /**
87       * Tests the correct synchronization of addProperty().
88       */
89      @Test
90      public void testAddPropertySynchronized() {
91          config.addProperty(PROP, "of course");
92          sync.verify(Methods.BEGIN_WRITE, Methods.END_WRITE);
93      }
94  
95      /**
96       * Tests whether the append() method uses synchronization.
97       */
98      @Test
99      public void testAppendSynchronized() {
100         final Configuration config2 = prepareConfigurationMockForCopy();
101         config.append(config2);
102 
103         verifyConfigurationMockAfterCopy(config2);
104         verifyNoMoreInteractions(config2);
105     }
106 
107     /**
108      * Tests the correct synchronization of clearProperty().
109      */
110     @Test
111     public void testClearPropertySynchronized() {
112         config.clearProperty(PROP);
113         sync.verify(Methods.BEGIN_WRITE, Methods.END_WRITE);
114     }
115 
116     /**
117      * Tests the correct synchronization of clear().
118      */
119     @Test
120     public void testClearSynchronized() {
121         config.clear();
122         sync.verifyStart(Methods.BEGIN_WRITE);
123         sync.verifyEnd(Methods.END_WRITE);
124     }
125 
126     /**
127      * Tests whether containsKey() is correctly synchronized.
128      */
129     @Test
130     public void testContainsKeySychronized() {
131         assertTrue(config.containsKey(PROP));
132         sync.verify(Methods.BEGIN_READ, Methods.END_READ);
133     }
134 
135     /**
136      * Tests whether the copy() method uses synchronization.
137      */
138     @Test
139     public void testCopySynchronized() {
140         final Configuration config2 = prepareConfigurationMockForCopy();
141         config.copy(config2);
142 
143         verifyConfigurationMockAfterCopy(config2);
144         verifyNoMoreInteractions(config2);
145     }
146 
147     /**
148      * Tests the Synchronizer used by default.
149      */
150     @Test
151     public void testDefaultSynchronizer() {
152         assertSame(NoOpSynchronizer.INSTANCE, new PropertiesConfiguration().getSynchronizer());
153     }
154 
155     /**
156      * Tests whether getKeys(String prefix) is correctly synchronized.
157      */
158     @Test
159     public void testGetKeysPrefixSynchronized() {
160         config.getKeys("test");
161         sync.verify(Methods.BEGIN_READ, Methods.END_READ);
162     }
163 
164     /**
165      * Tests whether getKeys() is correctly synchronized.
166      */
167     @Test
168     public void testGetKeysSynchronized() {
169         assertTrue(config.getKeys().hasNext());
170         sync.verify(Methods.BEGIN_READ, Methods.END_READ);
171     }
172 
173     /**
174      * Tests whether read access to properties is synchronized.
175      */
176     @Test
177     public void testGetPropertySynchronized() {
178         assertEquals("true", config.getProperty(PROP));
179         assertTrue(config.getBoolean(PROP));
180         sync.verify(Methods.BEGIN_READ, Methods.END_READ, Methods.BEGIN_READ, Methods.END_READ);
181     }
182 
183     /**
184      * Tests whether isEmpty() is correctly synchronized.
185      */
186     @Test
187     public void testIsEmptySynchronized() {
188         assertFalse(config.isEmpty());
189         sync.verify(Methods.BEGIN_READ, Methods.END_READ);
190     }
191 
192     /**
193      * Tests lock() with a null argument.
194      */
195     @Test
196     public void testLockNull() {
197         assertThrows(NullPointerException.class, () -> config.lock(null));
198     }
199 
200     /**
201      * Tests whether a read lock can be obtained.
202      */
203     @Test
204     public void testLockRead() {
205         config.lock(LockMode.READ);
206         sync.verify(Methods.BEGIN_READ);
207     }
208 
209     /**
210      * Tests whether a write lock can be obtained.
211      */
212     @Test
213     public void testLockWrite() {
214         config.lock(LockMode.WRITE);
215         sync.verify(Methods.BEGIN_WRITE);
216     }
217 
218     /**
219      * Tests the correct synchronization of setProperty().
220      */
221     @Test
222     public void testSetPropertySynchronized() {
223         config.setProperty(PROP, "yes");
224         sync.verifyStart(Methods.BEGIN_WRITE);
225         sync.verifyEnd(Methods.END_WRITE);
226     }
227 
228     /**
229      * Tests whether size() is correctly synchronized.
230      */
231     @Test
232     public void testSizeSynchronized() {
233         assertFalse(config.isEmpty());
234         sync.verify(Methods.BEGIN_READ, Methods.END_READ);
235     }
236 
237     /**
238      * Tests synchronization of subset().
239      */
240     @Test
241     public void testSubsetSynchronized() {
242         final AbstractConfiguration subset = (AbstractConfiguration) config.subset("configuration");
243         sync.verify();
244         assertEquals(NoOpSynchronizer.INSTANCE, subset.getSynchronizer());
245     }
246 
247     /**
248      * Tests whether a read lock can be released.
249      */
250     @Test
251     public void testUnlockRead() {
252         config.unlock(LockMode.READ);
253         sync.verify(Methods.END_READ);
254     }
255 
256     /**
257      * Tests whether a write lock can be released.
258      */
259     @Test
260     public void testUnlockWrite() {
261         config.unlock(LockMode.WRITE);
262         sync.verify(Methods.END_WRITE);
263     }
264 }