View Javadoc
1   /*
2    *    Copyright 2009-2024 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.reflection.property;
17  
18  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * @author <a href="1181963012mw@gmail.com">mawen12</a>
25   *
26   * @see PropertyTokenizer
27   */
28  class PropertyTokenizerTest {
29  
30    @Test
31    void shouldParsePropertySuccessfully() {
32      String fullname = "id";
33      PropertyTokenizer tokenizer = new PropertyTokenizer(fullname);
34  
35      assertEquals("id", tokenizer.getIndexedName());
36      assertEquals("id", tokenizer.getName());
37  
38      assertNull(tokenizer.getChildren());
39      assertNull(tokenizer.getIndex());
40      assertFalse(tokenizer.hasNext());
41      assertNull(tokenizer.getIndex());
42  
43      assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(tokenizer::remove)
44          .withMessage("Remove is not supported, as it has no meaning in the context of properties.");
45    }
46  
47    @Test
48    void shouldParsePropertyWhichContainsDelimSuccessfully() {
49      String fullname = "person.id";
50      PropertyTokenizer tokenizer = new PropertyTokenizer(fullname);
51  
52      assertEquals("person", tokenizer.getIndexedName());
53      assertEquals("person", tokenizer.getName());
54      assertTrue(tokenizer.hasNext());
55      assertEquals("id", tokenizer.getChildren());
56  
57      assertNull(tokenizer.getIndex());
58  
59      assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(tokenizer::remove)
60          .withMessage("Remove is not supported, as it has no meaning in the context of properties.");
61    }
62  
63    @Test
64    void shouldParsePropertyWhichContainsIndexSuccessfully() {
65      String fullname = "array[0]";
66      PropertyTokenizer tokenizer = new PropertyTokenizer(fullname);
67  
68      assertEquals("array[0]", tokenizer.getIndexedName());
69      assertEquals("array", tokenizer.getName());
70      assertEquals("0", tokenizer.getIndex());
71  
72      assertFalse(tokenizer.hasNext());
73      assertNull(tokenizer.getChildren());
74  
75      assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(tokenizer::remove)
76          .withMessage("Remove is not supported, as it has no meaning in the context of properties.");
77    }
78  }