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.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Test;
22  
23  import java.io.File;
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  
27  public class FileFilterUtilTest {
28  
29      Context context = new ContextBase();
30  
31      // see also http://jira.qos.ch/browse/LBCORE-164
32      @Test
33      public void findHighestCounterTest() throws ParseException {
34          String[] sa = new String[] { "c:/log/debug-old-2010-08-10.0.log", "c:/log/debug-old-2010-08-10.1.log",
35                  "c:/log/debug-old-2010-08-10.10.log", "c:/log/debug-old-2010-08-10.11.log",
36                  "c:/log/debug-old-2010-08-10.12.log", "c:/log/debug-old-2010-08-10.2.log",
37                  "c:/log/debug-old-2010-08-10.3.log", "c:/log/debug-old-2010-08-10.4.log",
38                  "c:/log/debug-old-2010-08-10.5.log", "c:/log/debug-old-2010-08-10.6.log",
39                  "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          Assertions.assertEquals(12, result);
53      }
54  }