View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.db;
15  
16  import static org.assertj.core.api.Assertions.assertThat;
17  
18  import org.junit.Test;
19  
20  import ch.qos.logback.classic.db.names.DBNameResolver;
21  import ch.qos.logback.classic.db.names.DefaultDBNameResolver;
22  import ch.qos.logback.classic.db.names.SimpleDBNameResolver;
23  
24  /**
25   * @author Tomasz Nurkiewicz
26   * @since 2010-03-22
27   */
28  public class SQLBuilderTest {
29  
30      @Test
31      public void shouldReturnDefaultSqlInsertLoggingEventQuery() throws Exception {
32          // given
33          DBNameResolver nameResolver = new DefaultDBNameResolver();
34  
35          // when
36          String sql = SQLBuilder.buildInsertSQL(nameResolver);
37  
38          // then
39          final String expected = "INSERT INTO logging_event (timestmp, formatted_message, logger_name, level_string, thread_name, reference_flag, arg0, arg1, arg2, arg3, caller_filename, caller_class, caller_method, caller_line) VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
40          assertThat(sql).isEqualTo(expected);
41      }
42  
43      @Test
44      public void shouldReturnDefaultSqlInsertExceptionQuery() throws Exception {
45          // given
46          DBNameResolver nameResolver = new DefaultDBNameResolver();
47  
48          // when
49          String sql = SQLBuilder.buildInsertExceptionSQL(nameResolver);
50  
51          // then
52          final String expected = "INSERT INTO logging_event_exception (event_id, i, trace_line) VALUES (?, ?, ?)";
53          assertThat(sql).isEqualTo(expected);
54      }
55  
56      @Test
57      public void shouldReturnDefaultSqlInsertLoggingPropertyQuery() throws Exception {
58          // given
59          DBNameResolver nameResolver = new DefaultDBNameResolver();
60  
61          // when
62          String sql = SQLBuilder.buildInsertPropertiesSQL(nameResolver);
63  
64          // then
65          final String expected = "INSERT INTO logging_event_property (event_id, mapped_key, mapped_value) VALUES (?, ?, ?)";
66          assertThat(sql).isEqualTo(expected);
67      }
68  
69      private DBNameResolver createSimpleDBNameResolver() {
70          final SimpleDBNameResolver nameResolver = new SimpleDBNameResolver();
71          nameResolver.setTableNamePrefix("tp_");
72          nameResolver.setTableNameSuffix("_ts");
73          nameResolver.setColumnNamePrefix("cp_");
74          nameResolver.setColumnNameSuffix("_cs");
75          return nameResolver;
76      }
77  
78      @Test
79      public void shouldReturnSimpleSqlInsertLoggingEventQuery() throws Exception {
80          // given
81          DBNameResolver nameResolver = createSimpleDBNameResolver();
82  
83          // when
84          String sql = SQLBuilder.buildInsertSQL(nameResolver);
85  
86          // then
87          final String expected = "INSERT INTO tp_logging_event_ts (cp_timestmp_cs, cp_formatted_message_cs, cp_logger_name_cs, cp_level_string_cs, cp_thread_name_cs, cp_reference_flag_cs, cp_arg0_cs, cp_arg1_cs, cp_arg2_cs, cp_arg3_cs, cp_caller_filename_cs, cp_caller_class_cs, cp_caller_method_cs, cp_caller_line_cs) VALUES (?, ?, ? ,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
88          assertThat(sql).isEqualTo(expected);
89      }
90  
91      @Test
92      public void shouldReturnSimpleSqlInsertExceptionQuery() throws Exception {
93          // given
94          DBNameResolver nameResolver = createSimpleDBNameResolver();
95  
96          // when
97          String sql = SQLBuilder.buildInsertExceptionSQL(nameResolver);
98  
99          // then
100         final String expected = "INSERT INTO tp_logging_event_exception_ts (cp_event_id_cs, cp_i_cs, cp_trace_line_cs) VALUES (?, ?, ?)";
101         assertThat(sql).isEqualTo(expected);
102     }
103 
104     @Test
105     public void shouldReturnSimpleSqlInsertLoggingPropertyQuery() throws Exception {
106         // given
107         DBNameResolver nameResolver = createSimpleDBNameResolver();
108 
109         // when
110         String sql = SQLBuilder.buildInsertPropertiesSQL(nameResolver);
111 
112         // then
113         final String expected = "INSERT INTO tp_logging_event_property_ts (cp_event_id_cs, cp_mapped_key_cs, cp_mapped_value_cs) VALUES (?, ?, ?)";
114         assertThat(sql).isEqualTo(expected);
115     }
116 
117 }