Exemplo n.º 1
0
  /** Instead of only testing onCommandLong, test feeding into onMessage */
  @Test(dataProvider = "onCommandLongTests")
  public void executeTest(String cmdMessage, OnCommandLong command, String[][] expectedArgs)
      throws Exception {
    // Remove the prefix
    cmdMessage = cmdMessage.substring(1);

    // Build the messageEvent
    final StringBuilder response = new StringBuilder();
    MessageEvent messageEvent =
        new MessageEvent(bot, channel, null, cmdMessage) {
          @Override
          public void respond(String commandResponse) {
            if (commandResponse != null) response.append(commandResponse);
          }
        };

    // Add our test listener
    controller.getHookManager().addHook(command);

    // Feed into onMessage
    log.trace("Sending message " + cmdMessage);
    hook.execute(messageEvent, channel, user, cmdMessage);

    // Verify the results
    assertEquals(
        response.toString(),
        "Success",
        "onCommandLong in test " + command.getName() + " doesn't return expected value");
    assertTrue(
        Arrays.deepEquals(command.getArgs(), expectedArgs),
        "Command test " + command.getName() + " args don't equal");

    // Remove it to cleanup
    controller.getHookManager().removeHook(command);
  }
  /** Test the normalization and the middle substring match. */
  @Test(dataProvider = "substringMiddleMatchData")
  public void middleMatchingRules(
      final String value, final String[] middleSubs, final ConditionResult result)
      throws Exception {
    final MatchingRule rule = getRule();

    // normalize the 2 provided values and check that they are equals
    final ByteString normalizedValue = rule.normalizeAttributeValue(ByteString.valueOf(value));

    final StringBuilder printableMiddleSubs = new StringBuilder();
    final List<ByteSequence> middleList = new ArrayList<>(middleSubs.length);
    printableMiddleSubs.append("*");
    for (final String middleSub : middleSubs) {
      printableMiddleSubs.append(middleSub);
      printableMiddleSubs.append("*");
      middleList.add(ByteString.valueOf(middleSub));
    }

    final ConditionResult substringAssertionMatches =
        rule.getSubstringAssertion(null, middleList, null).matches(normalizedValue);
    final ConditionResult assertionMatches =
        rule.getAssertion(ByteString.valueOf(printableMiddleSubs)).matches(normalizedValue);
    final String message = getMessage("middle", rule, value, printableMiddleSubs.toString());
    assertEquals(substringAssertionMatches, result, message);
    assertEquals(assertionMatches, result, message);
  }
Exemplo n.º 3
0
 private static <T> String toJoinedString(List<T> list, String... p) {
   StringBuilder sb = new StringBuilder();
   String sep = "";
   String init = "";
   String end = "";
   String empty = null;
   switch (p.length) {
     case 4:
       empty = p[3];
       /*fall-through*/
     case 3:
       end = p[2];
       /*fall-through*/
     case 2:
       init = p[1];
       /*fall-through*/
     case 1:
       sep = p[0];
       break;
   }
   if (empty != null && list.isEmpty()) {
     return empty;
   } else {
     sb.append(init);
     for (T x : list) {
       if (sb.length() != init.length()) {
         sb.append(sep);
       }
       sb.append(x.toString());
     }
     sb.append(end);
     return sb.toString();
   }
 }
Exemplo n.º 4
0
 private String quote(String string) {
   StringBuilder buf = new StringBuilder();
   buf.append('\'');
   for (int i = 0; i < string.length(); i++) {
     char ch = string.charAt(i);
     if (ch == '!' || ch == '\'') {
       buf.append('!');
     }
     buf.append(ch);
   }
   buf.append('\'');
   return buf.toString();
 }
  @Test
  public void testWriteWithArrayExpansion() {
    StringBuilder longText = new StringBuilder(1000000);
    for (int i = 0; i < 1000000; i++) {
      longText.append(i % 10);
    }

    CsvWriterSettings s = new CsvWriterSettings();
    s.setMaxCharsPerColumn(2);
    CsvWriter w = new CsvWriter(s);

    w.writeValue(longText);
    String value = w.writeValuesToString().trim();
    assertEquals(value.length(), longText.length());
    assertEquals(value, longText.toString());

    w.writeValue(longText);
    w.writeValue(longText);
    value = w.writeValuesToString().trim();
    assertEquals(value.length(), longText.length() * 2 + 1);
    assertEquals(value, longText.toString() + "," + longText.toString());
  }
  /** Test that invalid values are rejected. */
  @Test(
      expectedExceptions = DecodeException.class,
      dataProvider = "substringInvalidAssertionValues")
  public void matchingRulesInvalidAssertionValuesString(
      final String subInitial, final String[] anys, final String subFinal) throws Exception {
    // Get the instance of the rule to be tested.
    final MatchingRule rule = getRule();

    final StringBuilder assertionString = new StringBuilder();
    if (subInitial != null) {
      assertionString.append(subInitial);
    }
    assertionString.append("*");
    for (final String middleSub : anys) {
      assertionString.append(middleSub);
      assertionString.append("*");
    }
    if (subFinal != null) {
      assertionString.append(subFinal);
    }
    rule.getAssertion(ByteString.valueOf(assertionString.toString()));
  }
  private File createDelimitedFile(Set<TestBean> theBeans) {

    File aDelimitedFile = null;

    try {

      // Create a temporary file ...
      aDelimitedFile = createTempFile(this.getClass().getName(), ".csv");
      aDelimitedFile.deleteOnExit();

      // Write the set of beans out to the file ...
      BufferedWriter aWriter = new BufferedWriter(new FileWriter(aDelimitedFile));
      for (TestBean aBean : theBeans) {

        StringBuilder aStringBuilder = new StringBuilder();

        aStringBuilder.append(aBean.getFirstName());
        aStringBuilder.append(DEFAULT_DELIMITER);
        aStringBuilder.append(aBean.getLastName());
        aStringBuilder.append(DEFAULT_DELIMITER);
        aStringBuilder.append(aBean.getFavoriteColor());

        aWriter.append(aStringBuilder);
        aWriter.newLine();
      }

      // File all buffers and close up shop ...
      aWriter.flush();
      aWriter.close();

    } catch (IOException e) {

      fail("Failed to create test delimited file.", e);
    }

    return aDelimitedFile;
  }
Exemplo n.º 8
0
 protected void logMultiArray(Object[][] array, String message) {
   StringBuilder sb = new StringBuilder(message);
   for (int outer = 0; outer < array.length; outer++)
     sb.append("\n").append(outer).append(" - ").append(StringUtils.join(array[outer], ", "));
   log.trace(sb.toString().trim());
 }