View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.annotation;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.annotation.PackageAnnotationCheck.MSG_KEY;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class PackageAnnotationCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/annotation/packageannotation";
37      }
38  
39      /**
40       * This tests a package annotation that is in the package-info.java file.
41       */
42      @Test
43      public void testGoodPackageAnnotation() throws Exception {
44          final DefaultConfiguration checkConfig = createModuleConfig(PackageAnnotationCheck.class);
45  
46          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
47  
48          verify(checkConfig, getNonCompilablePath("package-info.java"), expected);
49      }
50  
51      @Test
52      public void testGetAcceptableTokens() {
53          final PackageAnnotationCheck constantNameCheckObj = new PackageAnnotationCheck();
54          final int[] actual = constantNameCheckObj.getAcceptableTokens();
55          final int[] expected = {TokenTypes.PACKAGE_DEF };
56          Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
57      }
58  
59      @Test
60      public void testAnnotationNotInPackageInfo() throws Exception {
61          final DefaultConfiguration checkConfig = createModuleConfig(PackageAnnotationCheck.class);
62  
63          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
64  
65          verify(checkConfig, getPath("InputPackageAnnotation.java"), expected);
66      }
67  
68      @Test
69      public void testWithoutAnnotation() throws Exception {
70          final DefaultConfiguration checkConfig = createModuleConfig(PackageAnnotationCheck.class);
71  
72          final String[] expected = {
73              "0: " + getCheckMessage(MSG_KEY),
74          };
75  
76          verify(checkConfig, getNonCompilablePath("InputPackageAnnotation2.java"), expected);
77      }
78  }