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

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