001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.helpers; 015 016import ch.qos.logback.core.Context; 017import ch.qos.logback.core.ContextBase; 018import ch.qos.logback.core.rolling.helper.FileFilterUtil; 019import ch.qos.logback.core.rolling.helper.FileNamePattern; 020import org.junit.Test; 021 022import static org.junit.Assert.assertEquals; 023 024import java.io.File; 025import java.text.ParseException; 026import java.text.SimpleDateFormat; 027 028 029public class FileFilterUtilTest { 030 031 Context context = new ContextBase(); 032 033 // see also http://jira.qos.ch/browse/LBCORE-164 034 @Test 035 public void findHighestCounterTest() throws ParseException { 036 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", 037 "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", 038 "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", 039 "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", 040 "c:/log/debug-old-2010-08-10.9.log" }; 041 042 File[] matchingFileArray = new File[sa.length]; 043 for (int i = 0; i < sa.length; i++) { 044 matchingFileArray[i] = new File(sa[i]); 045 } 046 FileNamePattern fnp = new FileNamePattern("c:/log/debug-old-%d{yyyy-MM-dd}.%i.log", context); 047 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 048 String rexexp = null; 049 rexexp = fnp.toRegexForFixedDate(sdf.parse("2010-08-10")); 050 String stemRegex = FileFilterUtil.afterLastSlash(rexexp); 051 int result = FileFilterUtil.findHighestCounter(matchingFileArray, stemRegex); 052 assertEquals(12, result); 053 } 054}