View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.lang.reflect.Array;
23  import java.util.function.Supplier;
24  
25  import org.junit.jupiter.api.Test;
26  
27  class $trange {
28  }
29  
30  /**
31   * Tests {@link ClassUtils#getShortClassName(Class)} for <a href="https://issues.apache.org/jira/browse/LANG-1818">LANG-1818</a>
32   */
33  public class ClassUtilsShortClassNameTest extends AbstractLangTest {
34  
35      class $Inner {
36      }
37  
38      class Inner {
39  
40          class Ne$ted {
41          }
42      }
43  
44      @Test
45      void testDeeplyNestedMemberOfAnonymousClass() {
46          final Supplier<Class<?>> anonymous = new Supplier<Class<?>>() {
47  
48              class Inner$Name {
49                  class Deep$Name {
50                  }
51              }
52  
53              @Override
54              public Class<?> get() {
55                  return Inner$Name.Deep$Name.class;
56              }
57          };
58          final Class<?> deep = anonymous.get();
59          final String expected = ClassUtils.getShortClassName(anonymous.getClass()) + ".Inner$Name.Deep$Name";
60          assertEquals(expected, ClassUtils.getShortClassName(deep));
61          assertEquals(expected + "[][]", ClassUtils.getShortClassName(Array.newInstance(deep, 0, 0).getClass()));
62      }
63  
64      @Test
65      void testDeeplyNestedMemberOfLocalClass() {
66          class Local {
67              class Inner$Name {
68                  class Deep$Name {
69                  }
70              }
71          }
72          final String expected = ClassUtils.getShortClassName(Local.class) + ".Inner$Name.Deep$Name";
73          assertEquals(expected, ClassUtils.getShortClassName(Local.Inner$Name.Deep$Name.class));
74          assertEquals(expected + "[][]", ClassUtils.getShortClassName(Local.Inner$Name.Deep$Name[][].class));
75      }
76  
77      @Test
78      void testDollarSignImmediatelyAfterPackage() {
79          assertEquals("$trange", ClassUtils.getShortClassName($trange.class));
80      }
81  
82      @Test
83      void testDollarSignWithinMemberOfAnonymousClass() {
84          final Supplier<Class<?>> anonymous = new Supplier<Class<?>>() {
85  
86              class Inner$Name {
87              }
88  
89              @Override
90              public Class<?> get() {
91                  return Inner$Name.class;
92              }
93          };
94          final String shortName = ClassUtils.getShortClassName(anonymous.get());
95          assertEquals(ClassUtils.getShortClassName(anonymous.getClass()) + ".Inner$Name", shortName);
96      }
97  
98      @Test
99      void testDollarSignWithinMemberOfLocalClass() {
100         class Local {
101             class Inner$Name {
102             }
103         }
104         final String shortName = ClassUtils.getShortClassName(Local.Inner$Name.class);
105         assertEquals(ClassUtils.getShortClassName(Local.class) + ".Inner$Name", shortName);
106     }
107 
108     @Test
109     void testDollarSignWithinName() {
110         assertEquals("Pa$$word", ClassUtils.getShortClassName(Pa$$word.class));
111     }
112 
113     @Test
114     void testInnerClassName() {
115         assertEquals(getClass().getSimpleName() + ".Inner", ClassUtils.getShortClassName(Inner.class));
116     }
117 
118     @Test
119     void testMemberOfAnonymousClass() {
120         final Supplier<Class<?>> anonymous = new Supplier<Class<?>>() {
121 
122             class Inner {
123             }
124 
125             @Override
126             public Class<?> get() {
127                 return Inner.class;
128             }
129         };
130         final Class<?> inner = anonymous.get();
131         // The enclosing class and compiler-generated ordinal must be kept, as in the name-based form.
132         assertEquals(ClassUtils.getShortClassName(inner.getName()), ClassUtils.getShortClassName(inner));
133         assertEquals(ClassUtils.getShortClassName(inner.getName()) + "[]", ClassUtils.getShortClassName(Array.newInstance(inner, 0).getClass()));
134     }
135 
136     @Test
137     void testMemberOfLocalClass() {
138         class Local {
139             class Inner {
140             }
141         }
142         // The enclosing class and compiler-generated ordinal must be kept, as in the name-based form.
143         assertEquals(ClassUtils.getShortClassName(Local.Inner.class.getName()), ClassUtils.getShortClassName(Local.Inner.class));
144         assertEquals(ClassUtils.getShortClassName(Local.Inner.class.getName()) + "[]", ClassUtils.getShortClassName(Local.Inner[].class));
145     }
146 
147     @Test
148     void testMultipleDollarSigns() {
149         assertEquals(getClass().getSimpleName() + ".$Inner", ClassUtils.getShortClassName($Inner.class));
150     }
151 
152     @Test
153     void testNe$tedClassName() {
154         assertEquals(getClass().getSimpleName() + ".Inner.Ne$ted", ClassUtils.getShortClassName(Inner.Ne$ted.class));
155     }
156 }
157 
158 class Pa$$word {
159 }