1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.varia;
19  import junit.framework.TestCase;
20  import org.apache.log4j.LogManager;
21  import org.apache.log4j.Logger;
22  import org.apache.log4j.PatternLayout;
23  import org.apache.log4j.RFATestCase;
24  
25  import java.io.DataInputStream;
26  import java.io.DataOutputStream;
27  import java.io.File;
28  import java.io.IOException;
29  import java.net.Socket;
30  
31  /**
32   *  Test of ExternallyRolledFileAppender.
33   *
34   * @author Curt Arnold
35   */
36  public class ERFATestCase extends TestCase {
37  
38      /**
39       * Create new instance of test.
40       * @param name test name.
41       */
42    public ERFATestCase(final String name) {
43      super(name);
44    }
45  
46      /**
47       * Reset configuration after test.
48       */
49    public void tearDown() {
50        LogManager.resetConfiguration();
51    }
52  
53      /**
54       * Test ExternallyRolledFileAppender constructor.
55       */
56    public void testConstructor() {
57        ExternallyRolledFileAppender appender =
58                new ExternallyRolledFileAppender();
59        assertEquals(0, appender.getPort());
60    }
61  
62      /**
63       * Send a message to the ERFA.
64       * @param port port number.
65       * @param msg message, may not be null.
66       * @param expectedResponse expected response, may not be null.
67       * @throws IOException thrown on IO error.
68       */
69    void sendMessage(int port, final String msg, final String expectedResponse) throws IOException {
70        Socket socket = new Socket((String) null, port);
71        DataInputStream reader = new DataInputStream(socket.getInputStream());
72        DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
73        writer.writeUTF(msg);
74        String response = reader.readUTF();
75        assertEquals(expectedResponse, response);
76        reader.close();
77        writer.close();
78        socket.close();
79    }
80  
81      /**
82       * Test externally triggered rollover.
83       * @throws IOException thrown on IO error.
84       */
85    public void testRollover() throws IOException {
86        ExternallyRolledFileAppender erfa =
87                new ExternallyRolledFileAppender();
88  
89        int port = 5500;
90  
91        Logger logger = Logger.getLogger(RFATestCase.class);
92        Logger root = Logger.getRootLogger();
93        PatternLayout layout = new PatternLayout("%m\n");
94        erfa.setLayout(layout);
95        erfa.setAppend(false);
96        erfa.setMaxBackupIndex(2);
97        erfa.setPort(port);
98        erfa.setFile("output/ERFA-test2.log");
99        try {
100         erfa.activateOptions();
101       } catch(SecurityException ex) {
102           return;
103       }
104       try {
105          Thread.sleep(100);
106       } catch(InterruptedException ex) {
107       }
108       root.addAppender(erfa);
109 
110 
111       // Write exactly 10 bytes with each log
112       for (int i = 0; i < 55; i++) {
113         if (i < 10) {
114           logger.debug("Hello---" + i);
115         } else if (i < 100) {
116           logger.debug("Hello--" + i);
117         }
118         if ((i % 10) == 9) {
119             try {
120                 sendMessage(port, "RollOver", "OK");
121             } catch(SecurityException ex) {
122                 return;
123             }
124         }
125       }
126 
127       try {
128         sendMessage(port,
129               "That's all folks.",
130               "Expecting [RollOver] string.");
131       } catch(SecurityException ex) {
132           return;
133       }
134 
135 
136       assertTrue(new File("output/ERFA-test2.log").exists());
137       assertTrue(new File("output/ERFA-test2.log.1").exists());
138       assertTrue(new File("output/ERFA-test2.log.2").exists());
139       assertFalse(new File("output/ERFA-test2.log.3").exists());
140   }
141 }