• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Square, Inc.
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  * http://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 com.squareup.javapoet;
17 
18 import com.google.testing.compile.CompilationRule;
19 import java.util.ArrayList;
20 import java.util.List;
21 import javax.annotation.Nullable;
22 import javax.lang.model.element.ExecutableElement;
23 import javax.lang.model.element.TypeElement;
24 import javax.lang.model.element.VariableElement;
25 import javax.lang.model.util.Elements;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import javax.lang.model.element.Modifier;
29 import org.junit.Test;
30 
31 import static com.google.common.truth.Truth.assertThat;
32 import static com.squareup.javapoet.TestUtil.findFirst;
33 import static javax.lang.model.util.ElementFilter.fieldsIn;
34 import static javax.lang.model.util.ElementFilter.methodsIn;
35 import static org.junit.Assert.fail;
36 
37 public class ParameterSpecTest {
38   @Rule public final CompilationRule compilation = new CompilationRule();
39 
40   private Elements elements;
41 
setUp()42   @Before public void setUp() {
43     elements = compilation.getElements();
44   }
45 
getElement(Class<?> clazz)46   private TypeElement getElement(Class<?> clazz) {
47     return elements.getTypeElement(clazz.getCanonicalName());
48   }
49 
equalsAndHashCode()50   @Test public void equalsAndHashCode() {
51     ParameterSpec a = ParameterSpec.builder(int.class, "foo").build();
52     ParameterSpec b = ParameterSpec.builder(int.class, "foo").build();
53     assertThat(a.equals(b)).isTrue();
54     assertThat(a.hashCode()).isEqualTo(b.hashCode());
55     assertThat(a.toString()).isEqualTo(b.toString());
56     a = ParameterSpec.builder(int.class, "i").addModifiers(Modifier.STATIC).build();
57     b = ParameterSpec.builder(int.class, "i").addModifiers(Modifier.STATIC).build();
58     assertThat(a.equals(b)).isTrue();
59     assertThat(a.hashCode()).isEqualTo(b.hashCode());
60     assertThat(a.toString()).isEqualTo(b.toString());
61   }
62 
receiverParameterInstanceMethod()63   @Test public void receiverParameterInstanceMethod() {
64     ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "this");
65     assertThat(builder.build().name).isEqualTo("this");
66   }
67 
receiverParameterNestedClass()68   @Test public void receiverParameterNestedClass() {
69     ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "Foo.this");
70     assertThat(builder.build().name).isEqualTo("Foo.this");
71   }
72 
keywordName()73   @Test public void keywordName() {
74     try {
75       ParameterSpec.builder(int.class, "super");
76       fail();
77     } catch (Exception e) {
78       assertThat(e.getMessage()).isEqualTo("not a valid name: super");
79     }
80   }
81 
nullAnnotationsAddition()82   @Test public void nullAnnotationsAddition() {
83     try {
84       ParameterSpec.builder(int.class, "foo").addAnnotations(null);
85       fail();
86     } catch (Exception e) {
87       assertThat(e.getMessage())
88           .isEqualTo("annotationSpecs == null");
89     }
90   }
91 
92   final class VariableElementFieldClass {
93     String name;
94   }
95 
fieldVariableElement()96   @Test public void fieldVariableElement() {
97     TypeElement classElement = getElement(VariableElementFieldClass.class);
98     List<VariableElement> methods = fieldsIn(elements.getAllMembers(classElement));
99     VariableElement element = findFirst(methods, "name");
100 
101     try {
102       ParameterSpec.get(element);
103       fail();
104     } catch (IllegalArgumentException exception) {
105       assertThat(exception).hasMessageThat().isEqualTo("element is not a parameter");
106     }
107   }
108 
109   final class VariableElementParameterClass {
foo(@ullable final String bar)110     public void foo(@Nullable final String bar) {
111     }
112   }
113 
parameterVariableElement()114   @Test public void parameterVariableElement() {
115     TypeElement classElement = getElement(VariableElementParameterClass.class);
116     List<ExecutableElement> methods = methodsIn(elements.getAllMembers(classElement));
117     ExecutableElement element = findFirst(methods, "foo");
118     VariableElement parameterElement = element.getParameters().get(0);
119 
120     assertThat(ParameterSpec.get(parameterElement).toString())
121         .isEqualTo("java.lang.String arg0");
122   }
123 
addNonFinalModifier()124   @Test public void addNonFinalModifier() {
125     List<Modifier> modifiers = new ArrayList<>();
126     modifiers.add(Modifier.FINAL);
127     modifiers.add(Modifier.PUBLIC);
128 
129     try {
130       ParameterSpec.builder(int.class, "foo")
131           .addModifiers(modifiers);
132       fail();
133     } catch (Exception e) {
134       assertThat(e.getMessage()).isEqualTo("unexpected parameter modifier: public");
135     }
136   }
137 
modifyAnnotations()138   @Test public void modifyAnnotations() {
139     ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "foo")
140             .addAnnotation(Override.class)
141             .addAnnotation(SuppressWarnings.class);
142 
143     builder.annotations.remove(1);
144     assertThat(builder.build().annotations).hasSize(1);
145   }
146 
modifyModifiers()147   @Test public void modifyModifiers() {
148     ParameterSpec.Builder builder = ParameterSpec.builder(int.class, "foo")
149             .addModifiers(Modifier.PUBLIC, Modifier.STATIC);
150 
151     builder.modifiers.remove(1);
152     assertThat(builder.build().modifiers).containsExactly(Modifier.PUBLIC);
153   }
154 }
155