1/* 2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 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 22package com.github.javaparser.ast; 23 24import com.github.javaparser.JavaParser; 25import org.junit.Test; 26 27import java.io.IOException; 28import java.nio.file.Path; 29import java.nio.file.Paths; 30 31import static com.github.javaparser.JavaParser.parse; 32import static com.github.javaparser.utils.CodeGenerationUtils.mavenModuleRoot; 33import static org.junit.Assert.assertEquals; 34 35public class CompilationUnitTest { 36 @Test 37 public void issue578TheFirstCommentIsWithinTheCompilationUnit() { 38 CompilationUnit compilationUnit = parse("// This is my class, with my comment\n" + 39 "class A {\n" + 40 " static int a;\n" + 41 "}"); 42 43 assertEquals(1, compilationUnit.getAllContainedComments().size()); 44 } 45 46 @Test 47 public void testGetSourceRoot() throws IOException { 48 Path sourceRoot = mavenModuleRoot(CompilationUnitTest.class).resolve(Paths.get("src", "test", "resources")).normalize(); 49 Path testFile = sourceRoot.resolve(Paths.get("com", "github", "javaparser", "storage", "Z.java")); 50 51 CompilationUnit cu = parse(testFile); 52 Path sourceRoot1 = cu.getStorage().get().getSourceRoot(); 53 assertEquals(sourceRoot, sourceRoot1); 54 } 55 56 @Test(expected = RuntimeException.class) 57 public void testGetSourceRootWithBadPackageDeclaration() throws IOException { 58 Path sourceRoot = mavenModuleRoot(CompilationUnitTest.class).resolve(Paths.get("src", "test", "resources")).normalize(); 59 Path testFile = sourceRoot.resolve(Paths.get("com", "github", "javaparser", "storage", "A.java")); 60 61 CompilationUnit cu = parse(testFile); 62 cu.getStorage().get().getSourceRoot(); 63 } 64 65 @Test 66 public void testGetSourceRootInDefaultPackage() throws IOException { 67 Path sourceRoot = mavenModuleRoot(CompilationUnitTest.class).resolve(Paths.get("src", "test", "resources", "com", "github", "javaparser", "storage")).normalize(); 68 Path testFile = sourceRoot.resolve(Paths.get("B.java")); 69 70 CompilationUnit cu = parse(testFile); 71 Path sourceRoot1 = cu.getStorage().get().getSourceRoot(); 72 assertEquals(sourceRoot, sourceRoot1); 73 } 74 75 @Test 76 public void testGetPrimaryTypeName() throws IOException { 77 Path sourceRoot = mavenModuleRoot(CompilationUnitTest.class).resolve(Paths.get("src", "test", "resources")).normalize(); 78 Path testFile = sourceRoot.resolve(Paths.get("com", "github", "javaparser", "storage", "PrimaryType.java")); 79 CompilationUnit cu = JavaParser.parse(testFile); 80 81 assertEquals("PrimaryType", cu.getPrimaryTypeName().get()); 82 } 83 84 @Test 85 public void testNoPrimaryTypeName() { 86 CompilationUnit cu = JavaParser.parse("class PrimaryType{}"); 87 88 assertEquals(false, cu.getPrimaryTypeName().isPresent()); 89 } 90 @Test 91 public void testGetPrimaryType() throws IOException { 92 Path sourceRoot = mavenModuleRoot(CompilationUnitTest.class).resolve(Paths.get("src", "test", "resources")).normalize(); 93 Path testFile = sourceRoot.resolve(Paths.get("com", "github", "javaparser", "storage", "PrimaryType.java")); 94 CompilationUnit cu = JavaParser.parse(testFile); 95 96 assertEquals("PrimaryType", cu.getPrimaryType().get().getNameAsString()); 97 } 98 99 @Test 100 public void testNoPrimaryType() throws IOException { 101 Path sourceRoot = mavenModuleRoot(CompilationUnitTest.class).resolve(Paths.get("src", "test", "resources")).normalize(); 102 Path testFile = sourceRoot.resolve(Paths.get("com", "github", "javaparser", "storage", "PrimaryType2.java")); 103 CompilationUnit cu = JavaParser.parse(testFile); 104 105 assertEquals(false, cu.getPrimaryType().isPresent()); 106 } 107 108} 109