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

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