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.names; 015 016import org.junit.Before; 017import org.junit.Test; 018 019import static org.assertj.core.api.Assertions.assertThat; 020 021/** 022 * @author Tomasz Nurkiewicz 023 * @since 2010-03-22 024 */ 025public class SimpleDBNameResolverTest { 026 027 private SimpleDBNameResolver nameResolver; 028 029 @Before 030 public void setUp() throws Exception { 031 nameResolver = new SimpleDBNameResolver(); 032 /* 033 * nameResolver.setTableNameSuffix("_ts"); nameResolver.setColumnNamePrefix("cp_"); 034 * nameResolver.setColumnNameSuffix("_cs"); 035 */ 036 } 037 038 @Test 039 public void shouldReturnTableNameWithPrefix() throws Exception { 040 // given 041 042 // when 043 nameResolver.setTableNamePrefix("tp_"); 044 045 // then 046 assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("tp_logging_event"); 047 assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name"); 048 } 049 050 @Test 051 public void shouldReturnTableNameWithSuffix() throws Exception { 052 // given 053 054 // when 055 nameResolver.setTableNameSuffix("_ts"); 056 057 // then 058 assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event_ts"); 059 assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name"); 060 } 061 062 @Test 063 public void shouldReturnTableNameWithBothPrefixAndSuffix() throws Exception { 064 // given 065 066 // when 067 nameResolver.setTableNamePrefix("tp_"); 068 nameResolver.setTableNameSuffix("_ts"); 069 070 // then 071 assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("tp_logging_event_ts"); 072 assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name"); 073 } 074 075 @Test 076 public void shouldReturnColumnNameWithPrefix() throws Exception { 077 // given 078 079 // when 080 nameResolver.setColumnNamePrefix("cp_"); 081 082 // then 083 assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event"); 084 assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("cp_thread_name"); 085 } 086 087 @Test 088 public void shouldReturnColumnNameWithSuffix() throws Exception { 089 // given 090 091 // when 092 nameResolver.setColumnNameSuffix("_cs"); 093 094 // then 095 assertThat(nameResolver.getTableName(TableName.LOGGING_EVENT)).isEqualTo("logging_event"); 096 assertThat(nameResolver.getColumnName(ColumnName.THREAD_NAME)).isEqualTo("thread_name_cs"); 097 } 098 099 @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}