aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/test/java/com/yahoo/log/LogMessageTestCase.java
blob: da8162e9ad14e8819687e16562245bc592d0d71d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import com.yahoo.log.event.Event;
import com.yahoo.log.event.MalformedEventException;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Unit tests for the LogMessage class.
 *
 * @author Bjorn Borud
 * @author bjorncs
 */
public class LogMessageTestCase {

    @Test
    public void testLogParsing () throws IOException, InvalidLogFormatException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(LogMessageTestCase.class.getResourceAsStream("/logEntries.txt")))) {
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                LogMessage.parseNativeFormat(line);
            }
        }
    }

    /**
     * Read in some events and make sure we are able to identify
     * them as such.
     */
    @Test
    public void testEvents () throws IOException, InvalidLogFormatException, MalformedEventException {
        try (BufferedReader br =
                     new BufferedReader(
                             new InputStreamReader(
                                     LogMessageTestCase.class.getResourceAsStream("/event.txt")))) {
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                LogMessage m = LogMessage.parseNativeFormat(line);
                Event event = m.getEvent();
                assertNotNull(event);
            }
        }

    }

    @Test
    public void testParsingTimestampAndRendering() throws InvalidLogFormatException {
        {
            LogMessage message = LogMessage.parseNativeFormat("1096639280.524133935\tmalfunction\t26851\t-\tlogtest\tinfo\tStarting up, called as ./log/logtest");
            assertEquals(1096639280L, message.getTimestamp().getEpochSecond());
            assertEquals(524133935L, message.getTimestamp().getNano());
            assertEquals("1096639280.524133\tmalfunction\t26851\t-\tlogtest\tinfo\tStarting up, called as ./log/logtest\n", message.toString());
        }
        {
            LogMessage message = LogMessage.parseNativeFormat("1096639280.524\tmalfunction\t26851\t-\tlogtest\tinfo\tbackslash: \\\\");
            assertEquals(1096639280L, message.getTimestamp().getEpochSecond());
            assertEquals(524_000_000L, message.getTimestamp().getNano());
            assertEquals("1096639280.524000\tmalfunction\t26851\t-\tlogtest\tinfo\tbackslash: \\\\\n", message.toString());
        }
    }
}