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

IdentityTest.java 7.0KB

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