View Javadoc

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  import java.net.InetAddress;
31  
32  /***
33   *  Test of ExternallyRolledFileAppender.
34   *
35   * @author Curt Arnold
36   */
37  public class ERFATestCase extends TestCase {
38  
39      /***
40       * Create new instance of test.
41       * @param name test name.
42       */
43    public ERFATestCase(final String name) {
44      super(name);
45    }
46  
47      /***
48       * Reset configuration after test.
49       */
50    public void tearDown() {
51        LogManager.resetConfiguration();
52    }
53  
54      /***
55       * Test ExternallyRolledFileAppender constructor.
56       */
57    public void testConstructor() {
58        ExternallyRolledFileAppender appender =
59                new ExternallyRolledFileAppender();
60        assertEquals(0, appender.getPort());
61    }
62  
63      /***
64       * Send a message to the ERFA.
65       * @param port port number.
66       * @param msg message, may not be null.
67       * @param expectedResponse expected response, may not be null.
68       * @throws IOException thrown on IO error.
69       */
70    void sendMessage(int port, final String msg, final String expectedResponse) throws IOException {
71        Socket socket = new Socket((String) null, port);
72        DataInputStream reader = new DataInputStream(socket.getInputStream());
73        DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
74        writer.writeUTF(msg);
75        String response = reader.readUTF();
76        assertEquals(expectedResponse, response);
77        reader.close();
78        writer.close();
79        socket.close();
80    }
81  
82      /***
83       * Test externally triggered rollover.
84       * @throws IOException thrown on IO error.
85       */
86    public void testRollover() throws IOException {
87        ExternallyRolledFileAppender erfa =
88                new ExternallyRolledFileAppender();
89  
90        int port = 5500;
91  
92        Logger logger = Logger.getLogger(RFATestCase.class);
93        Logger root = Logger.getRootLogger();
94        PatternLayout layout = new PatternLayout("%m\n");
95        erfa.setLayout(layout);
96        erfa.setAppend(false);
97        erfa.setMaxBackupIndex(2);
98        erfa.setPort(port);
99        erfa.setFile("output/ERFA-test2.log");
100       try {
101         erfa.activateOptions();
102       } catch(SecurityException ex) {
103           return;
104       }
105       try {
106          Thread.sleep(100);
107       } catch(InterruptedException ex) {
108       }
109       root.addAppender(erfa);
110 
111 
112       // Write exactly 10 bytes with each log
113       for (int i = 0; i < 55; i++) {
114         if (i < 10) {
115           logger.debug("Hello---" + i);
116         } else if (i < 100) {
117           logger.debug("Hello--" + i);
118         }
119         if ((i % 10) == 9) {
120             try {
121                 sendMessage(port, "RollOver", "OK");
122             } catch(SecurityException ex) {
123                 return;
124             }
125         }
126       }
127 
128       try {
129         sendMessage(port,
130               "That's all folks.",
131               "Expecting [RollOver] string.");
132       } catch(SecurityException ex) {
133           return;
134       }
135 
136 
137       assertTrue(new File("output/ERFA-test2.log").exists());
138       assertTrue(new File("output/ERFA-test2.log.1").exists());
139       assertTrue(new File("output/ERFA-test2.log.2").exists());
140       assertFalse(new File("output/ERFA-test2.log.3").exists());
141   }
142 }