From 698487d5318d8adbdf720eb4ebb283dae59740fd Mon Sep 17 00:00:00 2001
From: JamesStauffer <jstauffer@spscommerce.com>
Date: Tue, 21 May 2013 13:55:16 -0500
Subject: [PATCH] Fixed minor bugs with format conversion.

The pattern to find logger calls wasn't restrictive enough.
Creates new Object[] explicitly when there are more than 2 parameters because that seems required with 1.7.2
Added more unit tests.
---
 .../org/slf4j/migrator/line/FormatConversionRule.java | 19 +++++++++++++++----
 .../java/org/slf4j/migrator/line/Log4jRuleSet.java    |  2 +-
 .../org/slf4j/migrator/line/Log4jRuleSetTest.java     |  7 +++++++
 3 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/FormatConversionRule.java b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/FormatConversionRule.java
index 7c6d038..fad5490 100644
--- a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/FormatConversionRule.java
+++ b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/FormatConversionRule.java
@@ -65,13 +65,13 @@ public class FormatConversionRule implements ConversionRule {
         for(matcher = concatPatternStart.matcher(text); matcher.find(); matcher = concatPatternStart.matcher(text)) {
             replacementLine.append(text.substring(0, matcher.start()));//Keep everything up to the concat
             Matcher matcherEndMiddle = concatPatternEndMiddle.matcher(text);
-            if(matcherEndMiddle.find()) {
+            if(matcherEndMiddle.find(matcher.end())) {
                 concatObjects.add(text.substring(matcher.end(), matcherEndMiddle.start()));
                 replacementLine.append("{}");
                 text = text.substring(matcherEndMiddle.end());
             } else {
                 Matcher matcherEndfinal = concatPatternEndFinal.matcher(text);
-                if(matcherEndfinal.find()) {
+                if(matcherEndfinal.find(matcher.end())) {
                     concatObjects.add(text.substring(matcher.end(), matcherEndfinal.start()));
                     replacementLine.append("{}");
                     text = "\");" + text.substring(matcherEndfinal.end());
@@ -85,8 +85,19 @@ public class FormatConversionRule implements ConversionRule {
         Matcher endMatcher = logEndPattern.matcher(text);
         if(endMatcher.find()) {
             replacementLine.append(text.substring(0, endMatcher.start()));
-            for(String concatObject : concatObjects) {
-                replacementLine.append(", ").append(concatObject);
+            if(concatObjects.size() > 2) {
+                replacementLine.append(", new Object[]{");
+                for(int concatObjecIndex = 0; concatObjecIndex < concatObjects.size(); concatObjecIndex++) {
+                    if(concatObjecIndex > 0) {
+                        replacementLine.append(", ");
+                    }
+                    replacementLine.append(concatObjects.get(concatObjecIndex));
+                }
+                replacementLine.append("}");
+            } else {
+                for(String concatObject : concatObjects) {
+                    replacementLine.append(", ").append(concatObject);
+                }
             }
             replacementLine.append(");");
             return new String[]{replacementLine.toString()};
diff --git a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/Log4jRuleSet.java b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/Log4jRuleSet.java
index ccca69b..d874fae 100644
--- a/slf4j-migrator/src/main/java/org/slf4j/migrator/line/Log4jRuleSet.java
+++ b/slf4j-migrator/src/main/java/org/slf4j/migrator/line/Log4jRuleSet.java
@@ -62,7 +62,7 @@ public class Log4jRuleSet implements RuleSet {
         .compile("LogManager.getLogger\\("), "LoggerFactory.getLogger(");
 
     FormatConversionRule fc0 = new FormatConversionRule(Pattern
-        .compile("log(log)?\\.(trace)|(debug)|(info)|(warn)|(error)|(fatal)\\(\""));
+        .compile("log(log)?\\.((trace)|(debug)|(info)|(warn)|(error))\\(\""));
     conversionRuleList = new ArrayList<ConversionRule>();
     conversionRuleList.add(crImport0);
     conversionRuleList.add(crImport1);
diff --git a/slf4j-migrator/src/test/java/org/slf4j/migrator/line/Log4jRuleSetTest.java b/slf4j-migrator/src/test/java/org/slf4j/migrator/line/Log4jRuleSetTest.java
index 2d86e0b..2d54639 100644
--- a/slf4j-migrator/src/test/java/org/slf4j/migrator/line/Log4jRuleSetTest.java
+++ b/slf4j-migrator/src/test/java/org/slf4j/migrator/line/Log4jRuleSetTest.java
@@ -25,6 +25,7 @@
 package org.slf4j.migrator.line;
 
 import java.util.Arrays;
+import java.util.regex.*;
 
 import org.slf4j.migrator.line.LineConverter;
 import org.slf4j.migrator.line.Log4jRuleSet;
@@ -162,11 +163,17 @@ public class Log4jRuleSetTest extends TestCase {
       .getOneLineReplacement("log.debug(\"blah=\" + blah);"));
     assertEquals("info call with two variables", "log.info(\"foo={} bar={}\", foo, bar);", log4jConverter
       .getOneLineReplacement("log.info(\"foo=\" + foo + \" bar=\" + bar);"));
+    assertEquals("info call with three variables", "log.info(\"foo={} bar={} info={}\", new Object[]{foo, bar, info});", log4jConverter
+      .getOneLineReplacement("log.info(\"foo=\" + foo + \" bar=\" + bar + \" info=\" + info);"));
     assertEquals("wrapped line", "log.debug(\"foo=\" + foo//TODO SLF4J Migrator unable to automatically change to parameterized logging", log4jConverter
       .getOneLineReplacement("log.debug(\"foo=\" + foo"));
     assertEquals("warn call with one variable and throwable", "log.warn(\"foo={}\", foo, e);", log4jConverter
       .getOneLineReplacement("log.warn(\"foo=\" + foo, e);"));
     assertEquals("error call with two variables and throwable", "log.warn(\"foo={} bar={}\", foo, bar, e);", log4jConverter
       .getOneLineReplacement("log.warn(\"foo=\" + foo + \" bar=\" + bar, e);"));
+
+    String unchangedMessage = "                    throw new Exception(\"Unable to match the SenderName and ReceiverName to the BuyerName and SellerName from partnership info provided in JMS header. SenderName=\"+senderName+\"/ReceiverName=\"+receiverName);";
+    assertEquals("Don't change exception", unchangedMessage, log4jConverter
+      .getOneLineReplacement(unchangedMessage));
   }
 }
-- 
1.8.1.msysgit.1

