Skip to content

Commit

Permalink
fix LOGBACK-971 CharSequenceToRegexMapper#findMinMaxLengthsInSymbols(…
Browse files Browse the repository at this point in the history
…) always returns min=0
  • Loading branch information
ceki committed Apr 9, 2014
1 parent 1561d00 commit 469e07b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
Expand Up @@ -110,11 +110,14 @@ private String symbolArrayToRegex(String[] symbolArray) {
return ".{" + minMax[0] + "," + minMax[1] + "}";
}

private int[] findMinMaxLengthsInSymbols(String[] symbols) {
static int[] findMinMaxLengthsInSymbols(String[] symbols) {
int min = Integer.MAX_VALUE;
int max = 0;
for (String symbol : symbols) {
int len = symbol.length();
// some SENTINEL values can be empty strings, the month at index 12 or the weekday at index 0
if(len == 0)
continue;
min = Math.min(min, len);
max = Math.max(max, len);
}
Expand Down
@@ -0,0 +1,72 @@
package ch.qos.logback.core.util;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;

import java.text.DateFormatSymbols;
import java.util.Locale;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;


public class CharSequenceToRegexMapperTest {
static Locale KO_LOCALE = new Locale("ko", "KR");
Locale oldLocale = Locale.getDefault();

@After
public void tearDown() {
Locale.setDefault(oldLocale);
}

@Test
public void findMinMaxLengthsInSymbolsWithTrivialInputs() {
String[] symbols = new String[]{"a", "bb"};
int[] results = CharSequenceToRegexMapper.findMinMaxLengthsInSymbols(symbols);
assertEquals(1, results[0]);
assertEquals(2, results[1]);
}

@Test
public void emptyStringValuesShouldBeIgnoredByFindMinMaxLengthsInSymbols() {
String[] symbols = new String[]{"aaa", ""};
int[] results = CharSequenceToRegexMapper.findMinMaxLengthsInSymbols(symbols);
assertEquals(3, results[0]);
assertEquals(3, results[1]);
}


@Test
@Ignore
public void noneOfTheSymbolsAreOfZeroLengthForKorean() {
Locale.setDefault(KO_LOCALE);
noneOfTheSymbolsAreOfZeroLength();
}

@Test
@Ignore
public void noneOfTheSymbolsAreOfZeroLengthForSwiss() {
Locale.setDefault(new Locale("fr", "CH"));
noneOfTheSymbolsAreOfZeroLength();
}

private void noneOfTheSymbolsAreOfZeroLength() {
DateFormatSymbols dateFormatSymbols = DateFormatSymbols.getInstance();
//checkEmptyString(dateFormatSymbols.getShortMonths(), "ShortMonths");
//checkEmptyString(dateFormatSymbols.getMonths(), "Months");
checkEmptyString(dateFormatSymbols.getShortWeekdays(), "ShortWeekdays");
checkEmptyString(dateFormatSymbols.getWeekdays(), "Weekdays");
checkEmptyString(dateFormatSymbols.getAmPmStrings(), "AmPmStrings");

}

private void checkEmptyString(String[] symbolArray, String category) {
for (String s : symbolArray) {
System.out.println(category + " [" + s + "]");
assertTrue(category + " contains empty strings", s.length() > 0);
}
}


}
3 changes: 2 additions & 1 deletion logback-core/src/test/java/ch/qos/logback/core/util/PackageTest.java 100644 → 100755
Expand Up @@ -25,6 +25,7 @@
OptionHelperTest.class,
StatusPrinterTest.class,
TimeUtilTest.class,
ContentTypeUtilTest.class})
ContentTypeUtilTest.class,
CharSequenceToRegexMapperTest.class})
public class PackageTest {
}

0 comments on commit 469e07b

Please sign in to comment.