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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.init();
  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(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. substitutor = new ActionSubstitutor(CoreActionType.CHANNEL_MESSAGE);
  74. args = new Object[]{
  75. channel,
  76. clientInfo,
  77. "1 2 3 fourth_word_here 5 6 7"
  78. };
  79. }
  80. @Test
  81. public void testSubstitution() {
  82. assertEquals(expected, substitutor.doSubstitution(input, args));
  83. }
  84. @Parameterized.Parameters
  85. public static List<String[]> data() {
  86. final String[][] tests = {
  87. // -- Existing behaviour -------------------------------------------
  88. // ---- No subs at all ---------------------------------------------
  89. {"no subs here!", "no subs here!"},
  90. // ---- Config subs ------------------------------------------------
  91. {"$alpha", "A"},
  92. {"--$alpha--", "--A--"},
  93. // ---- Word subs --------------------------------------------------
  94. {"$1", "1"},
  95. {"$4", "fourth_word_here"},
  96. {"$5-", "5 6 7"},
  97. // ---- Component subs ---------------------------------------------
  98. {"${2.STRING_LENGTH}", "28"},
  99. {"${2.STRING_STRING}", "1 2 3 fourth_word_here 5 6 7"},
  100. // ---- Server subs ------------------------------------------------
  101. {"${SERVER_MYAWAYREASON}", "foo"},
  102. // ---- Combinations -----------------------------------------------
  103. {"${1}${2.STRING_LENGTH}$alpha", "128A"},
  104. {"$alpha$4${SERVER_MYAWAYREASON}", "Afourth_word_herefoo"},
  105. // -- New behaviour ------------------------------------------------
  106. // ---- Config subs ------------------------------------------------
  107. {"${alpha}", "A"},
  108. {"\\$alpha", "$alpha"},
  109. {"$bravo", "A"},
  110. {"$charlie", "A"},
  111. {"$delta", "win!"},
  112. {"$sigma", "not_defined"},
  113. // ---- Word subs --------------------------------------------------
  114. {"$5-6", "5 6"},
  115. {"${5-6}", "5 6"},
  116. {"${5-$6}", "5 6"},
  117. {"${5-${${6}}}", "5 6"},
  118. // ---- Component subs ---------------------------------------------
  119. {"${2.STRING_STRING.STRING_LENGTH}", "28"},
  120. {"${2.STRING_FLUB.STRING_LENGTH}", "illegal_component"},
  121. {"${SERVER_NETWORKFOO}", "illegal_component"},
  122. {"${SERVER_NETWORK}", "not_connected"},
  123. // ---- Escaping ---------------------------------------------------
  124. {"\\$1", "$1"},
  125. {"\\$alpha $alpha", "$alpha A"},
  126. {"\\$$1", "$1"},
  127. {"\\\\$4", "\\fourth_word_here"},
  128. {"\\\\${4}", "\\fourth_word_here"},
  129. {"\\\\\\$4", "\\$4"},
  130. };
  131. return Arrays.asList(tests);
  132. }
  133. }