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