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.

ActionSubstitutorTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.Channel;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.ServerState;
  26. import com.dmdirc.config.InvalidIdentityFileException;
  27. import com.dmdirc.interfaces.ActionController;
  28. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  29. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  30. import java.util.Arrays;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import org.junit.BeforeClass;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.junit.runners.Parameterized;
  38. import static org.junit.Assert.*;
  39. import static org.mockito.Mockito.*;
  40. @RunWith(Parameterized.class)
  41. public class ActionSubstitutorTest {
  42. private static final Map<String, String> SETTINGS = new HashMap<>();
  43. private final String input, expected;
  44. private final Channel channel;
  45. private final ActionSubstitutor substitutor;
  46. private final Object[] args;
  47. @BeforeClass
  48. public static void setup() throws InvalidIdentityFileException {
  49. SETTINGS.put("alpha", "A");
  50. SETTINGS.put("bravo", "$alpha");
  51. SETTINGS.put("charlie", "${bravo}");
  52. SETTINGS.put("delta", "${${bravo}${bravo}}");
  53. SETTINGS.put("AA", "win!");
  54. }
  55. public ActionSubstitutorTest(final String input, final String expected) {
  56. this.input = input;
  57. this.expected = expected;
  58. this.channel = mock(Channel.class);
  59. final AggregateConfigProvider manager = mock(AggregateConfigProvider.class);
  60. final Server server = mock(Server.class);
  61. final ChannelClientInfo clientInfo = mock(ChannelClientInfo.class);
  62. when(channel.getServer()).thenReturn(server);
  63. when(channel.getConfigManager()).thenReturn(manager);
  64. when(server.getState()).thenReturn(ServerState.DISCONNECTED);
  65. when(server.getAwayMessage()).thenReturn("foo");
  66. when(server.getProtocol()).thenReturn("$alpha");
  67. when(manager.getOptions(eq("actions"))).thenReturn(SETTINGS);
  68. for (Map.Entry<String, String> entry : SETTINGS.entrySet()) {
  69. when(manager.hasOptionString("actions", entry.getKey())).thenReturn(true);
  70. when(manager.getOption("actions", entry.getKey())).thenReturn(entry.getValue());
  71. }
  72. final ActionController controller = mock(ActionController.class);
  73. when(controller.getComponent("STRING_STRING")).thenReturn(CoreActionComponent.STRING_STRING);
  74. when(controller.getComponent("STRING_LENGTH")).thenReturn(CoreActionComponent.STRING_LENGTH);
  75. when(controller.getComponent("SERVER_NETWORK")).thenReturn(CoreActionComponent.SERVER_NETWORK);
  76. when(controller.getComponent("SERVER_PROTOCOL")).thenReturn(CoreActionComponent.SERVER_PROTOCOL);
  77. when(controller.getComponent("SERVER_MYAWAYREASON")).thenReturn(CoreActionComponent.SERVER_MYAWAYREASON);
  78. substitutor = new ActionSubstitutor(controller, CoreActionType.CHANNEL_MESSAGE);
  79. args = new Object[]{
  80. channel,
  81. clientInfo,
  82. "1 2 3 fourth_word_here 5 6 7"
  83. };
  84. }
  85. @Test
  86. public void testSubstitution() {
  87. assertEquals(expected, substitutor.doSubstitution(input, args));
  88. }
  89. @Parameterized.Parameters
  90. public static List<String[]> data() {
  91. final String[][] tests = {
  92. // -- Existing behaviour -------------------------------------------
  93. // ---- No subs at all ---------------------------------------------
  94. {"no subs here!", "no subs here!"},
  95. // ---- Config subs ------------------------------------------------
  96. {"$alpha", "A"},
  97. {"--$alpha--", "--A--"},
  98. // ---- Word subs --------------------------------------------------
  99. {"$1", "1"},
  100. {"$4", "fourth_word_here"},
  101. {"$5-", "5 6 7"},
  102. // ---- Component subs ---------------------------------------------
  103. {"${2.STRING_LENGTH}", "28"},
  104. {"${2.STRING_STRING}", "1 2 3 fourth_word_here 5 6 7"},
  105. // ---- Server subs ------------------------------------------------
  106. {"${SERVER_MYAWAYREASON}", "foo"},
  107. // ---- Combinations -----------------------------------------------
  108. {"${1}${2.STRING_LENGTH}$alpha", "128A"},
  109. {"$alpha$4${SERVER_MYAWAYREASON}", "Afourth_word_herefoo"},
  110. // -- New behaviour ------------------------------------------------
  111. // ---- Config subs ------------------------------------------------
  112. {"${alpha}", "A"},
  113. {"\\$alpha", "$alpha"},
  114. {"$bravo", "A"},
  115. {"$charlie", "A"},
  116. {"$delta", "win!"},
  117. {"$sigma", "not_defined"},
  118. // ---- Word subs --------------------------------------------------
  119. {"$5-6", "5 6"},
  120. {"${5-6}", "5 6"},
  121. {"${5-$6}", "5 6"},
  122. {"${5-${${6}}}", "5 6"},
  123. // ---- Component subs ---------------------------------------------
  124. {"${2.STRING_STRING.STRING_LENGTH}", "28"},
  125. {"${2.STRING_FLUB.STRING_LENGTH}", "illegal_component"},
  126. {"${SERVER_NETWORKFOO}", "illegal_component"},
  127. {"${SERVER_NETWORK}", "not_connected"},
  128. {"${SERVER_PROTOCOL}", "$alpha"},
  129. // ---- Escaping ---------------------------------------------------
  130. {"\\$1", "$1"},
  131. {"\\$alpha $alpha", "$alpha A"},
  132. {"\\$$1", "$1"},
  133. {"\\\\$4", "\\fourth_word_here"},
  134. {"\\\\${4}", "\\fourth_word_here"},
  135. {"\\\\\\$4", "\\$4"},
  136. };
  137. return Arrays.asList(tests);
  138. }
  139. }