1   package ch.qos.logback.classic.db;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import java.net.InetAddress;
6   import java.util.Random;
7   
8   import org.junit.After;
9   import org.junit.AfterClass;
10  import org.junit.Before;
11  import org.junit.BeforeClass;
12  import org.junit.Ignore;
13  import org.junit.Test;
14  import org.slf4j.Logger;
15  import org.slf4j.MDC;
16  
17  import ch.qos.logback.classic.LoggerContext;
18  import ch.qos.logback.classic.joran.JoranConfigurator;
19  import ch.qos.logback.core.joran.spi.JoranException;
20  import ch.qos.logback.core.status.Status;
21  import ch.qos.logback.core.testUtil.Env;
22  import ch.qos.logback.core.util.StatusPrinter;
23  
24  public class DBAppenderIntegrationTest {
25  
26    static String LOCAL_HOST_NAME;
27    static String[] CONFORMING_HOST_LIST = new String[] { "Orion" };
28  
29    int diff = new Random(System.nanoTime()).nextInt(10000);
30    LoggerContext lc = new LoggerContext();
31    
32    @BeforeClass
33    public static void setUpBeforeClass() throws Exception {
34      InetAddress localhostIA = InetAddress.getLocalHost();
35      LOCAL_HOST_NAME = localhostIA.getHostName();
36    }
37  
38    @AfterClass
39    public static void tearDownAfterClass() throws Exception {
40    }
41  
42    @Before
43    public void setUp() throws Exception {
44      lc.setName("lc"+diff);
45    }
46  
47    @After
48    public void tearDown() throws Exception {
49      // lc will never be used again
50      lc.stop();
51    }
52  
53    public void doTest(String configFile) throws JoranException {
54      JoranConfigurator configurator = new JoranConfigurator();
55      configurator.setContext(lc);
56      configurator.doConfigure(configFile);
57  
58      Logger logger = lc.getLogger(DBAppenderIntegrationTest.class);
59  
60      MDC.put("userid", "user" + diff);
61      int runLength = 5;
62      for (int i = 1; i <= runLength; i++) {
63        logger.debug("This is a debug message. Message number: " + i);
64      }
65      logger.error("At last an error.", new Exception("Just testing"));
66      
67      // check that there were no errors
68      StatusPrinter.print(lc);
69      assertEquals(Status.INFO, lc.getStatusManager().getLevel());
70      
71    }
72    
73    static boolean isConformingHostAndJDK16OrHigher() {
74      if(!Env.isJDK6OrHigher()) {
75        return false;
76      }
77      for (String conformingHost : CONFORMING_HOST_LIST) {
78        if (conformingHost.equalsIgnoreCase(LOCAL_HOST_NAME)) {
79          return true;
80        }
81      }
82      return false;
83    }
84  
85    @Test
86    public void sqlserver() throws Exception {
87      // perform test only on conforming hosts
88      if (!isConformingHostAndJDK16OrHigher()) {
89        return;
90      }
91      doTest("src/test/input/integration/db/sqlserver-with-driver.xml");
92    }
93  
94    @Test
95    public void oracle10g() throws Exception {
96      // perform test only on conforming hosts
97      if (!isConformingHostAndJDK16OrHigher()) {
98        return;
99      }
100     doTest("src/test/input/integration/db/oracle10g-with-driver.xml");
101   }
102 
103   @Test
104   @Ignore
105   public void oracle11g() throws Exception {
106     // perform test only on conforming hosts
107     if (!isConformingHostAndJDK16OrHigher()) {
108       return;
109     }
110     doTest("src/test/input/integration/db/oracle11g-with-driver.xml");
111   }
112   
113   @Test
114   public void mysql() throws Exception {
115     // perform test only on conforming hosts
116     if (!isConformingHostAndJDK16OrHigher()) {
117       return;
118     }
119     doTest("src/test/input/integration/db/mysql-with-driver.xml");
120   }
121   
122   @Test
123   public void postgres() throws Exception {
124     // perform test only on conforming hosts
125     if (!isConformingHostAndJDK16OrHigher()) {
126       return;
127     }
128     doTest("src/test/input/integration/db/postgresql-with-driver.xml");
129   }
130   
131 }