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

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