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

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