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.

IdentityTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 com.dmdirc.interfaces.ConfigChangeListener;
  24. import java.io.IOException;
  25. import java.util.Properties;
  26. import org.junit.After;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.*;
  30. public class IdentityTest {
  31. private Identity myIdent;
  32. private ConfigTarget target;
  33. @Before
  34. public void setUp() throws Exception {
  35. target = new ConfigTarget();
  36. target.setChannel("#unittest@unittest");
  37. myIdent = Identity.buildIdentity(target);
  38. }
  39. @After
  40. public void tearDown() throws Exception {
  41. myIdent = null;
  42. }
  43. @Test
  44. public void testGetProperties() {
  45. myIdent.setOption("domain", "option", "value");
  46. final Properties props = myIdent.getProperties();
  47. assertEquals(props.getProperty("domain.option"), "value");
  48. myIdent.unsetOption("domain", "option");
  49. }
  50. @Test
  51. public void testGetName() {
  52. final Properties props = myIdent.getProperties();
  53. assertEquals(props.getProperty("identity.name"), myIdent.getName());
  54. }
  55. @Test
  56. public void testToString() {
  57. assertEquals(myIdent.getName(), myIdent.toString());
  58. }
  59. @Test
  60. public void testIsProfile() {
  61. assertFalse(myIdent.isProfile());
  62. myIdent.setOption("profile", "nickname", "foo");
  63. myIdent.setOption("profile", "realname", "foo");
  64. assertTrue(myIdent.isProfile());
  65. myIdent.unsetOption("profile", "nickname");
  66. myIdent.unsetOption("profile", "realname");
  67. }
  68. @Test
  69. public void testHasOption() {
  70. assertFalse(myIdent.hasOption("has", "option"));
  71. myIdent.setOption("has", "option", "");
  72. assertTrue(myIdent.hasOption("has", "option"));
  73. myIdent.unsetOption("has", "option");
  74. }
  75. @Test
  76. public void testGetOption() {
  77. myIdent.setOption("domain", "option", "value");
  78. final Properties props = myIdent.getProperties();
  79. assertEquals(props.getProperty("domain.option"), myIdent.getOption("domain", "option"));
  80. myIdent.unsetOption("domain", "option");
  81. }
  82. @Test
  83. public void testSetOption() {
  84. final int count = myIdent.getProperties().size();
  85. myIdent.setOption("foo", "bar", "baz");
  86. assertEquals(count + 1, myIdent.getProperties().size());
  87. myIdent.unsetOption("foo", "bar");
  88. }
  89. @Test
  90. public void testSetOptionInt() {
  91. myIdent.setOption("foo", "baz", 123);
  92. assertEquals("123", myIdent.getOption("foo", "baz"));
  93. }
  94. @Test
  95. public void testSetOptionBool() {
  96. myIdent.setOption("foo", "baz", false);
  97. assertEquals("false", myIdent.getOption("foo", "baz"));
  98. myIdent.setOption("foo", "baz", true);
  99. assertEquals("true", myIdent.getOption("foo", "baz"));
  100. }
  101. @Test
  102. public void testRemoveOption() {
  103. final Properties props = myIdent.getProperties();
  104. final int count = props.size();
  105. myIdent.setOption("foo", "bar", "baz");
  106. myIdent.unsetOption("foo", "bar");
  107. assertEquals(count, props.size());
  108. }
  109. @Test
  110. public void testSave() {
  111. myIdent.setOption("foo", "bar", "baz!");
  112. myIdent.save();
  113. myIdent = null;
  114. myIdent = Identity.buildIdentity(target);
  115. assertEquals("baz!", myIdent.getOption("foo", "bar"));
  116. myIdent.unsetOption("foo", "bar");
  117. myIdent.save();
  118. }
  119. @Test
  120. public void testGetTarget() {
  121. assertEquals(target.getData(), myIdent.getTarget().getData());
  122. assertEquals(target.getType(), myIdent.getTarget().getType());
  123. }
  124. @Test(expected=InvalidIdentityFileException.class)
  125. public void testNoName() throws IOException, InvalidIdentityFileException {
  126. new Identity(getClass().getResourceAsStream("identity1"), false);
  127. }
  128. @Test(expected=InvalidIdentityFileException.class)
  129. public void testNoTarget() throws IOException, InvalidIdentityFileException {
  130. new Identity(getClass().getResourceAsStream("identity2"), false);
  131. }
  132. @Test
  133. public void testSetListener() {
  134. final TestConfigListener listener = new TestConfigListener();
  135. myIdent.addListener(listener);
  136. assertEquals(0, listener.count);
  137. myIdent.setOption("unit", "test", "meep");
  138. assertEquals(1, listener.count);
  139. assertEquals("unit", listener.domain);
  140. assertEquals("test", listener.key);
  141. }
  142. @Test
  143. public void testUnsetListener() {
  144. final TestConfigListener listener = new TestConfigListener();
  145. myIdent.setOption("unit", "test", "meep");
  146. myIdent.addListener(listener);
  147. assertEquals(0, listener.count);
  148. myIdent.unsetOption("unit", "test");
  149. assertEquals(1, listener.count);
  150. assertEquals("unit", listener.domain);
  151. assertEquals("test", listener.key);
  152. }
  153. public static junit.framework.Test suite() {
  154. return new junit.framework.JUnit4TestAdapter(IdentityTest.class);
  155. }
  156. private class TestConfigListener implements ConfigChangeListener {
  157. public int count = 0;
  158. public String domain, key;
  159. public void configChanged(String domain, String key) {
  160. count++;
  161. this.domain = domain;
  162. this.key = key;
  163. }
  164. }
  165. }