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.classic.db;
015
016import static org.assertj.core.api.Assertions.assertThat;
017
018import org.junit.Test;
019
020import ch.qos.logback.classic.db.names.DBNameResolver;
021import ch.qos.logback.classic.db.names.DefaultDBNameResolver;
022import ch.qos.logback.classic.db.names.SimpleDBNameResolver;
023
024/**
025 * @author Tomasz Nurkiewicz
026 * @since 2010-03-22
027 */
028public class SQLBuilderTest {
029
030    @Test
031    public void shouldReturnDefaultSqlInsertLoggingEventQuery() throws Exception {
032        // given
033        DBNameResolver nameResolver = new DefaultDBNameResolver();
034
035        // when
036        String sql = SQLBuilder.buildInsertSQL(nameResolver);
037
038        // then
039        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 (?, ?, ? ,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
040        assertThat(sql).isEqualTo(expected);
041    }
042
043    @Test
044    public void shouldReturnDefaultSqlInsertExceptionQuery() throws Exception {
045        // given
046        DBNameResolver nameResolver = new DefaultDBNameResolver();
047
048        // when
049        String sql = SQLBuilder.buildInsertExceptionSQL(nameResolver);
050
051        // then
052        final String expected = "INSERT INTO logging_event_exception (event_id, i, trace_line) VALUES (?, ?, ?)";
053        assertThat(sql).isEqualTo(expected);
054    }
055
056    @Test
057    public void shouldReturnDefaultSqlInsertLoggingPropertyQuery() throws Exception {
058        // given
059        DBNameResolver nameResolver = new DefaultDBNameResolver();
060
061        // when
062        String sql = SQLBuilder.buildInsertPropertiesSQL(nameResolver);
063
064        // then
065        final String expected = "INSERT INTO logging_event_property (event_id, mapped_key, mapped_value) VALUES (?, ?, ?)";
066        assertThat(sql).isEqualTo(expected);
067    }
068
069    private DBNameResolver createSimpleDBNameResolver() {
070        final SimpleDBNameResolver nameResolver = new SimpleDBNameResolver();
071        nameResolver.setTableNamePrefix("tp_");
072        nameResolver.setTableNameSuffix("_ts");
073        nameResolver.setColumnNamePrefix("cp_");
074        nameResolver.setColumnNameSuffix("_cs");
075        return nameResolver;
076    }
077
078    @Test
079    public void shouldReturnSimpleSqlInsertLoggingEventQuery() throws Exception {
080        // given
081        DBNameResolver nameResolver = createSimpleDBNameResolver();
082
083        // when
084        String sql = SQLBuilder.buildInsertSQL(nameResolver);
085
086        // then
087        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 (?, ?, ? ,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
088        assertThat(sql).isEqualTo(expected);
089    }
090
091    @Test
092    public void shouldReturnSimpleSqlInsertExceptionQuery() throws Exception {
093        // given
094        DBNameResolver nameResolver = createSimpleDBNameResolver();
095
096        // when
097        String sql = SQLBuilder.buildInsertExceptionSQL(nameResolver);
098
099        // 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}