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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2006-2014 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.interfaces.config.ConfigChangeListener;
  24. import com.dmdirc.util.io.ConfigFile;
  25. import com.dmdirc.util.validators.PermissiveValidator;
  26. import java.io.IOException;
  27. import java.util.Map;
  28. import org.junit.Before;
  29. import org.junit.Ignore;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.mockito.Mock;
  33. import org.mockito.runners.MockitoJUnitRunner;
  34. import static org.junit.Assert.*;
  35. import static org.mockito.Mockito.*;
  36. @RunWith(MockitoJUnitRunner.class)
  37. public class IdentityTest {
  38. @Mock private IdentityManager identityManager;
  39. private ConfigFileBackedConfigProvider myIdent;
  40. private ConfigTarget target;
  41. @Before
  42. public void setUp() throws Exception {
  43. target = new ConfigTarget();
  44. target.setChannel("#unittest@unittest");
  45. myIdent = new ConfigFileBackedConfigProvider(identityManager,
  46. new ConfigFile(getClass().getResourceAsStream("identity2")), target);
  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", new PermissiveValidator<String>()));
  64. myIdent.setOption("has", "option", "");
  65. assertTrue(myIdent.hasOption("has", "option", new PermissiveValidator<String>()));
  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. @Ignore("Needs to be rewritten to work without an IdentityManager/profile dir")
  105. public void testSave() throws IOException, InvalidIdentityFileException {
  106. myIdent.setOption("foo", "bar", "baz!");
  107. myIdent.save();
  108. myIdent = new ConfigFileBackedConfigProvider(identityManager,
  109. 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 ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity1"), false);
  120. }
  121. @Test(expected = InvalidIdentityFileException.class)
  122. public void testNoTarget() throws IOException, InvalidIdentityFileException {
  123. new ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity2"), false);
  124. }
  125. @Test
  126. public void testMigrate() throws IOException, InvalidIdentityFileException {
  127. final ConfigFileBackedConfigProvider id = new ConfigFileBackedConfigProvider(getClass().
  128. 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. }