001: package org.slf4j.migrator.helper;
002:
003: import org.slf4j.migrator.helper.Abbreviator;
004:
005: import junit.framework.TestCase;
006:
007: public class AbbreviatorTest extends TestCase {
008:
009: static final char FS = '/';
010: static final String INPUT_0 = "/abc/123456/ABC";
011: static final String INPUT_1 = "/abc/123456/xxxxx/ABC";
012:
013: RandomHelper rh = new RandomHelper(FS);
014:
015: public AbbreviatorTest(String arg0) {
016: super (arg0);
017: }
018:
019: protected void setUp() throws Exception {
020: super .setUp();
021: }
022:
023: protected void tearDown() throws Exception {
024: super .tearDown();
025: }
026:
027: public void testSmoke() {
028: {
029: Abbreviator abb = new Abbreviator(2, 100, FS);
030: String r = abb.abbreviate(INPUT_0);
031: assertEquals(INPUT_0, r);
032: }
033:
034: {
035: Abbreviator abb = new Abbreviator(3, 8, FS);
036: String r = abb.abbreviate(INPUT_0);
037: assertEquals("/abc/.../ABC", r);
038: }
039: {
040: Abbreviator abb = new Abbreviator(3, 8, FS);
041: String r = abb.abbreviate(INPUT_0);
042: assertEquals("/abc/.../ABC", r);
043: }
044: }
045:
046: public void testImpossibleToAbbreviate() {
047: Abbreviator abb = new Abbreviator(2, 20, FS);
048: String in = "iczldqwivpgm/mgrmvbjdxrwmqgprdjusth";
049: String r = abb.abbreviate(in);
050: assertEquals(in, r);
051: }
052:
053: public void testNoFS() {
054: Abbreviator abb = new Abbreviator(2, 100, FS);
055: String r = abb.abbreviate("hello");
056: assertEquals("hello", r);
057:
058: }
059:
060: public void testZeroPrefix() {
061: {
062: Abbreviator abb = new Abbreviator(0, 100, FS);
063: String r = abb.abbreviate(INPUT_0);
064: assertEquals(INPUT_0, r);
065: }
066: }
067:
068: public void testTheories() {
069: int MAX_RANDOM_FIXED_LEN = 20;
070: int MAX_RANDOM_AVG_LEN = 20;
071: int MAX_RANDOM_MAX_LEN = 100;
072: for (int i = 0; i < 10000; i++) {
073:
074: //System.out.println("Test number " + i);
075:
076: // 0 <= fixedLen < MAX_RANDOM_FIXED_LEN
077: int fixedLen = rh.nextInt(MAX_RANDOM_FIXED_LEN);
078: // 5 <= averageLen < MAX_RANDOM_AVG_LEN
079: int averageLen = rh.nextInt(MAX_RANDOM_AVG_LEN) + 3;
080: // System.out.println("fixedLen="+fixedLen+", averageLen="+averageLen);
081:
082: int maxLen = rh.nextInt(MAX_RANDOM_MAX_LEN) + fixedLen;
083: if (maxLen <= 1) {
084: continue;
085: }
086: // System.out.println("maxLen="+maxLen);
087: int targetLen = (maxLen / 2) + rh.nextInt(maxLen / 2) + 1;
088:
089: if (targetLen > maxLen) {
090: targetLen = maxLen;
091: }
092: String filename = rh
093: .buildRandomFileName(averageLen, maxLen);
094:
095: Abbreviator abb = new Abbreviator(fixedLen, targetLen, FS);
096: String result = abb.abbreviate(filename);
097: assertTheory0(averageLen, filename, result, fixedLen,
098: targetLen);
099: assertUsefulness(averageLen, filename, result, fixedLen,
100: targetLen);
101: assertTheory1(filename, result, fixedLen, targetLen);
102: assertTheory2(filename, result, fixedLen, targetLen);
103: }
104: }
105:
106: // result length is smaller than original length
107: void assertTheory0(int averageLen, String filename, String result,
108: int fixedLen, int targetLength) {
109: assertTrue("filename=[" + filename + "] result=[" + result
110: + "]", result.length() <= filename.length());
111: }
112:
113: // if conditions allow, result length should be to target length
114: void assertUsefulness(int averageLen, String filename,
115: String result, int fixedLen, int targetLength) {
116: int resLen = result.length();
117:
118: int margin = averageLen * 4;
119: if (targetLength > fixedLen + margin) {
120: assertTrue("filename=[" + filename + "], result=[" + result
121: + "] resultLength=" + resLen + " fixedLength="
122: + fixedLen + ", targetLength=" + targetLength
123: + ", avgLen=" + averageLen,
124: result.length() <= targetLength + averageLen);
125: }
126: }
127:
128: // result start with prefix found in filename
129: void assertTheory1(String filename, String result, int fixedLen,
130: int targetLength) {
131: String prefix = filename.substring(0, fixedLen);
132: assertTrue(result.startsWith(prefix));
133: }
134:
135: // The string /.../ is found in the result once at a position higher
136: // than fixedLen
137: void assertTheory2(String filename, String result, int fixedLen,
138: int targetLength) {
139: if (filename == result) {
140: return;
141: }
142: int fillerIndex = result.indexOf(Abbreviator.FILLER);
143: assertTrue(fillerIndex >= fixedLen);
144: }
145: }
|