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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2006-2008 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.FileNotFoundException;
  24. import java.io.IOException;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. public class ConfigFileTest extends junit.framework.TestCase {
  29. private ConfigFile cf;
  30. @Before
  31. public void setUp() throws Exception {
  32. cf = new ConfigFile(getClass().getClassLoader().
  33. getResource("com/dmdirc/util/test2.txt").toURI());
  34. }
  35. @Test
  36. public void testRead() {
  37. boolean err = false;
  38. try {
  39. cf.read();
  40. } catch (FileNotFoundException ex) {
  41. err = true;
  42. } catch (IOException ex) {
  43. err = true;
  44. } catch (InvalidConfigFileException ex) {
  45. err = true;
  46. }
  47. assertFalse(err);
  48. }
  49. @Test
  50. public void testDomains() {
  51. testRead();
  52. assertTrue(cf.hasDomain("keysections"));
  53. assertTrue(cf.hasDomain("section alpha"));
  54. assertTrue(cf.hasDomain("section one point one"));
  55. assertTrue(cf.hasDomain("section one"));
  56. assertFalse(cf.hasDomain("random domain"));
  57. }
  58. @Test
  59. public void testKeyDomains() {
  60. testRead();
  61. assertTrue(cf.isKeyDomain("section one"));
  62. assertFalse(cf.isKeyDomain("section one point one"));
  63. assertFalse(cf.isKeyDomain("section two"));
  64. }
  65. @Test
  66. public void testFlatDomains() {
  67. testRead();
  68. assertTrue(cf.isFlatDomain("keysections"));
  69. assertTrue(cf.isFlatDomain("section alpha"));
  70. assertTrue(cf.isFlatDomain("section one point one"));
  71. assertFalse(cf.isFlatDomain("section one"));
  72. assertFalse(cf.hasDomain("random domain"));
  73. }
  74. @Test
  75. public void testFlatDomainContents() {
  76. testRead();
  77. assertEquals(2, cf.getFlatDomain("section alpha").size());
  78. assertEquals("line 1", cf.getFlatDomain("section alpha").get(0));
  79. assertEquals("line 2", cf.getFlatDomain("section alpha").get(1));
  80. }
  81. @Test
  82. public void testKeyDomainContents() {
  83. testRead();
  84. assertEquals(3, cf.getKeyDomain("section one").size());
  85. assertEquals("one", cf.getKeyDomain("section one").get("1"));
  86. assertEquals("two", cf.getKeyDomain("section one").get("2"));
  87. assertEquals("three", cf.getKeyDomain("section one").get("3"));
  88. }
  89. }