LogAnalyzerTest.java Source code

Java tutorial

Introduction

Here is the source code for LogAnalyzerTest.java

Source

/*
 * Java-based syslog server and log search engine
 * Copyright (C) 2011 Henning Peters <pete@dexterslab.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import junit.framework.Assert;
import junit.framework.TestCase;

import java.io.StringReader;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;

public class LogAnalyzerTest extends TestCase {

    private LogAnalyzer analyzer;

    public void assertAnalyzesTo(String input, String[] output) throws Exception {
        System.out.println(input);
        AnalyzerUtils.displayTokensWithFullDetails(analyzer, input);

        TokenStream stream = analyzer.tokenStream("field", new StringReader(input));
        TermAttribute termAttr = stream.addAttribute(TermAttribute.class);
        for (String expected : output) {
            Assert.assertTrue(stream.incrementToken());
            Assert.assertEquals(expected, termAttr.term());
        }
        Assert.assertFalse(stream.incrementToken());
        stream.close();
    }

    protected void setUp() throws Exception {
        analyzer = new LogAnalyzer();
    }

    public void testSingleWord() throws Exception {
        assertAnalyzesTo("test", new String[] { "test" });
    }

    public void testMultipleWords() throws Exception {
        assertAnalyzesTo("this is a test", new String[] { "this", "is", "a", "test" });
    }

    public void testProcessID() throws Exception {
        assertAnalyzesTo("vsftpd[5262]: hello", new String[] { "vsftpd[5262]:", "5262", "vsftpd", "hello" });
    }

    public void testIPAddress() throws Exception {
        assertAnalyzesTo("message from 127.0.0.1",
                new String[] { "message", "from", "127.0.0.1", "1", "0", "0", "127" });
    }

    public void testBrackets() throws Exception {
        assertAnalyzesTo("[debug]='test'", new String[] { "[debug]='test'", "test", "debug" });
    }

    public void testNumber() throws Exception {
        assertAnalyzesTo("12345", new String[] { "12345" });
    }
}