UtilTest.java 2.2 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package hudson;

import junit.framework.TestCase;

import java.util.Map;
import java.util.HashMap;

/**
 * @author Kohsuke Kawaguchi
 */
public class UtilTest extends TestCase {
    public void testReplaceMacro() {
        Map<String,String> m = new HashMap<String,String>();
        m.put("A","a");
        m.put("AA","aa");
        m.put("B","B");

        // longest match
        assertEquals("aa",Util.replaceMacro("$AA",m));

        // invalid keys are ignored
        assertEquals("$AAB",Util.replaceMacro("$AAB",m));
23

24 25 26 27 28
        assertEquals("aaB",Util.replaceMacro("${AA}B",m));
        assertEquals("${AAB}",Util.replaceMacro("${AAB}",m));

    	// test that more complex scenarios work
	    assertEquals("/a/B/aa", Util.replaceMacro("/$A/$B/$AA",m));
29 30
        assertEquals("a-aa", Util.replaceMacro("$A-$AA",m));
        assertEquals("/a/foo/can/B/you-believe_aa~it?", Util.replaceMacro("/$A/foo/can/$B/you-believe_$AA~it?",m));
K
kohsuke 已提交
31
    }
32 33 34 35 36 37 38 39


    public void testTimeSpanString() {
        // Check that amounts less than 365 days are not rounded up to a whole year.
        // In the previous implementation there were 360 days in a year.
        // We're still working on the assumption that a month is 30 days, so there will
        // be 5 days at the end of the year that will be "12 months" but not "1 year".
        // First check 359 days.
C
cactusman 已提交
40
        assertEquals(Messages.Util_month(11), Util.getTimeSpanString(31017600000L));
41
        // And 362 days.
C
cactusman 已提交
42
        assertEquals(Messages.Util_month(12), Util.getTimeSpanString(31276800000L));
43 44

        // 11.25 years - Check that if the first unit has 2 or more digits, a second unit isn't used.
C
cactusman 已提交
45
        assertEquals(Messages.Util_year(11), Util.getTimeSpanString(354780000000L));
46
        // 9.25 years - Check that if the first unit has only 1 digit, a second unit is used.
C
cactusman 已提交
47
        assertEquals(Messages.Util_year(9)+ " " + Messages.Util_month(3), Util.getTimeSpanString(291708000000L));
48
        // 67 seconds
C
cactusman 已提交
49
        assertEquals(Messages.Util_minute(1) + " " + Messages.Util_second(7), Util.getTimeSpanString(67000L));
50
        // 17 seconds - Check that times less than a minute only use seconds.
C
cactusman 已提交
51
        assertEquals(Messages.Util_second(17), Util.getTimeSpanString(17000L));
52
    }
C
cactusman 已提交
53

K
kohsuke 已提交
54
}