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.juneau.config.store;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.util.concurrent.*;
22  
23  import org.apache.juneau.*;
24  import org.junit.jupiter.api.*;
25  
26  class ConfigMemoryStore_Test extends TestBase {
27  
28  	@Test void a01_noFile() {
29  		var fs = MemoryStore.create().build();
30  		assertEquals("", fs.read("X"));
31  	}
32  
33  	@Test void a02_simpleCreate() {
34  		var fs = MemoryStore.create().build();
35  		assertNull(fs.write("X", null, "foo"));
36  		assertEquals("foo", fs.read("X"));
37  	}
38  
39  	@Test void a03_failOnMismatch() {
40  		var fs = MemoryStore.create().build();
41  		assertNotNull(fs.write("X", "xxx", "foo"));
42  		assertEquals("", fs.read("X"));
43  		assertNull(fs.write("X", null, "foo"));
44  		assertEquals("foo", fs.read("X"));
45  		assertNotNull(fs.write("X", "xxx", "foo"));
46  		assertEquals("foo", fs.read("X"));
47  		assertNull(fs.write("X", "foo", "bar"));
48  		assertEquals("bar", fs.read("X"));
49  	}
50  
51  	@Test void a04_update() throws Exception {
52  		var fs = MemoryStore.create().build();
53  
54  		final var latch = new CountDownLatch(2);
55  		fs.register("X", contents -> {
56  			if ("xxx".equals(contents))
57  				latch.countDown();
58  		});
59  		fs.register("Y", contents -> {
60  			if ("yyy".equals(contents))
61  				latch.countDown();
62  		});
63  
64  		fs.update("X", "xxx");
65  		assertDoesNotThrow(()->fs.update("Y", "yyy"));
66  		if (! latch.await(10, TimeUnit.SECONDS))
67  			throw new Exception("CountDownLatch never reached zero.");
68  	}
69  
70  	@Test void a05_exists() {
71  		MemoryStore.DEFAULT.write("foo", null, "foo");
72  
73  		assertTrue(MemoryStore.DEFAULT.exists("foo"));
74  		assertFalse(MemoryStore.DEFAULT.exists("foo2"));
75  
76  		MemoryStore.DEFAULT.write("foo", "foo", null);
77  
78  		assertFalse(MemoryStore.DEFAULT.exists("foo"));
79  		assertFalse(MemoryStore.DEFAULT.exists("foo2"));
80  	}
81  }