1   package ch.qos.logback.access.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  
15  import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
16  import ch.qos.logback.access.joran.JoranConfigurator;
17  import ch.qos.logback.access.spi.AccessContext;
18  import ch.qos.logback.access.spi.AccessEvent;
19  import ch.qos.logback.core.Appender;
20  import ch.qos.logback.core.joran.spi.JoranException;
21  import ch.qos.logback.core.status.Status;
22  import ch.qos.logback.core.testUtil.Env;
23  import ch.qos.logback.core.util.StatusPrinter;
24  
25  public class DBAppenderIntegrationTest {
26  
27    static String LOCAL_HOST_NAME;
28    static String[] CONFORMING_HOST_LIST = new String[] { "Orion" };
29  
30    int diff = new Random(System.nanoTime()).nextInt(10000);
31    AccessContext context = new AccessContext();
32    
33    @BeforeClass
34    public static void setUpBeforeClass() throws Exception {
35      InetAddress localhostIA = InetAddress.getLocalHost();
36      LOCAL_HOST_NAME = localhostIA.getHostName();
37    }
38  
39    @AfterClass
40    public static void tearDownAfterClass() throws Exception {
41    }
42  
43    @Before
44    public void setUp() throws Exception {
45    }
46  
47    @After
48    public void tearDown() throws Exception {
49  
50    }
51  
52    public void doTest(String configFile) throws JoranException {
53      JoranConfigurator configurator = new JoranConfigurator();
54      configurator.setContext(context);
55      configurator.doConfigure(configFile);
56  
57      Appender<AccessEvent> appender = context.getAppender("DB");
58      
59      for (int i = 0; i < 10; i++) {
60        AccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
61        appender.doAppend(event);
62      }
63      
64      StatusPrinter.print(context);
65      
66      // check that there were no errors
67      assertEquals(Status.INFO,  context.getStatusManager().getLevel());
68      
69    }
70    
71    static boolean isConformingHostAndJDK16OrHigher() {
72      if(!Env.isJDK6OrHigher()) {
73        return false;
74      }
75      for (String conformingHost : CONFORMING_HOST_LIST) {
76        if (conformingHost.equalsIgnoreCase(LOCAL_HOST_NAME)) {
77          return true;
78        }
79      }
80      return false;
81    }
82  
83    @Test
84    public void sqlserver() throws Exception {
85      // perform test only on conforming hosts
86      if (!isConformingHostAndJDK16OrHigher()) {
87        return;
88      }
89      doTest("src/test/input/integration/db/sqlserver-with-driver.xml");
90    }
91  
92    @Test
93    public void oracle10g() throws Exception {
94      // perform test only on conforming hosts
95      if (!isConformingHostAndJDK16OrHigher()) {
96        return;
97      }
98      doTest("src/test/input/integration/db/oracle10g-with-driver.xml");
99    }
100 
101   @Test
102   @Ignore
103   public void oracle11g() throws Exception {
104     // perform test only on conforming hosts
105     if (!isConformingHostAndJDK16OrHigher()) {
106       return;
107     }
108     doTest("src/test/input/integration/db/oracle11g-with-driver.xml");
109   }
110   
111   @Test
112   public void mysql() throws Exception {
113     // perform test only on conforming hosts
114     if (!isConformingHostAndJDK16OrHigher()) {
115       return;
116     }
117     doTest("src/test/input/integration/db/mysql-with-driver.xml");
118   }
119   
120   @Test
121   public void postgres() throws Exception {
122     // perform test only on conforming hosts
123     if (!isConformingHostAndJDK16OrHigher()) {
124       return;
125     }
126     doTest("src/test/input/integration/db/postgresql-with-driver.xml");
127   }
128   
129 }