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

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