You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigFileTest.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.io;
  23. import com.google.common.jimfs.Configuration;
  24. import com.google.common.jimfs.Jimfs;
  25. import java.io.IOException;
  26. import java.nio.file.AccessDeniedException;
  27. import java.nio.file.AccessMode;
  28. import java.nio.file.FileSystem;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.nio.file.spi.FileSystemProvider;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34. import org.junit.After;
  35. import org.junit.Before;
  36. import org.junit.Test;
  37. import org.junit.runner.RunWith;
  38. import org.mockito.Mock;
  39. import org.mockito.runners.MockitoJUnitRunner;
  40. import static org.junit.Assert.assertEquals;
  41. import static org.junit.Assert.assertFalse;
  42. import static org.junit.Assert.assertTrue;
  43. import static org.mockito.Mockito.doThrow;
  44. import static org.mockito.Mockito.when;
  45. @RunWith(MockitoJUnitRunner.class)
  46. public class ConfigFileTest {
  47. @Mock private Path ro;
  48. @Mock private FileSystem mockedFileSystem;
  49. @Mock private FileSystemProvider fileSystemProvider;
  50. private ConfigFile cf;
  51. private Path temp;
  52. private FileSystem fileSystem;
  53. @Before
  54. public void setUp() throws Exception {
  55. fileSystem = Jimfs.newFileSystem(Configuration.unix());
  56. Files.copy(getClass().getResourceAsStream("test2.txt"), fileSystem.getPath("/test2.txt"));
  57. cf = new ConfigFile(fileSystem.getPath("/test2.txt"));
  58. temp = fileSystem.getPath("/temp.txt");
  59. when(mockedFileSystem.provider()).thenReturn(fileSystemProvider);
  60. when(ro.getFileSystem()).thenReturn(mockedFileSystem);
  61. doThrow(new AccessDeniedException("Nup.")).when(fileSystemProvider).checkAccess(ro, AccessMode.WRITE);
  62. }
  63. @After
  64. public void tearDown() throws Exception {
  65. fileSystem.close();
  66. }
  67. @Test
  68. public void testRead() throws IOException, InvalidConfigFileException {
  69. cf.read();
  70. }
  71. @Test(expected = UnsupportedOperationException.class)
  72. public void testWrite() throws IOException {
  73. new ConfigFile(ro).write();
  74. }
  75. @Test
  76. public void testDomains() throws IOException, InvalidConfigFileException {
  77. cf.read();
  78. assertTrue(cf.hasDomain("keysections"));
  79. assertTrue(cf.hasDomain("section alpha"));
  80. assertTrue(cf.hasDomain("section one point one"));
  81. assertTrue(cf.hasDomain("section one"));
  82. assertFalse(cf.hasDomain("random domain"));
  83. }
  84. @Test
  85. public void testKeyDomains() throws IOException, InvalidConfigFileException {
  86. cf.read();
  87. assertTrue(cf.isKeyDomain("section one"));
  88. assertFalse(cf.isKeyDomain("section one point one"));
  89. assertFalse(cf.isKeyDomain("section two"));
  90. }
  91. @Test
  92. public void testFlatDomains() throws IOException, InvalidConfigFileException {
  93. cf.read();
  94. assertTrue(cf.isFlatDomain("keysections"));
  95. assertTrue(cf.isFlatDomain("section alpha"));
  96. assertTrue(cf.isFlatDomain("section one point one"));
  97. assertFalse(cf.isFlatDomain("section one"));
  98. assertFalse(cf.hasDomain("random domain"));
  99. }
  100. @Test
  101. public void testFlatDomainContents() throws IOException, InvalidConfigFileException {
  102. cf.read();
  103. assertEquals(2, cf.getFlatDomain("section alpha").size());
  104. assertEquals("line 1", cf.getFlatDomain("section alpha").get(0));
  105. assertEquals("line 2", cf.getFlatDomain("section alpha").get(1));
  106. }
  107. @Test
  108. public void testKeyDomainContents() throws IOException, InvalidConfigFileException {
  109. cf.read();
  110. assertEquals(3, cf.getKeyDomain("section one").size());
  111. assertEquals("one", cf.getKeyDomain("section one").get("1"));
  112. assertEquals("two", cf.getKeyDomain("section one").get("2"));
  113. assertEquals("three", cf.getKeyDomain("section one").get("3"));
  114. }
  115. @Test
  116. public void testColons() throws IOException, InvalidConfigFileException {
  117. final ConfigFile config = new ConfigFile(temp);
  118. final Map<String, String> data = new HashMap<>();
  119. data.put("test1", "hello");
  120. data.put("test:2", "hello");
  121. data.put("test3", "hello:");
  122. config.addDomain("test", data);
  123. config.write();
  124. final ConfigFile config2 = new ConfigFile(temp);
  125. config2.read();
  126. assertTrue(config2.isKeyDomain("test"));
  127. final Map<String, String> test = config2.getKeyDomain("test");
  128. assertEquals("hello", test.get("test1"));
  129. assertEquals("hello", test.get("test:2"));
  130. assertEquals("hello:", test.get("test3"));
  131. }
  132. @Test
  133. public void testEquals() throws IOException, InvalidConfigFileException {
  134. final ConfigFile config = new ConfigFile(temp);
  135. final Map<String, String> data = new HashMap<>();
  136. data.put("test1", "hello");
  137. data.put("test=2", "hello");
  138. data.put("test3", "hello=");
  139. config.addDomain("test", data);
  140. config.write();
  141. final ConfigFile config2 = new ConfigFile(temp);
  142. config2.read();
  143. assertTrue(config2.isKeyDomain("test"));
  144. final Map<String, String> test = config2.getKeyDomain("test");
  145. assertEquals("hello", test.get("test1"));
  146. assertEquals("hello", test.get("test=2"));
  147. assertEquals("hello=", test.get("test3"));
  148. }
  149. @Test
  150. public void testNewlines() throws IOException, InvalidConfigFileException {
  151. final ConfigFile config = new ConfigFile(temp);
  152. final Map<String, String> data = new HashMap<>();
  153. data.put("test1", "hello");
  154. data.put("test2", "hello\ngoodbye");
  155. data.put("test3", "hello\n");
  156. data.put("test4", "hello\r\ngoodbye");
  157. config.addDomain("test", data);
  158. config.write();
  159. final ConfigFile config2 = new ConfigFile(temp);
  160. config2.read();
  161. assertTrue(config2.isKeyDomain("test"));
  162. final Map<String, String> test = config2.getKeyDomain("test");
  163. assertEquals("hello", test.get("test1"));
  164. assertEquals("hello\ngoodbye", test.get("test2"));
  165. assertEquals("hello\n", test.get("test3"));
  166. assertEquals("hello\r\ngoodbye", test.get("test4"));
  167. }
  168. @Test
  169. public void testBackslash() throws IOException, InvalidConfigFileException {
  170. final ConfigFile config = new ConfigFile(temp);
  171. final Map<String, String> data = new HashMap<>();
  172. data.put("test1", "hello\\");
  173. data.put("test2", "\\nhello");
  174. data.put("test3\\", "hello");
  175. config.addDomain("test", data);
  176. config.write();
  177. final ConfigFile config2 = new ConfigFile(temp);
  178. config2.read();
  179. assertTrue(config2.isKeyDomain("test"));
  180. final Map<String, String> test = config2.getKeyDomain("test");
  181. assertEquals("hello\\", test.get("test1"));
  182. assertEquals("\\nhello", test.get("test2"));
  183. assertEquals("hello", test.get("test3\\"));
  184. }
  185. @Test
  186. public void testHash() throws IOException, InvalidConfigFileException {
  187. final ConfigFile config = new ConfigFile(temp);
  188. final Map<String, String> data = new HashMap<>();
  189. data.put("test1#", "hello");
  190. data.put("#test2", "hello");
  191. data.put("test3", "#hello");
  192. config.addDomain("test", data);
  193. config.write();
  194. final ConfigFile config2 = new ConfigFile(temp);
  195. config2.read();
  196. assertTrue(config2.isKeyDomain("test"));
  197. final Map<String, String> test = config2.getKeyDomain("test");
  198. assertEquals("hello", test.get("test1#"));
  199. assertEquals("hello", test.get("#test2"));
  200. assertEquals("#hello", test.get("test3"));
  201. }
  202. @Test
  203. public void testEscape() {
  204. final String input = "blah blah\\foo\r\nbar=:";
  205. final String output = "blah blah\\\\foo\\r\\nbar\\=\\:";
  206. assertEquals(output, ConfigFile.escape(input));
  207. }
  208. @Test
  209. public void testUnescape() {
  210. final String input = "blah blah\\foo\r\nbar=:";
  211. assertEquals(input, ConfigFile.unescape(ConfigFile.escape(input)));
  212. }
  213. @Test
  214. public void testDelete() throws IOException {
  215. final ConfigFile config = new ConfigFile(temp);
  216. config.write();
  217. assertTrue(Files.exists(temp));
  218. config.delete();
  219. assertFalse(Files.exists(temp));
  220. }
  221. @Test
  222. public void testDuplicateKeys() throws IOException, InvalidConfigFileException {
  223. final ConfigFile file = new ConfigFile(getClass().getResourceAsStream("test2.txt"));
  224. file.read();
  225. assertTrue(file.isKeyDomain("section one"));
  226. assertEquals(3, file.getKeyDomain("section one").size());
  227. assertTrue(file.isFlatDomain("section one point one"));
  228. }
  229. @Test(expected = InvalidConfigFileException.class)
  230. public void testInvalidLine() throws IOException, InvalidConfigFileException {
  231. final ConfigFile file = new ConfigFile(getClass().getResourceAsStream("test1.txt"));
  232. file.read();
  233. }
  234. }