View Javadoc
1   /*
2    *    Copyright 2009-2023 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.apache.ibatis.scripting.xmltags;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ExecutorService;
26  import java.util.concurrent.Executors;
27  import java.util.concurrent.Future;
28  import java.util.stream.IntStream;
29  
30  import org.junit.jupiter.api.Test;
31  
32  class OgnlCacheTest {
33    @Test
34    void concurrentAccess() throws Exception {
35      class DataClass {
36        @SuppressWarnings("unused")
37        private int id;
38      }
39      int run = 1000;
40      Map<String, Object> context = new HashMap<>();
41      List<Future<Object>> futures = new ArrayList<>();
42      context.put("data", new DataClass());
43      ExecutorService executor = Executors.newCachedThreadPool();
44      IntStream.range(0, run).forEach(i -> {
45        futures.add(executor.submit(() -> OgnlCache.getValue("data.id", context)));
46      });
47      for (int i = 0; i < run; i++) {
48        assertNotNull(futures.get(i).get());
49      }
50      executor.shutdown();
51    }
52  
53    @Test
54    void issue2609() throws Exception {
55      Map<String, Object> context = new HashMap<>();
56      context.put("d1", java.sql.Date.valueOf("2022-01-01"));
57      context.put("d2", java.sql.Date.valueOf("2022-01-02"));
58      assertEquals(-1, OgnlCache.getValue("d1.compareTo(d2)", context));
59    }
60  }