Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ConfigFileTest.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2006-2014 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 java.io.File;
  24. import java.io.IOException;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.junit.Assert.assertFalse;
  31. import static org.junit.Assert.assertTrue;
  32. public class ConfigFileTest {
  33. private ConfigFile cf;
  34. @Before
  35. public void setUp() throws Exception {
  36. cf = new ConfigFile(getClass().getResourceAsStream("test2.txt"));
  37. }
  38. @Test
  39. public void testRead() throws IOException, InvalidConfigFileException {
  40. cf.read();
  41. }
  42. @Test(expected = UnsupportedOperationException.class)
  43. public void testWrite() throws IOException {
  44. cf.write();
  45. }
  46. @Test
  47. public void testDomains() throws IOException, InvalidConfigFileException {
  48. cf.read();
  49. assertTrue(cf.hasDomain("keysections"));
  50. assertTrue(cf.hasDomain("section alpha"));
  51. assertTrue(cf.hasDomain("section one point one"));
  52. assertTrue(cf.hasDomain("section one"));
  53. assertFalse(cf.hasDomain("random domain"));
  54. }
  55. @Test
  56. public void testKeyDomains() throws IOException, InvalidConfigFileException {
  57. cf.read();
  58. assertTrue(cf.isKeyDomain("section one"));
  59. assertFalse(cf.isKeyDomain("section one point one"));
  60. assertFalse(cf.isKeyDomain("section two"));
  61. }
  62. @Test
  63. public void testFlatDomains() throws IOException, InvalidConfigFileException {
  64. cf.read();
  65. assertTrue(cf.isFlatDomain("keysections"));
  66. assertTrue(cf.isFlatDomain("section alpha"));
  67. assertTrue(cf.isFlatDomain("section one point one"));
  68. assertFalse(cf.isFlatDomain("section one"));
  69. assertFalse(cf.hasDomain("random domain"));
  70. }
  71. @Test
  72. public void testFlatDomainContents() throws IOException, InvalidConfigFileException {
  73. cf.read();
  74. assertEquals(2, cf.getFlatDomain("section alpha").size());
  75. assertEquals("line 1", cf.getFlatDomain("section alpha").get(0));
  76. assertEquals("line 2", cf.getFlatDomain("section alpha").get(1));
  77. }
  78. @Test
  79. public void testKeyDomainContents() throws IOException, InvalidConfigFileException {
  80. cf.read();
  81. assertEquals(3, cf.getKeyDomain("section one").size());
  82. assertEquals("one", cf.getKeyDomain("section one").get("1"));
  83. assertEquals("two", cf.getKeyDomain("section one").get("2"));
  84. assertEquals("three", cf.getKeyDomain("section one").get("3"));
  85. }
  86. @Test
  87. public void testColons() throws IOException, InvalidConfigFileException {
  88. final File file = File.createTempFile("DMDirc.unittest", null);
  89. file.deleteOnExit();
  90. ConfigFile config = new ConfigFile(file);
  91. Map<String, String> data = new HashMap<>();
  92. data.put("test1", "hello");
  93. data.put("test:2", "hello");
  94. data.put("test3", "hello:");
  95. config.addDomain("test", data);
  96. config.write();
  97. config = new ConfigFile(file);
  98. config.read();
  99. assertTrue(config.isKeyDomain("test"));
  100. data = config.getKeyDomain("test");
  101. assertEquals("hello", data.get("test1"));
  102. assertEquals("hello", data.get("test:2"));
  103. assertEquals("hello:", data.get("test3"));
  104. }
  105. @Test
  106. public void testEquals() throws IOException, InvalidConfigFileException {
  107. final File file = File.createTempFile("DMDirc.unittest", null);
  108. file.deleteOnExit();
  109. ConfigFile config = new ConfigFile(file);
  110. Map<String, String> data = new HashMap<>();
  111. data.put("test1", "hello");
  112. data.put("test=2", "hello");
  113. data.put("test3", "hello=");
  114. config.addDomain("test", data);
  115. config.write();
  116. config = new ConfigFile(file);
  117. config.read();
  118. assertTrue(config.isKeyDomain("test"));
  119. data = config.getKeyDomain("test");
  120. assertEquals("hello", data.get("test1"));
  121. assertEquals("hello", data.get("test=2"));
  122. assertEquals("hello=", data.get("test3"));
  123. }
  124. @Test
  125. public void testNewlines() throws IOException, InvalidConfigFileException {
  126. final File file = File.createTempFile("DMDirc.unittest", null);
  127. file.deleteOnExit();
  128. ConfigFile config = new ConfigFile(file);
  129. Map<String, String> data = new HashMap<>();
  130. data.put("test1", "hello");
  131. data.put("test2", "hello\ngoodbye");
  132. data.put("test3", "hello\n");
  133. data.put("test4", "hello\r\ngoodbye");
  134. config.addDomain("test", data);
  135. config.write();
  136. config = new ConfigFile(file);
  137. config.read();
  138. assertTrue(config.isKeyDomain("test"));
  139. data = config.getKeyDomain("test");
  140. assertEquals("hello", data.get("test1"));
  141. assertEquals("hello\ngoodbye", data.get("test2"));
  142. assertEquals("hello\n", data.get("test3"));
  143. assertEquals("hello\r\ngoodbye", data.get("test4"));
  144. }
  145. @Test
  146. public void testBackslash() throws IOException, InvalidConfigFileException {
  147. final File file = File.createTempFile("DMDirc.unittest", null);
  148. file.deleteOnExit();
  149. ConfigFile config = new ConfigFile(file);
  150. Map<String, String> data = new HashMap<>();
  151. data.put("test1", "hello\\");
  152. data.put("test2", "\\nhello");
  153. data.put("test3\\", "hello");
  154. config.addDomain("test", data);
  155. config.write();
  156. config = new ConfigFile(file);
  157. config.read();
  158. assertTrue(config.isKeyDomain("test"));
  159. data = config.getKeyDomain("test");
  160. assertEquals("hello\\", data.get("test1"));
  161. assertEquals("\\nhello", data.get("test2"));
  162. assertEquals("hello", data.get("test3\\"));
  163. }
  164. @Test
  165. public void testHash() throws IOException, InvalidConfigFileException {
  166. final File file = File.createTempFile("DMDirc.unittest", null);
  167. file.deleteOnExit();
  168. ConfigFile config = new ConfigFile(file);
  169. Map<String, String> data = new HashMap<>();
  170. data.put("test1#", "hello");
  171. data.put("#test2", "hello");
  172. data.put("test3", "#hello");
  173. config.addDomain("test", data);
  174. config.write();
  175. config = new ConfigFile(file);
  176. config.read();
  177. assertTrue(config.isKeyDomain("test"));
  178. data = config.getKeyDomain("test");
  179. assertEquals("hello", data.get("test1#"));
  180. assertEquals("hello", data.get("#test2"));
  181. assertEquals("#hello", data.get("test3"));
  182. }
  183. @Test
  184. public void testEscape() {
  185. final String input = "blah blah\\foo\r\nbar=:";
  186. final String output = "blah blah\\\\foo\\r\\nbar\\=\\:";
  187. assertEquals(output, ConfigFile.escape(input));
  188. }
  189. @Test
  190. public void testUnescape() {
  191. final String input = "blah blah\\foo\r\nbar=:";
  192. assertEquals(input, ConfigFile.unescape(ConfigFile.escape(input)));
  193. }
  194. @Test
  195. public void testDelete() throws IOException {
  196. final File file = File.createTempFile("DMDirc_unittest", null);
  197. file.deleteOnExit();
  198. ConfigFile config = new ConfigFile(file);
  199. config.write();
  200. assertTrue(file.exists());
  201. config.delete();
  202. assertFalse(file.exists());
  203. }
  204. @Test
  205. public void testDuplicateKeys() throws IOException, InvalidConfigFileException {
  206. final ConfigFile file = new ConfigFile(getClass().getResourceAsStream("test2.txt"));
  207. file.read();
  208. assertTrue(file.isKeyDomain("section one"));
  209. assertEquals(3, file.getKeyDomain("section one").size());
  210. assertTrue(file.isFlatDomain("section one point one"));
  211. }
  212. @Test(expected = InvalidConfigFileException.class)
  213. public void testInvalidLine() throws IOException, InvalidConfigFileException {
  214. final ConfigFile file = new ConfigFile(getClass().getResourceAsStream("test1.txt"));
  215. file.read();
  216. }
  217. }