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.names;
15  
16  import org.junit.Before;
17  import org.junit.Test;
18  
19  import static org.assertj.core.api.Assertions.assertThat;
20  
21  /**
22   * @author Tomasz Nurkiewicz
23   * @since 2010-03-22
24   */
25  public class SimpleDBNameResolverTest {
26  
27      private SimpleDBNameResolver nameResolver;
28  
29      @Before
30      public void setUp() throws Exception {
31          nameResolver = new SimpleDBNameResolver();
32          /*
33           * nameResolver.setTableNameSuffix("_ts"); nameResolver.setColumnNamePrefix("cp_");
34           * nameResolver.setColumnNameSuffix("_cs");
35           */
36      }
37  
38      @Test
39      public void shouldReturnTableNameWithPrefix() throws Exception {
40          // given
41  
42          // when
43          nameResolver.setTableNamePrefix("tp_");
44  
45          // then
46          assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("tp_logging_event");
47          assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name");
48      }
49  
50      @Test
51      public void shouldReturnTableNameWithSuffix() throws Exception {
52          // given
53  
54          // when
55          nameResolver.setTableNameSuffix("_ts");
56  
57          // then
58          assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event_ts");
59          assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name");
60      }
61  
62      @Test
63      public void shouldReturnTableNameWithBothPrefixAndSuffix() throws Exception {
64          // given
65  
66          // when
67          nameResolver.setTableNamePrefix("tp_");
68          nameResolver.setTableNameSuffix("_ts");
69  
70          // then
71          assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("tp_logging_event_ts");
72          assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name");
73      }
74  
75      @Test
76      public void shouldReturnColumnNameWithPrefix() throws Exception {
77          // given
78  
79          // when
80          nameResolver.setColumnNamePrefix("cp_");
81  
82          // then
83          assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event");
84          assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("cp_thread_name");
85      }
86  
87      @Test
88      public void shouldReturnColumnNameWithSuffix() throws Exception {
89          // given
90  
91          // when
92          nameResolver.setColumnNameSuffix("_cs");
93  
94          // then
95          assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event");
96          assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name_cs");
97      }
98  
99      @Test
100     public void shouldReturnColumnNameWithBothPrefixAndSuffix() throws Exception {
101         // given
102 
103         // when
104         nameResolver.setColumnNamePrefix("cp_");
105         nameResolver.setColumnNameSuffix("_cs");
106 
107         // then
108         assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event");
109         assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("cp_thread_name_cs");
110     }
111 
112     @Test
113     public void shouldReturnTableAndColumnNamesWithBothPrefixAndSuffix() throws Exception {
114         // given
115 
116         // when
117         nameResolver.setTableNamePrefix("tp_");
118         nameResolver.setTableNameSuffix("_ts");
119         nameResolver.setColumnNamePrefix("cp_");
120         nameResolver.setColumnNameSuffix("_cs");
121 
122         // then
123         assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("tp_logging_event_ts");
124         assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("cp_thread_name_cs");
125     }
126 
127     @Test
128     public void shouldHandleNullsAsEmptyStrings() throws Exception {
129         // given
130 
131         // when
132         nameResolver.setTableNamePrefix(null);
133         nameResolver.setTableNameSuffix(null);
134         nameResolver.setColumnNamePrefix(null);
135         nameResolver.setColumnNameSuffix(null);
136 
137         // then
138         assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event");
139         assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name");
140     }
141 
142 }