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.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.util.concurrent.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class ConfigClasspathStore_Test extends TestBase {
28  
29  	@Test void a01_noFile() throws Exception {
30  		var fs = ClasspathStore.create().build();
31  		assertEquals("", fs.read("X.cfg"));
32  	}
33  
34  	@Test void a02_realFiles() throws Exception {
35  		var fs = ClasspathStore.create().build();
36  		assertContains("bar1", fs.read("foo1.cfg"));
37  		assertContains("bar2", fs.read("sub/foo2.cfg"));
38  		assertEquals("", fs.read("sub/bad.cfg"));
39  		assertEquals("", fs.read("bad/bad.cfg"));
40  	}
41  
42  	@Test void a03_overwriteRealFiles() throws Exception {
43  		var fs = ClasspathStore.create().build();
44  		assertContains("bar1", fs.read("foo1.cfg"));
45  		fs.write("foo1.cfg", fs.read("foo1.cfg"), "xxx");
46  		assertEquals("xxx", fs.read("foo1.cfg"));
47  	}
48  
49  	@Test void a04_simpleCreate() throws Exception {
50  		var fs = ClasspathStore.create().build();
51  		assertNull(fs.write("X.cfg", null, "foo"));
52  		assertEquals("foo", fs.read("X.cfg"));
53  	}
54  
55  	@Test void a05_failOnMismatch() throws Exception {
56  		var fs = ClasspathStore.create().build();
57  		assertNotNull(fs.write("X.cfg", "xxx", "foo"));
58  		assertEquals("", fs.read("X.cfg"));
59  		assertNull(fs.write("X.cfg", null, "foo"));
60  		assertEquals("foo", fs.read("X.cfg"));
61  		assertNotNull(fs.write("X.cfg", "xxx", "foo"));
62  		assertEquals("foo", fs.read("X.cfg"));
63  		assertNull(fs.write("X.cfg", "foo", "bar"));
64  		assertEquals("bar", fs.read("X.cfg"));
65  	}
66  
67  	@Test void a06_update() throws Exception {
68  		var fs = ClasspathStore.create().build();
69  
70  		final var latch = new CountDownLatch(2);
71  		fs.register("X.cfg", contents -> {
72  			if ("xxx".equals(contents))
73  				latch.countDown();
74  		});
75  		fs.register("Y.cfg", contents -> {
76  			if ("yyy".equals(contents))
77  				latch.countDown();
78  		});
79  
80  		fs.update("X.cfg", "xxx");
81  		assertDoesNotThrow(()->fs.update("Y.cfg", "yyy"));
82  		if (! latch.await(10, TimeUnit.SECONDS))
83  			throw new Exception("CountDownLatch never reached zero.");
84  	}
85  
86  	@Test void a07_exists() throws Exception {
87  		ClasspathStore.DEFAULT.write("foo.cfg", null, "foo");
88  
89  		assertTrue(ClasspathStore.DEFAULT.exists("foo.cfg"));
90  		assertFalse(ClasspathStore.DEFAULT.exists("foo2.cfg"));
91  
92  		ClasspathStore.DEFAULT.write("foo.cfg", "foo", null);
93  
94  		assertFalse(ClasspathStore.DEFAULT.exists("foo.cfg"));
95  		assertFalse(ClasspathStore.DEFAULT.exists("foo2.cfg"));
96  	}
97  }