1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2009, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.rolling.helper;
11  
12  import static org.junit.Assert.*;
13  
14  import java.util.Calendar;
15  
16  import org.junit.Test;
17  
18  
19  import ch.qos.logback.core.Context;
20  import ch.qos.logback.core.ContextBase;
21  
22  /**
23   * @author Ceki
24   * 
25   */
26  public class FileNamePatternTest {
27  
28    Context context = new ContextBase();
29  
30    @Test
31    public void testSmoke() {
32      // System.out.println("Testing [t]");
33      FileNamePattern pp = new FileNamePattern("t", context);
34      assertEquals("t", pp.convertInt(3));
35  
36      // System.out.println("Testing [foo]");
37      pp = new FileNamePattern("foo", context);
38      assertEquals("foo", pp.convertInt(3));
39  
40      // System.out.println("Testing [foo%]");
41      // pp = new FileNamePattern("foo%", context);
42      // StatusPrinter.print(context.getStatusManager());
43      // assertEquals("foo%", pp.convertInt(3));
44  
45      pp = new FileNamePattern("%i foo", context);
46      
47      assertEquals("3 foo", pp.convertInt(3));
48  
49      pp = new FileNamePattern("foo%i.xixo", context);
50      assertEquals("foo3.xixo", pp.convertInt(3));
51  
52      pp = new FileNamePattern("foo%i.log", context);
53      assertEquals("foo3.log", pp.convertInt(3));
54  
55      pp = new FileNamePattern("foo.%i.log", context);
56      assertEquals("foo.3.log", pp.convertInt(3));
57  
58      pp = new FileNamePattern("%i.foo\\%", context);
59      assertEquals("3.foo%", pp.convertInt(3));
60  
61      pp = new FileNamePattern("\\%foo", context);
62      assertEquals("%foo", pp.convertInt(3));
63    }
64  
65    @Test
66    // test ways for dealing with flowing i converter, as in "foo%ix"
67    public void testFlowingI() {
68      // System.out.println("Testing [foo%ibar%i]");
69  
70      {
71        FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
72        assertEquals("foo3bar3", pp.convertInt(3));
73      }
74      {
75        FileNamePattern pp = new FileNamePattern("foo%i{}bar%i", context);
76        assertEquals("foo3bar3", pp.convertInt(3));
77      }
78    }
79  
80    @Test
81    public void testDate() {
82      Calendar cal = Calendar.getInstance();
83      cal.set(2003, 4, 20, 17, 55);
84  
85      FileNamePattern pp = new FileNamePattern("foo%d{yyyy.MM.dd}", context);
86      
87      assertEquals("foo2003.05.20", pp.convertDate(cal.getTime()));
88  
89      pp = new FileNamePattern("foo%d{yyyy.MM.dd HH:mm}", context);
90      assertEquals("foo2003.05.20 17:55", pp.convertDate(cal.getTime()));
91  
92      pp = new FileNamePattern("%d{yyyy.MM.dd HH:mm} foo", context);
93      assertEquals("2003.05.20 17:55 foo", pp.convertDate(cal.getTime()));
94  
95    }
96  
97    @Test
98    public void testWithBackslash() {
99      FileNamePattern pp = new FileNamePattern("c:\\foo\\bar.%i", context);
100     assertEquals("c:\\foo\\bar.3", pp.convertInt(3));
101   }
102 
103 
104 }