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.

BugtraqFormatterTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2013 by syntevo GmbH. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * o Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. *
  10. * o Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * o Neither the name of syntevo GmbH nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.syntevo.bugtraq;
  31. import junit.framework.*;
  32. import java.util.*;
  33. import org.jetbrains.annotations.*;
  34. public class BugtraqFormatterTest extends TestCase {
  35. // Accessing ==============================================================
  36. public void testSimple() throws BugtraqException {
  37. final BugtraqFormatter formatter = createFormatter(createEntry("https://jira.atlassian.com/browse/%BUGID%", "JRA-\\d+"));
  38. doTest(formatter, "JRA-7399: Email subject formatting", l("JRA-7399", "https://jira.atlassian.com/browse/JRA-7399"), t(": Email subject formatting"));
  39. doTest(formatter, " JRA-7399, JRA-7398: Email subject formatting", t(" "), l("JRA-7399", "https://jira.atlassian.com/browse/JRA-7399"), t(", "), l("JRA-7398", "https://jira.atlassian.com/browse/JRA-7398"), t(": Email subject formatting"));
  40. doTest(formatter, "Fixed JRA-7399", t("Fixed "), l("JRA-7399", "https://jira.atlassian.com/browse/JRA-7399"));
  41. }
  42. public void testTwoNonIntersectingConfigurations() throws BugtraqException {
  43. final BugtraqFormatter formatter = createFormatter(createEntry("https://jira.atlassian.com/browse/%BUGID%", "JRA-\\d+"),
  44. createEntry("https://issues.apache.org/jira/browse/%BUGID%", "VELOCITY-\\d+"));
  45. doTest(formatter, "JRA-7399, VELOCITY-847: fix", l("JRA-7399", "https://jira.atlassian.com/browse/JRA-7399"), t(", "), l("VELOCITY-847", "https://issues.apache.org/jira/browse/VELOCITY-847"), t(": fix"));
  46. doTest(formatter, " JRA-7399: fix/VELOCITY-847", t(" "), l("JRA-7399", "https://jira.atlassian.com/browse/JRA-7399"), t(": fix/"), l("VELOCITY-847", "https://issues.apache.org/jira/browse/VELOCITY-847"));
  47. doTest(formatter, "JRA-7399VELOCITY-847", l("JRA-7399", "https://jira.atlassian.com/browse/JRA-7399"), l("VELOCITY-847", "https://issues.apache.org/jira/browse/VELOCITY-847"));
  48. }
  49. public void testTwoIntersectingConfigurations() throws BugtraqException {
  50. final BugtraqFormatter formatter = createFormatter(createEntry("https://host1/%BUGID%", "A[AB]"),
  51. createEntry("https://host2/%BUGID%", "BA[A]?"));
  52. doTest(formatter, "AA: fix", l("AA", "https://host1/AA"), t(": fix"));
  53. doTest(formatter, "AB: fix", l("AB", "https://host1/AB"), t(": fix"));
  54. doTest(formatter, "BA: fix", l("BA", "https://host2/BA"), t(": fix"));
  55. doTest(formatter, "BAA: fix", l("BAA", "https://host2/BAA"), t(": fix"));
  56. doTest(formatter, "BAAA: fix", l("BAA", "https://host2/BAA"), t("A: fix"));
  57. doTest(formatter, "BAAAA: fix", l("BAA", "https://host2/BAA"), l("AA", "https://host1/AA"), t(": fix"));
  58. doTest(formatter, "BAAAAA: fix", l("BAA", "https://host2/BAA"), l("AA", "https://host1/AA"), t("A: fix"));
  59. doTest(formatter, "BAAABA: fix", l("BAA", "https://host2/BAA"), l("AB", "https://host1/AB"), t("A: fix"));
  60. doTest(formatter, "BAAABAA: fix", l("BAA", "https://host2/BAA"), l("AB", "https://host1/AB"), l("AA", "https://host1/AA"), t(": fix"));
  61. doTest(formatter, "BAAB: fix", l("BAA", "https://host2/BAA"), t("B: fix"));
  62. doTest(formatter, "BAAAB: fix", l("BAA", "https://host2/BAA"), l("AB", "https://host1/AB"), t(": fix"));
  63. doTest(formatter, "BAABBA: fix", l("BAA", "https://host2/BAA"), t("B"), l("BA", "https://host2/BA"), t(": fix"));
  64. }
  65. // Utils ==================================================================
  66. private BugtraqFormatter createFormatter(BugtraqEntry ... entries) {
  67. return new BugtraqFormatter(new BugtraqConfig(Arrays.asList(entries)));
  68. }
  69. private BugtraqEntry createEntry(String url, String ... logRegexs) throws BugtraqException {
  70. return new BugtraqEntry(url, Arrays.asList(logRegexs));
  71. }
  72. private Text t(String text) {
  73. return new Text(text);
  74. }
  75. private Link l(String text, String url) {
  76. return new Link(text, url);
  77. }
  78. private void doTest(BugtraqFormatter formatter, String message, Atom ... expectedAtoms) {
  79. final List<Atom> actualAtoms = new ArrayList<Atom>();
  80. formatter.formatLogMessage(message, new BugtraqFormatter.OutputHandler() {
  81. @Override
  82. public void appendText(@NotNull String text) {
  83. actualAtoms.add(t(text));
  84. }
  85. @Override
  86. public void appendLink(@NotNull String name, @NotNull String target) {
  87. actualAtoms.add(l(name, target));
  88. }
  89. });
  90. assertEquals(Arrays.asList(expectedAtoms), actualAtoms);
  91. }
  92. // Inner Classes ==========================================================
  93. private static interface Atom {
  94. }
  95. private static class Text implements Atom {
  96. private final String text;
  97. private Text(String text) {
  98. this.text = text;
  99. }
  100. @Override
  101. public String toString() {
  102. return text;
  103. }
  104. @Override
  105. public int hashCode() {
  106. return text.hashCode();
  107. }
  108. @Override
  109. public boolean equals(Object obj) {
  110. if (obj == null || obj.getClass() != getClass()) {
  111. return false;
  112. }
  113. return text.equals(((Text)obj).text);
  114. }
  115. }
  116. private static class Link implements Atom {
  117. private final String text;
  118. private final String url;
  119. private Link(String text, String url) {
  120. this.text = text;
  121. this.url = url;
  122. }
  123. @Override
  124. public String toString() {
  125. return "(" + text + "," + url + ")";
  126. }
  127. @Override
  128. public int hashCode() {
  129. return text.hashCode() ^ url.hashCode();
  130. }
  131. @Override
  132. public boolean equals(Object obj) {
  133. if (obj == null || obj.getClass() != getClass()) {
  134. return false;
  135. }
  136. return text.equals(((Link)obj).text)
  137. && url.equals(((Link)obj).url);
  138. }
  139. }
  140. }