1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.builder;
17
18 import java.util.Map;
19
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.Test;
22
23 class ParameterExpressionTest {
24
25 @Test
26 void simpleProperty() {
27 Map<String, String> result = new ParameterExpression("id");
28 Assertions.assertEquals(1, result.size());
29 Assertions.assertEquals("id", result.get("property"));
30 }
31
32 @Test
33 void propertyWithSpacesInside() {
34 Map<String, String> result = new ParameterExpression(" with spaces ");
35 Assertions.assertEquals(1, result.size());
36 Assertions.assertEquals("with spaces", result.get("property"));
37 }
38
39 @Test
40 void simplePropertyWithOldStyleJdbcType() {
41 Map<String, String> result = new ParameterExpression("id:VARCHAR");
42 Assertions.assertEquals(2, result.size());
43 Assertions.assertEquals("id", result.get("property"));
44 Assertions.assertEquals("VARCHAR", result.get("jdbcType"));
45 }
46
47 @Test
48 void oldStyleJdbcTypeWithExtraWhitespaces() {
49 Map<String, String> result = new ParameterExpression(" id : VARCHAR ");
50 Assertions.assertEquals(2, result.size());
51 Assertions.assertEquals("id", result.get("property"));
52 Assertions.assertEquals("VARCHAR", result.get("jdbcType"));
53 }
54
55 @Test
56 void expressionWithOldStyleJdbcType() {
57 Map<String, String> result = new ParameterExpression("(id.toString()):VARCHAR");
58 Assertions.assertEquals(2, result.size());
59 Assertions.assertEquals("id.toString()", result.get("expression"));
60 Assertions.assertEquals("VARCHAR", result.get("jdbcType"));
61 }
62
63 @Test
64 void simplePropertyWithOneAttribute() {
65 Map<String, String> result = new ParameterExpression("id,name=value");
66 Assertions.assertEquals(2, result.size());
67 Assertions.assertEquals("id", result.get("property"));
68 Assertions.assertEquals("value", result.get("name"));
69 }
70
71 @Test
72 void expressionWithOneAttribute() {
73 Map<String, String> result = new ParameterExpression("(id.toString()),name=value");
74 Assertions.assertEquals(2, result.size());
75 Assertions.assertEquals("id.toString()", result.get("expression"));
76 Assertions.assertEquals("value", result.get("name"));
77 }
78
79 @Test
80 void simplePropertyWithManyAttributes() {
81 Map<String, String> result = new ParameterExpression("id, attr1=val1, attr2=val2, attr3=val3");
82 Assertions.assertEquals(4, result.size());
83 Assertions.assertEquals("id", result.get("property"));
84 Assertions.assertEquals("val1", result.get("attr1"));
85 Assertions.assertEquals("val2", result.get("attr2"));
86 Assertions.assertEquals("val3", result.get("attr3"));
87 }
88
89 @Test
90 void expressionWithManyAttributes() {
91 Map<String, String> result = new ParameterExpression("(id.toString()), attr1=val1, attr2=val2, attr3=val3");
92 Assertions.assertEquals(4, result.size());
93 Assertions.assertEquals("id.toString()", result.get("expression"));
94 Assertions.assertEquals("val1", result.get("attr1"));
95 Assertions.assertEquals("val2", result.get("attr2"));
96 Assertions.assertEquals("val3", result.get("attr3"));
97 }
98
99 @Test
100 void simplePropertyWithOldStyleJdbcTypeAndAttributes() {
101 Map<String, String> result = new ParameterExpression("id:VARCHAR, attr1=val1, attr2=val2");
102 Assertions.assertEquals(4, result.size());
103 Assertions.assertEquals("id", result.get("property"));
104 Assertions.assertEquals("VARCHAR", result.get("jdbcType"));
105 Assertions.assertEquals("val1", result.get("attr1"));
106 Assertions.assertEquals("val2", result.get("attr2"));
107 }
108
109 @Test
110 void simplePropertyWithSpaceAndManyAttributes() {
111 Map<String, String> result = new ParameterExpression("user name, attr1=val1, attr2=val2, attr3=val3");
112 Assertions.assertEquals(4, result.size());
113 Assertions.assertEquals("user name", result.get("property"));
114 Assertions.assertEquals("val1", result.get("attr1"));
115 Assertions.assertEquals("val2", result.get("attr2"));
116 Assertions.assertEquals("val3", result.get("attr3"));
117 }
118
119 @Test
120 void shouldIgnoreLeadingAndTrailingSpaces() {
121 Map<String, String> result = new ParameterExpression(" id , jdbcType = VARCHAR, attr1 = val1 , attr2 = val2 ");
122 Assertions.assertEquals(4, result.size());
123 Assertions.assertEquals("id", result.get("property"));
124 Assertions.assertEquals("VARCHAR", result.get("jdbcType"));
125 Assertions.assertEquals("val1", result.get("attr1"));
126 Assertions.assertEquals("val2", result.get("attr2"));
127 }
128
129 @Test
130 void invalidOldJdbcTypeFormat() {
131 try {
132 new ParameterExpression("id:");
133 Assertions.fail();
134 } catch (BuilderException e) {
135 Assertions.assertTrue(e.getMessage().contains("Parsing error in {id:} in position 3"));
136 }
137 }
138
139 @Test
140 void invalidJdbcTypeOptUsingExpression() {
141 try {
142 new ParameterExpression("(expression)+");
143 Assertions.fail();
144 } catch (BuilderException e) {
145 Assertions.assertTrue(e.getMessage().contains("Parsing error in {(expression)+} in position 12"));
146 }
147 }
148
149 }