1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package ch.qos.logback.core.rolling;
18  
19  import static org.junit.Assert.assertTrue;
20  import static org.junit.Assert.fail;
21  
22  import java.io.File;
23  
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import ch.qos.logback.core.Context;
29  import ch.qos.logback.core.ContextBase;
30  import ch.qos.logback.core.Layout;
31  import ch.qos.logback.core.layout.DummyLayout;
32  import ch.qos.logback.core.testUtil.Env;
33  import ch.qos.logback.core.util.Compare;
34  import ch.qos.logback.core.util.Constants;
35  
36  /**
37   * 
38   * Do not forget to call start() when configuring programatically.
39   * 
40   * @author Ceki Gülcü
41   * @author Sébastien Pennec
42   * 
43   */
44  public class SizeBasedRollingTest  {
45  
46  
47    @Before
48    public void setUp() {
49      {
50        File target = new File(Constants.OUTPUT_DIR_PREFIX
51            + "sizeBased-test2.log");
52        target.mkdirs();
53        target.delete();
54      }
55      {
56        File target = new File(Constants.OUTPUT_DIR_PREFIX + "sbr-test3.log");
57        target.mkdirs();
58        target.delete();
59      }
60    }
61  
62    @After
63    public void tearDown() {
64    }
65  
66    /**
67     * Test whether FixedWindowRollingPolicy throws an exception when the
68     * ActiveFileName is not set.
69     */
70    @Test
71    public void test1() throws Exception {
72      // We purposefully use the \n as the line separator.
73      // This makes the regression test system independent.
74      Context context = new ContextBase();
75      Layout<Object> layout = new DummyLayout<Object>();
76      RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
77      rfa.setLayout(layout);
78      rfa.setContext(new ContextBase());
79  
80      FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy();
81      fwrp.setContext(context);
82      fwrp.setParent(rfa);
83      SizeBasedTriggeringPolicy sbtp = new SizeBasedTriggeringPolicy();
84      sbtp.setContext(context);
85  
86      sbtp.setMaxFileSize("100");
87      sbtp.start();
88      fwrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX
89          + "sizeBased-test1.%i");
90      try {
91        fwrp.start();
92        fail("The absence of activeFileName option should have caused an exception.");
93      } catch (IllegalStateException e) {
94        return;
95      }
96  
97      // StatusPrinter.print(context.getStatusManager());
98    }
99  
100   /**
101    * Test basic rolling functionality.
102    */
103   @Test
104   public void test2() throws Exception {
105     Context context = new ContextBase();
106 
107     DummyLayout<Object> layout = new DummyLayout<Object>("0123456789");
108     RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
109     rfa.setName("ROLLING");
110     rfa.setLayout(layout);
111     rfa.setContext(context);
112     rfa.setFile(Constants.OUTPUT_DIR_PREFIX
113         + "sizeBased-test2.log");
114     
115     FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy();
116     swrp.setContext(context);
117     SizeBasedTriggeringPolicy<Object> sbtp = new SizeBasedTriggeringPolicy<Object>();
118     sbtp.setContext(context);
119 
120     sbtp.setMaxFileSize("100");
121     swrp.setMinIndex(0);
122 //    swrp.setActiveFileName(Constants.TEST_DIR_PREFIX
123 //        + "output/sizeBased-test2.log");
124 
125     swrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX
126         + "sizeBased-test2.%i");
127     swrp.setParent(rfa);
128     swrp.start();
129 
130     rfa.setRollingPolicy(swrp);
131     rfa.setTriggeringPolicy(sbtp);
132     rfa.start();
133 
134     // Write exactly 10 bytes with each log
135     // for (int i = 0; i < 25; i++) {
136     // Thread.sleep(100);
137     // if (i < 10) {
138     // rfa.doAppend("Hello---" + i);
139     // //logger.debug("Hello---" + i);
140     // } else if (i < 100) {
141     // rfa.doAppend("Hello---" + i);
142     // //logger.debug("Hello--" + i);
143     // }
144     // }
145 
146     for (int i = 0; i < 45; i++) {
147       Thread.sleep(10);
148       rfa.doAppend("hello");
149     }
150 
151     assertTrue(new File(Constants.OUTPUT_DIR_PREFIX
152         + "sizeBased-test2.log").exists());
153     assertTrue(new File(Constants.OUTPUT_DIR_PREFIX + "sizeBased-test2.0")
154         .exists());
155     assertTrue(new File(Constants.OUTPUT_DIR_PREFIX + "sizeBased-test2.1")
156         .exists());
157 
158     // The File.length() method is not accurate under Windows
159 
160     if (!Env.isWindows()) {
161 
162       assertTrue(Compare.compare(Constants.OUTPUT_DIR_PREFIX
163           + "sizeBased-test2.log", Constants.TEST_DIR_PREFIX
164           + "witness/rolling/sbr-test2.l"));
165       assertTrue(Compare.compare(Constants.OUTPUT_DIR_PREFIX
166           + "sizeBased-test2.0", Constants.TEST_DIR_PREFIX
167           + "witness/rolling/sbr-test2.0"));
168       assertTrue(Compare.compare(Constants.OUTPUT_DIR_PREFIX
169           + "sizeBased-test2.1", Constants.TEST_DIR_PREFIX
170           + "witness/rolling/sbr-test2.1"));
171     }
172 
173     // StatusPrinter.print(context.getStatusManager());
174   }
175 
176   /**
177    * Same as testBasic but also with GZ compression.
178    */
179   @Test
180   public void test3() throws Exception {
181     Context context = new ContextBase();
182     DummyLayout<Object> layout = new DummyLayout<Object>("0123456789");
183     RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
184     rfa.setLayout(layout);
185     rfa.setContext(context);
186     rfa.setFile(Constants.OUTPUT_DIR_PREFIX + "sbr-test3.log");
187 
188     FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy();
189     fwrp.setContext(context);
190     SizeBasedTriggeringPolicy<Object> sbtp = new SizeBasedTriggeringPolicy<Object>();
191     sbtp.setContext(context);
192 
193     sbtp.setMaxFileSize("100");
194     fwrp.setMinIndex(0);
195     //fwrp.setActiveFileName(Constants.TEST_DIR_PREFIX + "output/sbr-test3.log");
196     fwrp.setFileNamePattern(Constants.OUTPUT_DIR_PREFIX
197         + "sbr-test3.%i.gz");
198     fwrp.setParent(rfa);
199     fwrp.start();
200     rfa.setRollingPolicy(fwrp);
201     rfa.setTriggeringPolicy(sbtp);
202     rfa.start();
203 
204     // Write exactly 10 bytes with each log
205     // for (int i = 0; i < 25; i++) {
206     // Thread.sleep(100);
207     // if (i < 10) {
208     // rfa.doAppend("Hello---" + i);
209     // //logger.debug("Hello---" + i);
210     // } else if (i < 100) {
211     // rfa.doAppend("Hello---" + i);
212     // //logger.debug("Hello--" + i);
213     // }
214     // }
215 
216     for (int i = 0; i < 25; i++) {
217       Thread.sleep(10);
218       rfa.doAppend("hello");
219     }
220 
221     assertTrue(new File(Constants.OUTPUT_DIR_PREFIX + "sbr-test3.log")
222         .exists());
223     assertTrue(new File(Constants.OUTPUT_DIR_PREFIX + "sbr-test3.0.gz")
224         .exists());
225     assertTrue(new File(Constants.OUTPUT_DIR_PREFIX + "sbr-test3.1.gz")
226         .exists());
227 
228     if (!Env.isWindows()) {
229 
230       assertTrue(Compare.compare(
231           Constants.OUTPUT_DIR_PREFIX+"sbr-test3.log",
232           Constants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.l"));
233       assertTrue(Compare.gzCompare(
234           Constants.OUTPUT_DIR_PREFIX+"sbr-test3.0.gz",
235           Constants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.0.gz"));
236       assertTrue(Compare.gzCompare(
237           Constants.OUTPUT_DIR_PREFIX+"sbr-test3.1.gz",
238           Constants.TEST_DIR_PREFIX + "witness/rolling/sbr-test3.1.gz"));
239     }
240 
241     // StatusPrinter.print(context.getStatusManager());
242   }
243 
244 
245 }