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 8.6KB

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