001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.db;
015
016import static org.junit.Assert.assertEquals;
017
018import java.util.Random;
019
020import ch.qos.logback.access.spi.IAccessEvent;
021import ch.qos.logback.core.testUtil.EnvUtilForTests;
022import ch.qos.logback.core.testUtil.StatusChecker;
023import ch.qos.logback.core.util.EnvUtil;
024import org.junit.After;
025import org.junit.AfterClass;
026import org.junit.Before;
027import org.junit.BeforeClass;
028import org.junit.Ignore;
029import org.junit.Test;
030
031import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
032import ch.qos.logback.access.joran.JoranConfigurator;
033import ch.qos.logback.access.spi.AccessContext;
034import ch.qos.logback.core.Appender;
035import ch.qos.logback.core.joran.spi.JoranException;
036import ch.qos.logback.core.status.Status;
037import ch.qos.logback.core.util.StatusPrinter;
038
039public class DBAppenderIntegrationTest {
040
041    static String LOCAL_HOST_NAME = EnvUtilForTests.getLocalHostName();
042    static String[] CONFORMING_HOST_LIST = new String[] { "Orion" };
043
044    int diff = new Random(System.nanoTime()).nextInt(10000);
045    AccessContext context = new AccessContext();
046    StatusChecker statusChecker = new StatusChecker(context);
047
048    @BeforeClass
049    public static void setUpBeforeClass() throws Exception {
050    }
051
052    @AfterClass
053    public static void tearDownAfterClass() throws Exception {
054    }
055
056    @Before
057    public void setUp() throws Exception {
058    }
059
060    @After
061    public void tearDown() throws Exception {
062
063    }
064
065    public void doTest(String configFile) throws JoranException {
066        JoranConfigurator configurator = new JoranConfigurator();
067        configurator.setContext(context);
068        configurator.doConfigure(configFile);
069
070        Appender<IAccessEvent> appender = context.getAppender("DB");
071
072        for (int i = 0; i < 10; i++) {
073            IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
074            appender.doAppend(event);
075        }
076
077        StatusPrinter.print(context);
078
079        // check that there were no errors
080        assertEquals(Status.INFO, statusChecker.getHighestLevel(0));
081
082    }
083
084    static boolean isConformingHostAndJDK16OrHigher() {
085        if (!EnvUtil.isJDK6OrHigher()) {
086            return false;
087        }
088        return EnvUtilForTests.isLocalHostNameInList(CONFORMING_HOST_LIST);
089    }
090
091    @Test
092    public void sqlserver() throws Exception {
093        // perform test only on conforming hosts
094        if (!isConformingHostAndJDK16OrHigher()) {
095            return;
096        }
097        doTest("src/test/input/integration/db/sqlserver-with-driver.xml");
098    }
099
100    @Test
101    @Ignore
102    public void oracle10g() throws Exception {
103        // perform test only on conforming hosts
104        if (!isConformingHostAndJDK16OrHigher()) {
105            return;
106        }
107        doTest("src/test/input/integration/db/oracle10g-with-driver.xml");
108    }
109
110    @Test
111    @Ignore
112    public void oracle11g() throws Exception {
113        // perform test only on conforming hosts
114        if (!isConformingHostAndJDK16OrHigher()) {
115            return;
116        }
117        doTest("src/test/input/integration/db/oracle11g-with-driver.xml");
118    }
119
120    @Test
121    public void mysql() throws Exception {
122        // perform test only on conforming hosts
123        if (!isConformingHostAndJDK16OrHigher()) {
124            return;
125        }
126        doTest("src/test/input/integration/db/mysql-with-driver.xml");
127    }
128
129    @Test
130    public void postgres() throws Exception {
131        // perform test only on conforming hosts
132        if (!isConformingHostAndJDK16OrHigher()) {
133            return;
134        }
135        doTest("src/test/input/integration/db/postgresql-with-driver.xml");
136    }
137
138}