]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3531 Allow to use two instances of MultilinesDocTokenizer without subclassing
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 10 Jul 2012 18:40:00 +0000 (00:40 +0600)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 10 Jul 2012 18:48:54 +0000 (00:48 +0600)
sonar-colorizer/src/main/java/org/sonar/colorizer/HtmlCodeBuilder.java
sonar-colorizer/src/main/java/org/sonar/colorizer/MultilinesDocTokenizer.java
sonar-colorizer/src/test/java/org/sonar/colorizer/MultilinesDocTokenizerTest.java

index ed6d6b4c832db7c143b229eb0fbe9b97f7ded882..83350fd433b135c538e3694e3b77f28db77b5048 100644 (file)
@@ -25,7 +25,7 @@ import java.util.Map;
 public class HtmlCodeBuilder implements Appendable {
 
   private StringBuilder colorizedCode = new StringBuilder();
-  private Map variables = new HashMap(); // stateful data
+  private Map variables = new HashMap();
 
   public Appendable append(CharSequence csq) {
     for (int i = 0; i < csq.length(); i++) {
@@ -67,7 +67,7 @@ public class HtmlCodeBuilder implements Appendable {
 
   /**
    * Save a stateful variable.
-   * 
+   *
    * @param key
    *          can NOT be null
    * @param value
index 5471bcc4fc838cba6b7ea995296af87631c45b60..f77f2bd7dd45bac2d4423a408aa0c6ccecaa8cec 100644 (file)
@@ -104,16 +104,12 @@ public class MultilinesDocTokenizer extends Tokenizer {
 
   private boolean isCommentStartedOnPreviousLine(HtmlCodeBuilder codeBuilder) {
     Boolean b = (Boolean) codeBuilder.getVariable(COMMENT_STARTED_ON_PREVIOUS_LINE, Boolean.FALSE);
-    return (b == Boolean.TRUE) && (getTokenizerId().equals(codeBuilder.getVariable(COMMENT_TOKENIZER)));
+    return (b == Boolean.TRUE) && (this.equals(codeBuilder.getVariable(COMMENT_TOKENIZER)));
   }
 
   private void setCommentStartedOnPreviousLine(HtmlCodeBuilder codeBuilder, Boolean b) {
     codeBuilder.setVariable(COMMENT_STARTED_ON_PREVIOUS_LINE, b);
-    codeBuilder.setVariable(COMMENT_TOKENIZER, b ? getTokenizerId() : null);
-  }
-
-  private String getTokenizerId() {
-    return getClass().getSimpleName();
+    codeBuilder.setVariable(COMMENT_TOKENIZER, b ? this : null);
   }
 
 }
index 8265efae16c0a5b537a83eccaa4d39e81090a272..bf77bfe8b4f200deba4bbf90ac70f5182b2874a4 100644 (file)
@@ -83,6 +83,19 @@ public class MultilinesDocTokenizerTest {
     assertThat(codeBuilder.toString()).isEqualTo("&lt;!-- multi-line comment--&gt;");
   }
 
+  /**
+   * SONAR-3531
+   */
+  @Test
+  public void should_be_possible_to_use_two_instances() {
+    CodeReader reader = new CodeReader("<!-- multi-line comment\n--> private part");
+    MultilinesDocTokenizer tokenizer1 = new MultiLineDocTokenizerImpl("<!--", "-->");
+    MultilinesDocTokenizer tokenizer2 = new MultiLineDocTokenizerImpl("<!--", "-->");
+    assertThat(tokenizer1.consume(reader, codeBuilder)).isTrue();
+    reader.pop();
+    assertThat(tokenizer2.consume(reader, codeBuilder)).isFalse();
+  }
+
   public class MultiLineDocTokenizerImpl extends MultilinesDocTokenizer {
     public MultiLineDocTokenizerImpl(String startToken, String endToken) {
       super(startToken, endToken, "", "");