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.

ActionComponentChainTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.actions;
  23. import com.dmdirc.Server;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.junit.runner.RunWith;
  29. import org.mockito.Mock;
  30. import org.mockito.runners.MockitoJUnitRunner;
  31. import static org.junit.Assert.*;
  32. import static org.mockito.Mockito.*;
  33. @RunWith(MockitoJUnitRunner.class)
  34. public class ActionComponentChainTest {
  35. @Mock private ActionManager actionManager;
  36. @Mock private IdentityManager identityManager;
  37. @Mock private AggregateConfigProvider configManager;
  38. @Before
  39. public void setUp() throws Exception {
  40. IdentityManager.setIdentityManager(identityManager);
  41. when(identityManager.getGlobalConfiguration()).thenReturn(configManager);
  42. when(actionManager.getComponent("SERVER_NAME"))
  43. .thenReturn(CoreActionComponent.SERVER_NAME);
  44. when(actionManager.getComponent("SERVER_NETWORK"))
  45. .thenReturn(CoreActionComponent.SERVER_NETWORK);
  46. when(actionManager.getComponent("STRING_LENGTH"))
  47. .thenReturn(CoreActionComponent.STRING_LENGTH);
  48. when(actionManager.getComponent("STRING_STRING"))
  49. .thenReturn(CoreActionComponent.STRING_STRING);
  50. when(actionManager.getComponent("USER_MODES"))
  51. .thenReturn(CoreActionComponent.USER_MODES);
  52. // TODO: Testing for this behaviour here is odd - ActionComponentChain
  53. // should probably do its own validation and throw a more
  54. // appropriate exception.
  55. when(actionManager.getComponent(""))
  56. .thenThrow(new AssertionError());
  57. }
  58. @Test
  59. public void testSingle() {
  60. final ActionComponentChain chain = new ActionComponentChain(String.class, "STRING_STRING", actionManager);
  61. assertEquals(chain.get("foo bar baz"), "foo bar baz");
  62. assertEquals("STRING_STRING", chain.toString());
  63. }
  64. @Test
  65. public void testDouble() {
  66. final ActionComponentChain chain = new ActionComponentChain(String.class,
  67. "STRING_STRING.STRING_STRING", actionManager);
  68. assertEquals(chain.get("foo bar baz"), "foo bar baz");
  69. assertEquals("STRING_STRING.STRING_STRING", chain.toString());
  70. }
  71. @Test(expected=IllegalArgumentException.class)
  72. public void testInvalidName() {
  73. new ActionComponentChain(String.class, "STRONG_STRING", actionManager);
  74. }
  75. @Test(expected=IllegalArgumentException.class)
  76. public void testInvalidType() {
  77. new ActionComponentChain(String.class, "USER_MODES.STRING_STRING", actionManager);
  78. }
  79. @Test(expected=IllegalArgumentException.class)
  80. public void testInvalidLink() {
  81. new ActionComponentChain(String.class, "STRING_STRING.USER_MODES", actionManager);
  82. }
  83. @Test
  84. public void testAppliesTo() {
  85. final ActionComponentChain chain = new ActionComponentChain(String.class,
  86. "STRING_STRING.STRING_STRING.STRING_LENGTH", actionManager);
  87. assertEquals(String.class, chain.appliesTo());
  88. }
  89. @Test
  90. public void testGetType() {
  91. final ActionComponentChain chain = new ActionComponentChain(String.class,
  92. "STRING_STRING.STRING_STRING.STRING_LENGTH", actionManager);
  93. assertEquals(Integer.class, chain.getType());
  94. }
  95. @Test
  96. public void testGetName() {
  97. final ActionComponentChain chain = new ActionComponentChain(String.class,
  98. "STRING_STRING.STRING_STRING.STRING_LENGTH", actionManager);
  99. assertTrue(chain.getName().indexOf(CoreActionComponent.STRING_STRING.getName()) > -1);
  100. assertTrue(chain.getName().indexOf(CoreActionComponent.STRING_LENGTH.getName()) > -1);
  101. }
  102. @Test(expected=AssertionError.class)
  103. public void testEmptyAppliesTo() {
  104. final ActionComponentChain chain = new ActionComponentChain(String.class, "", actionManager);
  105. chain.appliesTo();
  106. }
  107. @Test(expected=AssertionError.class)
  108. public void testEmptyGetType() {
  109. final ActionComponentChain chain = new ActionComponentChain(String.class, "", actionManager);
  110. chain.getType();
  111. }
  112. @Test(expected=AssertionError.class)
  113. public void testEmptyGetName() {
  114. final ActionComponentChain chain = new ActionComponentChain(String.class, "", actionManager);
  115. chain.getName();
  116. }
  117. @Test(expected=AssertionError.class)
  118. public void testEmptyToString() {
  119. final ActionComponentChain chain = new ActionComponentChain(String.class, "", actionManager);
  120. chain.toString();
  121. }
  122. @Test()
  123. public void testRequiresConnection1() {
  124. final ActionComponentChain chain = new ActionComponentChain(Server.class,
  125. "SERVER_NETWORK", actionManager);
  126. assertTrue(chain.requiresConnection());
  127. }
  128. @Test()
  129. public void testRequiresConnection2() {
  130. final ActionComponentChain chain = new ActionComponentChain(Server.class,
  131. "SERVER_NETWORK.STRING_LENGTH", actionManager);
  132. assertTrue(chain.requiresConnection());
  133. }
  134. @Test()
  135. public void testRequiresConnection3() {
  136. final ActionComponentChain chain = new ActionComponentChain(Server.class,
  137. "SERVER_NAME.STRING_LENGTH", actionManager);
  138. assertFalse(chain.requiresConnection());
  139. }
  140. }