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.apache.juneau.commons.utils.FileUtils.*;
21  import static org.apache.juneau.commons.utils.IoUtils.*;
22  import static org.junit.jupiter.api.Assertions.*;
23  
24  import java.io.*;
25  import java.util.concurrent.*;
26  
27  import org.apache.juneau.*;
28  import org.junit.jupiter.api.*;
29  
30  class ConfigFileStoreTest extends TestBase {
31  
32  	private static final File DIR = new File("./target/config");
33  
34  	@AfterEach void cleanUp() {
35  		deleteFile(DIR);
36  	}
37  
38  	@Test void a01_noFile() throws Exception {
39  		var fs = FileStore.create().directory(DIR).build();
40  		assertEquals("", fs.read("X.cfg"));
41  		assertFileNotExists("X.cfg");
42  	}
43  
44  	@Test void a02_differentExtension() throws Exception {
45  		var fs = FileStore.create().directory(DIR).build();
46  		assertEquals("", fs.read("X.xxx"));
47  		assertFileNotExists("X.xxx");
48  	}
49  
50  	@Test void a03_simpleCreateAndDelete() throws Exception {
51  		var fs = FileStore.create().directory(DIR).build();
52  		assertNull(fs.write("X.cfg", null, "foo"));
53  		assertEquals("foo", fs.read("X.cfg"));
54  		assertFileExists("X.cfg");
55  		fs.write("X.cfg", "foo", null);
56  		assertFileNotExists("X.cfg");
57  	}
58  
59  	@Test void a04_simpleCreateAndDeleteWithNoExtension() throws Exception {
60  		var fs = FileStore.create().directory(DIR).build();
61  		assertNull(fs.write("X", null, "foo"));
62  		assertEquals("foo", fs.read("X"));
63  		assertFileExists("X.cfg");
64  		fs.write("X", "foo", null);
65  		assertFileNotExists("X.cfg");
66  	}
67  
68  	@Test void a05_simpleCreateAndDeleteWithNonStandardExtension() throws Exception {
69  		var fs = FileStore.create().directory(DIR).extensions("xxx").build();
70  		assertNull(fs.write("X", null, "foo"));
71  		assertEquals("foo", fs.read("X"));
72  		assertFileExists("X.xxx");
73  		fs.write("X", "foo", null);
74  		assertFileNotExists("X.xxx");
75  	}
76  
77  	@Test void a06_simpleCreateAndDeleteWithMultipleSpecialExtension() throws Exception {
78  		var fs = FileStore.create().directory(DIR).extensions("foo1,foo2").build();
79  		assertNull(fs.write("X", null, "foo"));
80  		assertEquals("foo", fs.read("X"));
81  		assertFileExists("X.foo1");
82  		fs.write("X", "foo", null);
83  		assertFileNotExists("X.foo1");
84  	}
85  
86  	@Test void a07_failOnMismatch() throws Exception {
87  		assertFileNotExists("X.cfg");
88  		var fs = FileStore.create().directory(DIR).build();
89  		assertNotNull(fs.write("X.cfg", "xxx", "foo"));
90  		assertFileNotExists("X.cfg");
91  		assertEquals("", fs.read("X.cfg"));
92  		assertFileNotExists("X.cfg");
93  		assertNull(fs.write("X.cfg", null, "foo"));
94  		assertEquals("foo", fs.read("X.cfg"));
95  		assertNotNull(fs.write("X.cfg", "xxx", "foo"));
96  		assertEquals("foo", fs.read("X.cfg"));
97  		assertNull(fs.write("X.cfg", "foo", "bar"));
98  		assertEquals("bar", fs.read("X.cfg"));
99  	}
100 
101 	@Test void a08_failOnMismatchNoExtension() throws Exception {
102 		assertFileNotExists("X.cfg");
103 		var fs = FileStore.create().directory(DIR).build();
104 		assertNotNull(fs.write("X", "xxx", "foo"));
105 		assertFileNotExists("X.cfg");
106 		assertEquals("", fs.read("X"));
107 		assertEquals("", fs.read("X.cfg"));
108 		assertFileNotExists("X.cfg");
109 		assertNull(fs.write("X", null, "foo"));
110 		assertEquals("foo", fs.read("X"));
111 		assertEquals("foo", fs.read("X.cfg"));
112 		assertNotNull(fs.write("X", "xxx", "foo"));
113 		assertEquals("foo", fs.read("X"));
114 		assertEquals("foo", fs.read("X.cfg"));
115 		assertNull(fs.write("X", "foo", "bar"));
116 		assertEquals("bar", fs.read("X"));
117 		assertEquals("bar", fs.read("X.cfg"));
118 	}
119 
120 	@Test void a09_charset() throws Exception {
121 		var fs = FileStore.create().directory(DIR).charset(UTF8).build();
122 		assertNull(fs.write("X.cfg", null, "foo"));
123 		assertEquals("foo", fs.read("X.cfg"));
124 		assertEquals("foo", fs.read("X"));
125 	}
126 
127 	@Test void a10_charsetNoExtension() throws Exception {
128 		var fs = FileStore.create().directory(DIR).charset(UTF8).build();
129 		assertNull(fs.write("X", null, "foo"));
130 		assertEquals("foo", fs.read("X"));
131 		assertEquals("foo", fs.read("X.cfg"));
132 	}
133 
134 	@Test void a11_watcher_LONGRUNNING() throws Exception {
135 		var fs = FileStore.create().directory(DIR).enableWatcher().watcherSensitivity(WatcherSensitivity.HIGH).build();
136 
137 		final var latch = new CountDownLatch(4);
138 		fs.register("X.cfg", contents -> {
139 			if ("xxx".equals(contents))
140 				latch.countDown();
141 		});
142 		fs.register("X", contents -> {
143 			if ("xxx".equals(contents))
144 				latch.countDown();
145 		});
146 		fs.register("Y.cfg", contents -> {
147 			if ("yyy".equals(contents))
148 				latch.countDown();
149 		});
150 		fs.register("Y", contents -> {
151 			if ("yyy".equals(contents))
152 				latch.countDown();
153 		});
154 		pipe(reader("zzz"), new File(DIR, "Z.ini"));
155 		pipe(reader("xxx"), new File(DIR, "X.cfg"));
156 		assertDoesNotThrow(()->pipe(reader("yyy"), new File(DIR, "Y.cfg")));
157 		if (! latch.await(10, TimeUnit.SECONDS))
158 			throw new Exception("CountDownLatch never reached zero.");
159 	}
160 
161 	@Test void a12_update() throws Exception {
162 		var fs = FileStore.create().directory(DIR).build();
163 
164 		final var latch = new CountDownLatch(4);
165 		fs.register("X.cfg", contents -> {
166 			if ("xxx".equals(contents))
167 				latch.countDown();
168 		});
169 		fs.register("X", contents -> {
170 			if ("xxx".equals(contents))
171 				latch.countDown();
172 		});
173 		fs.register("Y.cfg", contents -> {
174 			if ("yyy".equals(contents))
175 				latch.countDown();
176 		});
177 		fs.register("Y", contents -> {
178 			if ("yyy".equals(contents))
179 				latch.countDown();
180 		});
181 
182 		fs.update("X.cfg", "xxx");
183 		assertDoesNotThrow(()->fs.update("Y.cfg", "yyy"));
184 		if (! latch.await(10, TimeUnit.SECONDS))
185 			throw new Exception("CountDownLatch never reached zero.");
186 	}
187 
188 	@Test void a13_exists() throws IOException {
189 		var cs = FileStore.DEFAULT;
190 		assertTrue(cs.exists("test.cfg"));
191 		assertTrue(cs.exists("test"));
192 		assertFalse(cs.exists("test2.cfg"));
193 
194 		assertFalse(cs.exists("foo.cfg"));
195 		cs.write("foo.cfg", null, "foo");
196 		assertTrue(cs.exists("foo.cfg"));
197 		assertTrue(cs.exists("foo"));
198 		cs.write("foo.cfg", "foo", null);
199 		assertFalse(cs.exists("foo.cfg"));
200 		assertFalse(cs.exists("foo"));
201 
202 		pipe(reader("xxx"), new File("Foox.cfg"));
203 		assertTrue(cs.exists("Foox.cfg"));
204 		assertTrue(cs.exists("Foox"));
205 		new File("Foox.cfg").delete();
206 		assertFalse(cs.exists("Foox.cfg"));
207 		assertFalse(cs.exists("Foox"));
208 	}
209 
210 	private static void assertFileExists(String name) {
211 		assertTrue(new File(DIR, name).exists());
212 	}
213 
214 	private static void assertFileNotExists(String name) {
215 		assertTrue(! new File(DIR, name).exists());
216 	}
217 }