選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ConfigSourceTest.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 testGetOptionalColour() {
  40. assertEquals(Color.RED, s.getOptionColour("true", "false:0", Color.RED));
  41. assertEquals(Color.WHITE, s.getOptionColour("true", "true:0", Color.RED));
  42. }
  43. @Test
  44. public void testGetBoolean() {
  45. assertTrue(s.getOptionBool("true", "true"));
  46. assertFalse(s.getOptionBool("true", "false"));
  47. assertTrue(s.getOptionBool("true", "true", false));
  48. assertTrue(s.getOptionBool("false", "true", true));
  49. assertFalse(s.getOptionBool("false", "true", false));
  50. }
  51. @Test
  52. public void testGetInt() {
  53. assertEquals(42, s.getOptionInt("false", "moo", 42));
  54. assertEquals(42, s.getOptionInt("true", "42", 0));
  55. assertEquals(42, s.getOptionInt("true", "moo", 42));
  56. }
  57. @Test
  58. public void testGetList() {
  59. assertTrue(s.getOptionList("false", "moo").isEmpty());
  60. assertTrue(s.getOptionList("true", "").isEmpty());
  61. assertTrue(s.getOptionList("true", "\n\n\n").isEmpty());
  62. assertEquals(4, s.getOptionList("true", "\n\n\na", false).size());
  63. assertEquals(4, s.getOptionList("true", "a\nb\nc\nd", true).size());
  64. assertEquals("c", s.getOptionList("true", "a\nb\nc\nd", true).get(2));
  65. }
  66. private class MySource extends ConfigSource {
  67. @Override
  68. public String getOption(String domain, String option) {
  69. if (Boolean.parseBoolean(domain)) {
  70. return option;
  71. } else {
  72. assertTrue("Requested non-existant option", false);
  73. return null;
  74. }
  75. }
  76. @Override
  77. public boolean hasOption(String domain, String option) {
  78. return Boolean.parseBoolean(domain);
  79. }
  80. }
  81. }