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.

ConfigSourceTest.java 3.8KB

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.config;
  23. import java.awt.Color;
  24. import org.junit.Test;
  25. import static org.junit.Assert.*;
  26. public class ConfigSourceTest extends junit.framework.TestCase {
  27. private final MySource s = new MySource();
  28. @Test
  29. public void testGetOption() {
  30. assertEquals("moo", s.getOption("false", "bar", "moo"));
  31. assertEquals("bar", s.getOption("true", "bar", "moo"));
  32. }
  33. @Test
  34. public void testGetColour() {
  35. assertEquals(Color.RED, s.getOptionColour("false", "moo", Color.RED));
  36. assertEquals(Color.WHITE, s.getOptionColour("true", "0", Color.RED));
  37. }
  38. @Test
  39. public void testGetChar() {
  40. assertEquals('c', s.getOptionChar("true", "c", 'd'));
  41. assertEquals('c', s.getOptionChar("true", "coo", 'd'));
  42. assertEquals('d', s.getOptionChar("false", "c", 'd'));
  43. assertEquals('d', s.getOptionChar("true", "", 'd'));
  44. }
  45. @Test
  46. public void testGetOptionalColour() {
  47. assertEquals(Color.RED, s.getOptionColour("true", "false:0", Color.RED));
  48. assertEquals(Color.WHITE, s.getOptionColour("true", "true:0", Color.RED));
  49. }
  50. @Test
  51. public void testGetBoolean() {
  52. assertTrue(s.getOptionBool("true", "true"));
  53. assertFalse(s.getOptionBool("true", "false"));
  54. assertTrue(s.getOptionBool("true", "true", false));
  55. assertTrue(s.getOptionBool("false", "true", true));
  56. assertFalse(s.getOptionBool("false", "true", false));
  57. }
  58. @Test
  59. public void testGetInt() {
  60. assertEquals(42, s.getOptionInt("false", "moo", 42));
  61. assertEquals(42, s.getOptionInt("true", "42", 0));
  62. assertEquals(42, s.getOptionInt("true", "moo", 42));
  63. }
  64. @Test
  65. public void testGetList() {
  66. assertTrue(s.getOptionList("false", "moo").isEmpty());
  67. assertTrue(s.getOptionList("true", "").isEmpty());
  68. assertTrue(s.getOptionList("true", "\n\n\n").isEmpty());
  69. assertEquals(4, s.getOptionList("true", "\n\n\na", false).size());
  70. assertEquals(4, s.getOptionList("true", "a\nb\nc\nd", true).size());
  71. assertEquals("c", s.getOptionList("true", "a\nb\nc\nd", true).get(2));
  72. }
  73. private class MySource extends ConfigSource {
  74. @Override
  75. public String getOption(String domain, String option) {
  76. if (Boolean.parseBoolean(domain)) {
  77. return option;
  78. } else {
  79. assertTrue("Requested non-existant option", false);
  80. return null;
  81. }
  82. }
  83. @Override
  84. public boolean hasOption(String domain, String option) {
  85. return Boolean.parseBoolean(domain);
  86. }
  87. }
  88. }