您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConfigFileTest.java 8.7KB

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