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