View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.helpers;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.rolling.helper.FileFilterUtil;
19  import ch.qos.logback.core.rolling.helper.FileNamePattern;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import java.io.File;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  
28  
29  public class FileFilterUtilTest {
30  
31      Context context = new ContextBase();
32  
33      // see also http://jira.qos.ch/browse/LBCORE-164
34      @Test
35      public void findHighestCounterTest() throws ParseException {
36          String[] sa = new String[] { "c:/log/debug-old-2010-08-10.0.log", "c:/log/debug-old-2010-08-10.1.log", "c:/log/debug-old-2010-08-10.10.log",
37                  "c:/log/debug-old-2010-08-10.11.log", "c:/log/debug-old-2010-08-10.12.log", "c:/log/debug-old-2010-08-10.2.log",
38                  "c:/log/debug-old-2010-08-10.3.log", "c:/log/debug-old-2010-08-10.4.log", "c:/log/debug-old-2010-08-10.5.log",
39                  "c:/log/debug-old-2010-08-10.6.log", "c:/log/debug-old-2010-08-10.7.log", "c:/log/debug-old-2010-08-10.8.log",
40                  "c:/log/debug-old-2010-08-10.9.log" };
41  
42          File[] matchingFileArray = new File[sa.length];
43          for (int i = 0; i < sa.length; i++) {
44              matchingFileArray[i] = new File(sa[i]);
45          }
46          FileNamePattern fnp = new FileNamePattern("c:/log/debug-old-%d{yyyy-MM-dd}.%i.log", context);
47          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
48          String rexexp = null;
49          rexexp = fnp.toRegexForFixedDate(sdf.parse("2010-08-10"));
50          String stemRegex = FileFilterUtil.afterLastSlash(rexexp);
51          int result = FileFilterUtil.findHighestCounter(matchingFileArray, stemRegex);
52          assertEquals(12, result);
53      }
54  }