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

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