2
* Hibernate, Relational Persistence for Idiomatic Java
4
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
5
* indicated by the @author tags or express copyright attribution
6
* statements applied by the authors. All third-party contributions are
7
* distributed under license by Red Hat Middleware LLC.
9
* This copyrighted material is made available to anyone wishing to use, modify,
10
* copy, or redistribute it subject to the terms and conditions of the GNU
11
* Lesser General Public License, as published by the Free Software Foundation.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this distribution; if not, write to:
20
* Free Software Foundation, Inc.
21
* 51 Franklin Street, Fifth Floor
22
* Boston, MA 02110-1301 USA
24
package org.hibernate.test.dialect.function;
26
import java.util.List;
27
import java.util.ArrayList;
29
import junit.framework.TestCase;
31
import org.hibernate.dialect.function.AnsiTrimEmulationFunction;
36
* @author Steve Ebersole
38
public class AnsiTrimEmulationFunctionTest extends TestCase {
39
private static final String trimSource = "a.column";
41
public void testBasicSqlServerProcessing() {
42
AnsiTrimEmulationFunction function = new AnsiTrimEmulationFunction();
44
performBasicSpaceTrimmingTests( function );
46
final String expectedTrimPrep = "replace(replace(a.column,' ','${space}$'),'-',' ')";
47
final String expectedPostTrimPrefix = "replace(replace(";
48
final String expectedPostTrimSuffix = ",' ','-'),'${space}$',' ')";
50
// -> trim(LEADING '-' FROM a.column)
51
String rendered = function.render( argList( "LEADING", "'-'", "FROM", trimSource ), null );
52
String expected = expectedPostTrimPrefix + "ltrim(" + expectedTrimPrep + ")" + expectedPostTrimSuffix;
53
assertEquals( expected, rendered );
55
// -> trim(TRAILING '-' FROM a.column)
56
rendered = function.render( argList( "TRAILING", "'-'", "FROM", trimSource ), null );
57
expected = expectedPostTrimPrefix + "rtrim(" + expectedTrimPrep + ")" + expectedPostTrimSuffix;
58
assertEquals( expected, rendered );
60
// -> trim(BOTH '-' FROM a.column)
61
rendered = function.render( argList( "BOTH", "'-'", "FROM", trimSource ), null );
62
expected = expectedPostTrimPrefix + "ltrim(rtrim(" + expectedTrimPrep + "))" + expectedPostTrimSuffix;
63
assertEquals( expected, rendered );
65
// -> trim('-' FROM a.column)
66
rendered = function.render( argList( "'-'", "FROM", trimSource ), null );
67
expected = expectedPostTrimPrefix + "ltrim(rtrim(" + expectedTrimPrep + "))" + expectedPostTrimSuffix;
68
assertEquals( expected, rendered );
71
public void testBasicSybaseProcessing() {
72
AnsiTrimEmulationFunction function = new AnsiTrimEmulationFunction(
73
AnsiTrimEmulationFunction.LTRIM,
74
AnsiTrimEmulationFunction.RTRIM,
78
performBasicSpaceTrimmingTests( function );
80
final String expectedTrimPrep = "str_replace(str_replace(a.column,' ','${space}$'),'-',' ')";
81
final String expectedPostTrimPrefix = "str_replace(str_replace(";
82
final String expectedPostTrimSuffix = ",' ','-'),'${space}$',' ')";
84
// -> trim(LEADING '-' FROM a.column)
85
String rendered = function.render( argList( "LEADING", "'-'", "FROM", trimSource ), null );
86
String expected = expectedPostTrimPrefix + "ltrim(" + expectedTrimPrep + ")" + expectedPostTrimSuffix;
87
assertEquals( expected, rendered );
89
// -> trim(TRAILING '-' FROM a.column)
90
rendered = function.render( argList( "TRAILING", "'-'", "FROM", trimSource ), null );
91
expected = expectedPostTrimPrefix + "rtrim(" + expectedTrimPrep + ")" + expectedPostTrimSuffix;
92
assertEquals( expected, rendered );
94
// -> trim(BOTH '-' FROM a.column)
95
rendered = function.render( argList( "BOTH", "'-'", "FROM", trimSource ), null );
96
expected = expectedPostTrimPrefix + "ltrim(rtrim(" + expectedTrimPrep + "))" + expectedPostTrimSuffix;
97
assertEquals( expected, rendered );
99
// -> trim('-' FROM a.column)
100
rendered = function.render( argList( "'-'", "FROM", trimSource ), null );
101
expected = expectedPostTrimPrefix + "ltrim(rtrim(" + expectedTrimPrep + "))" + expectedPostTrimSuffix;
102
assertEquals( expected, rendered );
105
private void performBasicSpaceTrimmingTests(AnsiTrimEmulationFunction function) {
107
String rendered = function.render( argList( trimSource ), null );
108
assertEquals( "ltrim(rtrim(a.column))", rendered );
110
// -> trim(FROM a.column)
111
rendered = function.render( argList( "FROM", trimSource ), null );
112
assertEquals( "ltrim(rtrim(a.column))", rendered );
114
// -> trim(BOTH FROM a.column)
115
rendered = function.render( argList( "BOTH", "FROM", trimSource ), null );
116
assertEquals( "ltrim(rtrim(a.column))", rendered );
118
// -> trim(BOTH ' ' FROM a.column)
119
rendered = function.render( argList( "BOTH", "' '", "FROM", trimSource ), null );
120
assertEquals( "ltrim(rtrim(a.column))", rendered );
122
// -> trim(LEADING FROM a.column)
123
rendered = function.render( argList( "LEADING", "FROM", trimSource ), null );
124
assertEquals( "ltrim(a.column)", rendered );
126
// -> trim(LEADING ' ' FROM a.column)
127
rendered = function.render( argList( "LEADING", "' '", "FROM", trimSource ), null );
128
assertEquals( "ltrim(a.column)", rendered );
130
// -> trim(TRAILING FROM a.column)
131
rendered = function.render( argList( "TRAILING", "FROM", trimSource ), null );
132
assertEquals( "rtrim(a.column)", rendered );
134
// -> trim(TRAILING ' ' FROM a.column)
135
rendered = function.render( argList( "TRAILING", "' '", "FROM", trimSource ), null );
136
assertEquals( "rtrim(a.column)", rendered );
139
private List argList(String arg) {
140
ArrayList args = new ArrayList();
145
private List argList(String arg1, String arg2) {
146
ArrayList args = new ArrayList();
152
private List argList(String arg1, String arg2, String arg3) {
153
ArrayList args = new ArrayList();
160
private List argList(String arg1, String arg2, String arg3, String arg4) {
161
ArrayList args = new ArrayList();