View Javadoc
1   /*
2    *    Copyright 2012-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.scripting.velocity;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.StringWriter;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Properties;
25  
26  import org.apache.ibatis.mapping.ParameterMapping;
27  import org.apache.ibatis.session.Configuration;
28  import org.apache.velocity.VelocityContext;
29  import org.apache.velocity.app.VelocityEngine;
30  import org.apache.velocity.runtime.RuntimeConstants;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  class InDirectiveTest {
35  
36    private static VelocityContext ctxt;
37    private static VelocityEngine velocity;
38  
39    @BeforeAll
40    static void setUpClass() {
41      Properties p = new Properties();
42      p.setProperty(RuntimeConstants.CUSTOM_DIRECTIVES, InDirective.class.getName());
43      velocity = new VelocityEngine();
44      velocity.setProperty("runtime.log", "target/velocity.log");
45      velocity.init(p);
46      ctxt = new VelocityContext();
47      ctxt.put(SQLScriptSource.MAPPING_COLLECTOR_KEY,
48          new ParameterMappingCollector(new ParameterMapping[] {}, new HashMap<>(), new Configuration()));
49      StringWriter writer = new StringWriter();
50      velocity.evaluate(ctxt, writer, "WARM", "1+1");
51    }
52  
53    @Test
54    void ensureInClauseHasEmpty() {
55      StringWriter w = new StringWriter();
56      ctxt.put("list", Collections.emptyList());
57      velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
58      String result = w.toString();
59      assertEquals("((id NOT IN ( NULL )))", result);
60    }
61  
62    @Test
63    void ensureInClauseHasOne() {
64      StringWriter w = new StringWriter();
65      ctxt.put("list", Collections.singletonList("?"));
66      velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
67      String result = w.toString();
68      assertEquals(1, result.split("\\?").length - 1);
69      assertEquals(1, result.split("IN").length - 1);
70    }
71  
72    @Test
73    void ensureInClauseHasTwo() {
74      StringWriter w = new StringWriter();
75      ctxt.put("list", Arrays.asList("?", "?"));
76      velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
77      String result = w.toString();
78      assertEquals(2, result.split("\\?").length - 1);
79    }
80  
81    @Test
82    void ensureInClauseHasOneThousand() {
83      StringWriter w = new StringWriter();
84      String[] arr = new String[1000];
85      Arrays.fill(arr, "?");
86      ctxt.put("list", Arrays.asList(arr));
87      velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
88      String result = w.toString();
89      assertEquals(1000, result.split("\\?").length - 1);
90      assertEquals(0, result.split("OR").length - 1);
91    }
92  
93    @Test
94    void ensureInClauseHasOneThousandAndOne() {
95      StringWriter w = new StringWriter();
96      String[] arr = new String[1001];
97      Arrays.fill(arr, "?");
98      ctxt.put("list", Arrays.asList(arr));
99      velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
100     String result = w.toString();
101     assertEquals(1001, result.split("\\?").length - 1);
102     assertEquals(1, result.split("OR").length - 1);
103   }
104 
105   @Test
106   void ensureInClauseHasTwoThousand() {
107     StringWriter w = new StringWriter();
108     String[] arr = new String[2000];
109     Arrays.fill(arr, "?");
110     ctxt.put("list", Arrays.asList(arr));
111     velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
112     String result = w.toString();
113     assertEquals(2000, result.split("\\?").length - 1);
114     assertEquals(1, result.split("OR").length - 1);
115   }
116 
117   @Test
118   void ensureInClauseHasTwoThousandAndOne() {
119     StringWriter w = new StringWriter();
120     String[] arr = new String[2001];
121     Arrays.fill(arr, "?");
122     ctxt.put("list", Arrays.asList(arr));
123     velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
124     String result = w.toString();
125     assertEquals(2001, result.split("\\?").length - 1);
126     assertEquals(2, result.split("OR").length - 1);
127   }
128 
129   @Test
130   void ensureInClauseHasThreeThousandAndOne() {
131     StringWriter w = new StringWriter();
132     String[] arr = new String[3001];
133     Arrays.fill(arr, "?");
134     ctxt.put("list", Arrays.asList(arr));
135     velocity.evaluate(ctxt, w, "TEST", "#in($list $id 'id')?#end");
136     String result = w.toString();
137     assertEquals(3001, result.split("\\?").length - 1);
138     assertEquals(3, result.split("OR").length - 1);
139   }
140 
141 }