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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2006-2011 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.ConfigManager;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.config.InvalidIdentityFileException;
  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<String, String>();
  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. IdentityManager.load();
  50. ActionManager.getActionManager().initialise();
  51. SETTINGS.put("alpha", "A");
  52. SETTINGS.put("bravo", "$alpha");
  53. SETTINGS.put("charlie", "${bravo}");
  54. SETTINGS.put("delta", "${${bravo}${bravo}}");
  55. SETTINGS.put("AA", "win!");
  56. }
  57. public ActionSubstitutorTest(final String input, final String expected) {
  58. this.input = input;
  59. this.expected = expected;
  60. this.channel = mock(Channel.class);
  61. final ConfigManager manager = mock(ConfigManager.class);
  62. final Server server = mock(Server.class);
  63. final ChannelClientInfo clientInfo = mock(ChannelClientInfo.class);
  64. when(channel.getServer()).thenReturn(server);
  65. when(channel.getConfigManager()).thenReturn(manager);
  66. when(server.getState()).thenReturn(ServerState.DISCONNECTED);
  67. when(server.getAwayMessage()).thenReturn("foo");
  68. when(server.getProtocol()).thenReturn("$alpha");
  69. when(manager.getOptions(eq("actions"))).thenReturn(SETTINGS);
  70. for (Map.Entry<String, String> entry : SETTINGS.entrySet()) {
  71. when(manager.hasOptionString("actions", entry.getKey())).thenReturn(true);
  72. when(manager.getOption("actions", entry.getKey())).thenReturn(entry.getValue());
  73. }
  74. substitutor = new ActionSubstitutor(CoreActionType.CHANNEL_MESSAGE);
  75. args = new Object[]{
  76. channel,
  77. clientInfo,
  78. "1 2 3 fourth_word_here 5 6 7"
  79. };
  80. }
  81. @Test
  82. public void testSubstitution() {
  83. assertEquals(expected, substitutor.doSubstitution(input, args));
  84. }
  85. @Parameterized.Parameters
  86. public static List<String[]> data() {
  87. final String[][] tests = {
  88. // -- Existing behaviour -------------------------------------------
  89. // ---- No subs at all ---------------------------------------------
  90. {"no subs here!", "no subs here!"},
  91. // ---- Config subs ------------------------------------------------
  92. {"$alpha", "A"},
  93. {"--$alpha--", "--A--"},
  94. // ---- Word subs --------------------------------------------------
  95. {"$1", "1"},
  96. {"$4", "fourth_word_here"},
  97. {"$5-", "5 6 7"},
  98. // ---- Component subs ---------------------------------------------
  99. {"${2.STRING_LENGTH}", "28"},
  100. {"${2.STRING_STRING}", "1 2 3 fourth_word_here 5 6 7"},
  101. // ---- Server subs ------------------------------------------------
  102. {"${SERVER_MYAWAYREASON}", "foo"},
  103. // ---- Combinations -----------------------------------------------
  104. {"${1}${2.STRING_LENGTH}$alpha", "128A"},
  105. {"$alpha$4${SERVER_MYAWAYREASON}", "Afourth_word_herefoo"},
  106. // -- New behaviour ------------------------------------------------
  107. // ---- Config subs ------------------------------------------------
  108. {"${alpha}", "A"},
  109. {"\\$alpha", "$alpha"},
  110. {"$bravo", "A"},
  111. {"$charlie", "A"},
  112. {"$delta", "win!"},
  113. {"$sigma", "not_defined"},
  114. // ---- Word subs --------------------------------------------------
  115. {"$5-6", "5 6"},
  116. {"${5-6}", "5 6"},
  117. {"${5-$6}", "5 6"},
  118. {"${5-${${6}}}", "5 6"},
  119. // ---- Component subs ---------------------------------------------
  120. {"${2.STRING_STRING.STRING_LENGTH}", "28"},
  121. {"${2.STRING_FLUB.STRING_LENGTH}", "illegal_component"},
  122. {"${SERVER_NETWORKFOO}", "illegal_component"},
  123. {"${SERVER_NETWORK}", "not_connected"},
  124. {"${SERVER_PROTOCOL}", "$alpha"},
  125. // ---- Escaping ---------------------------------------------------
  126. {"\\$1", "$1"},
  127. {"\\$alpha $alpha", "$alpha A"},
  128. {"\\$$1", "$1"},
  129. {"\\\\$4", "\\fourth_word_here"},
  130. {"\\\\${4}", "\\fourth_word_here"},
  131. {"\\\\\\$4", "\\$4"},
  132. };
  133. return Arrays.asList(tests);
  134. }
  135. }