View Javadoc
1   package org.codehaus.plexus.util.reflection;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Test;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  /**
25   * <p>ReflectorTest class.</p>
26   *
27   * @author J&ouml;rg Schaible
28   * @version $Id: $Id
29   * @since 3.4.0
30   */
31  public class ReflectorTest {
32      private Project project;
33  
34      private Reflector reflector;
35  
36      /**
37       * <p>setUp.</p>
38       *
39       * @throws java.lang.Exception if any.
40       */
41      @BeforeEach
42      public void setUp() throws Exception {
43          project = new Project();
44          project.setModelVersion("1.0.0");
45          project.setVersion("42");
46  
47          reflector = new Reflector();
48      }
49  
50      /**
51       * <p>testObjectPropertyFromName.</p>
52       *
53       * @throws java.lang.Exception if any.
54       */
55      @Test
56      public void testObjectPropertyFromName() throws Exception {
57          assertEquals("1.0.0", reflector.getObjectProperty(project, "modelVersion"));
58      }
59  
60      /**
61       * <p>testObjectPropertyFromBean.</p>
62       *
63       * @throws java.lang.Exception if any.
64       */
65      @org.junit.jupiter.api.Test
66      public void testObjectPropertyFromBean() throws Exception {
67          assertEquals("Foo", reflector.getObjectProperty(project, "name"));
68      }
69  
70      /**
71       * <p>testObjectPropertyFromField.</p>
72       *
73       * @throws java.lang.Exception if any.
74       */
75      @Test
76      public void testObjectPropertyFromField() throws Exception {
77          assertEquals("42", reflector.getObjectProperty(project, "version"));
78      }
79  
80      public static class Project {
81          private String model;
82  
83          private String name;
84  
85          private String version;
86  
87          public void setModelVersion(String modelVersion) {
88              this.model = modelVersion;
89          }
90  
91          public void setVersion(String version) {
92              this.version = version;
93          }
94  
95          public String modelVersion() {
96              return model;
97          }
98  
99          public String getName() {
100             return "Foo";
101         }
102     }
103 }