• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2017 The JavaParser Team.
4  *
5  * This file is part of JavaParser.
6  *
7  * JavaParser can be used either under the terms of
8  * a) the GNU Lesser General Public License as published by
9  *     the Free Software Foundation, either version 3 of the License, or
10  *     (at your option) any later version.
11  * b) the terms of the Apache License
12  *
13  * You should have received a copy of both licenses in LICENCE.LGPL and
14  * LICENCE.APACHE. Please refer to those files for details.
15  *
16  * JavaParser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  */
21 
22 package com.github.javaparser.ast.expr;
23 
24 import com.github.javaparser.ParseProblemException;
25 import com.github.javaparser.ast.CompilationUnit;
26 import com.github.javaparser.ast.ImportDeclaration;
27 import com.github.javaparser.printer.ConcreteSyntaxModel;
28 import org.junit.Test;
29 
30 import static com.github.javaparser.JavaParser.*;
31 import static com.github.javaparser.utils.Utils.EOL;
32 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
33 import static org.junit.Assert.assertEquals;
34 
35 public class NameTest {
36     @Test
outerNameExprIsTheRightMostIdentifier()37     public void outerNameExprIsTheRightMostIdentifier() {
38         Name name = parseName("a.b.c");
39         assertEquals("c", name.getIdentifier());
40     }
41 
42     @Test
parsingAndUnparsingWorks()43     public void parsingAndUnparsingWorks() {
44         Name name = parseName("a.b.c");
45         assertEquals("a.b.c", name.asString());
46     }
47 
48     @Test(expected = ParseProblemException.class)
parsingEmptyNameThrowsException()49     public void parsingEmptyNameThrowsException() {
50         parseName("");
51     }
52 
53     @Test
nameCanHaveAnnotationsInside()54     public void nameCanHaveAnnotationsInside() {
55         Name name = parseName("a.@A b. @C c");
56         assertEquals("a.b.c", name.asString());
57         assertThat(name.getAnnotations()).containsExactly(new MarkerAnnotationExpr("C"));
58         assertThat(name.getQualifier().get().getAnnotations()).containsExactly(new MarkerAnnotationExpr("A"));
59 
60         assertEquals("a.@A b.@C c", name.toString());
61         assertEquals("a.@A b.@C c", ConcreteSyntaxModel.genericPrettyPrint(name));
62     }
63 
64     @Test
importName()65     public void importName() {
66         ImportDeclaration importDeclaration = parseImport("import java.@Abc util.List;");
67 
68         assertThat(importDeclaration.getName().getQualifier().get().getAnnotations()).containsExactly(new MarkerAnnotationExpr("Abc"));
69 
70         assertEquals("import java.@Abc util.List;" + EOL, importDeclaration.toString());
71         assertEquals("import java.@Abc util.List;" + EOL, ConcreteSyntaxModel.genericPrettyPrint(importDeclaration));
72     }
73 
74     @Test
packageName()75     public void packageName() {
76         CompilationUnit cu = parse("package @Abc p1.p2;");
77 
78         assertThat(cu.getPackageDeclaration().get().getName().getQualifier().get().getAnnotations()).containsExactly(new MarkerAnnotationExpr("Abc"));
79 
80         assertEquals("package @Abc p1.p2;" + EOL + EOL, cu.toString());
81         assertEquals("package @Abc p1.p2;" + EOL + EOL, ConcreteSyntaxModel.genericPrettyPrint(cu));
82     }
83 }