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

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