|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2014-2017 abel533@gmail.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package tk.mybatis.mapper.test.util; |
| 26 | + |
| 27 | +import org.junit.Test; |
| 28 | +import tk.mybatis.mapper.MapperException; |
| 29 | +import tk.mybatis.mapper.entity.Example; |
| 30 | +import tk.mybatis.mapper.util.OGNL; |
| 31 | + |
| 32 | +import jakarta.persistence.Table; |
| 33 | + |
| 34 | +/** |
| 35 | + * Test OGNL.exampleHasAtLeastOneCriteriaCheck method |
| 36 | + * @author liuzh |
| 37 | + */ |
| 38 | +public class TestOGNLCriteriaCheck { |
| 39 | + |
| 40 | + // Simple test entity class with table annotation |
| 41 | + @Table(name = "test_entity") |
| 42 | + static class TestEntity { |
| 43 | + private Integer id; |
| 44 | + private String name; |
| 45 | + |
| 46 | + public Integer getId() { |
| 47 | + return id; |
| 48 | + } |
| 49 | + |
| 50 | + public void setId(Integer id) { |
| 51 | + this.id = id; |
| 52 | + } |
| 53 | + |
| 54 | + public String getName() { |
| 55 | + return name; |
| 56 | + } |
| 57 | + |
| 58 | + public void setName(String name) { |
| 59 | + this.name = name; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test with null example - should throw MapperException |
| 65 | + */ |
| 66 | + @Test(expected = MapperException.class) |
| 67 | + public void testNullExample() { |
| 68 | + OGNL.exampleHasAtLeastOneCriteriaCheck(null); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test with empty criteria list - should throw MapperException |
| 73 | + */ |
| 74 | + @Test(expected = MapperException.class) |
| 75 | + public void testEmptyCriteriaList() { |
| 76 | + Example example = new Example(TestEntity.class); |
| 77 | + OGNL.exampleHasAtLeastOneCriteriaCheck(example); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test with criteria that has no conditions (invalid) - should throw MapperException |
| 82 | + */ |
| 83 | + @Test(expected = MapperException.class) |
| 84 | + public void testInvalidCriteria() { |
| 85 | + Example example = new Example(TestEntity.class); |
| 86 | + example.createCriteria(); // Create criteria but don't add any conditions |
| 87 | + OGNL.exampleHasAtLeastOneCriteriaCheck(example); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test with criteria that has null value (invalid) - should throw MapperException |
| 92 | + */ |
| 93 | + @Test(expected = MapperException.class) |
| 94 | + public void testCriteriaWithNullValue() { |
| 95 | + Example example = new Example(TestEntity.class); |
| 96 | + // When notNull=false (default), null values are ignored and no criteria is added |
| 97 | + example.createCriteria().andEqualTo("name", null); |
| 98 | + OGNL.exampleHasAtLeastOneCriteriaCheck(example); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test with multiple criteria where all are invalid - should throw MapperException |
| 103 | + */ |
| 104 | + @Test(expected = MapperException.class) |
| 105 | + public void testMultipleInvalidCriteria() { |
| 106 | + Example example = new Example(TestEntity.class); |
| 107 | + example.createCriteria().andEqualTo("name", null); |
| 108 | + example.or().andEqualTo("name", null); |
| 109 | + OGNL.exampleHasAtLeastOneCriteriaCheck(example); |
| 110 | + } |
| 111 | +} |
0 commit comments