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