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.8KB

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