-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A check for author tag is implemented. Some tests for it are added.
Signed-off-by: Mihaela Memova <[email protected]>
- Loading branch information
Mihaela K. Memova
authored and
Mihaela K. Memova
committed
Apr 5, 2017
1 parent
06671a3
commit 178812f
Showing
7 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/org/openhab/tools/analysis/checkstyle/AuthorTagCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) 2010-2017 by the respective copyright holders. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.tools.analysis.checkstyle; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck; | ||
|
||
/** | ||
* Checks if a class/interface/enumeration has an author tag | ||
* | ||
* @author Mihaela Memova | ||
* | ||
*/ | ||
public class AuthorTagCheck extends WriteTagCheck { | ||
|
||
/** | ||
* Indicates whether the inner classes/interfaces/enumerations (briefly | ||
* called units) should be checked for an author tag. It is a configuration | ||
* property and can be changed through the check's configuration. | ||
*/ | ||
private boolean checkInnerUnits; | ||
|
||
public void setCheckInnerUnits(boolean checkInnerUnits) { | ||
this.checkInnerUnits = checkInnerUnits; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* <p> | ||
* Calls the {@link WriteTagCheck#visitToken(DetailAST)} taking into | ||
* consideration the check configuration | ||
*/ | ||
@Override | ||
public void visitToken(DetailAST ast) { | ||
if (!checkInnerUnits) { | ||
DetailAST astParent = ast.getParent(); | ||
// if outer class/interface/enum | ||
if (astParent == null) { | ||
super.visitToken(ast); | ||
} | ||
} else { | ||
super.visitToken(ast); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/test/java/org/openhab/tools/analysis/checkstyle/test/AuthorTagCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.openhab.tools.analysis.checkstyle.test; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Test; | ||
import org.openhab.tools.analysis.checkstyle.AuthorTagCheck; | ||
import org.openhab.tools.analysis.checkstyle.api.AbstractStaticCheckTest; | ||
|
||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration; | ||
import com.puppycrawl.tools.checkstyle.utils.CommonUtils; | ||
|
||
/** | ||
* Tests for {@link AuthorTagCheck} | ||
* | ||
* @author Mihaela Memova | ||
* | ||
*/ | ||
public class AuthorTagCheckTest extends AbstractStaticCheckTest { | ||
|
||
private static final String EXPECTED_WARNING_MESSAGE = "An author tag is missing"; | ||
|
||
@Test | ||
public void testOuterClassWithNoAuthorTag() throws Exception { | ||
|
||
String fileName = "NoAuthorOuterAndInnerClasses.java"; | ||
/* | ||
* an error is expected at the line where the outer class is declared | ||
* in the file | ||
*/ | ||
int warningLine = 4; | ||
boolean checkInnerClasses = false; | ||
checkFileForAuthorTags(checkInnerClasses, fileName, warningLine); | ||
} | ||
|
||
@Test | ||
public void testOuterAndInnerClassesWithNoAuthorTag() throws Exception { | ||
|
||
String fileName = "NoAuthorOuterAndInnerClasses.java"; | ||
/* | ||
* errors are expected at the lines where the classes are declared in | ||
* the file | ||
*/ | ||
int firstWarningLine = 4; | ||
int secondWarningLine = 9; | ||
boolean checkInnerClasses = true; | ||
checkFileForAuthorTags(checkInnerClasses, fileName, firstWarningLine, secondWarningLine); | ||
} | ||
|
||
@Test | ||
public void testOuterClasWithNoJavaDoc() throws Exception { | ||
|
||
String fileName = "NoJavaDocOuterAndInnerClasses.java"; | ||
/* | ||
* an error is expected at the line where the outer class is declared | ||
* in the file | ||
*/ | ||
int warningLine = 1; | ||
boolean checkInnerClasses = false; | ||
checkFileForAuthorTags(checkInnerClasses, fileName, warningLine); | ||
} | ||
|
||
@Test | ||
public void testOuterAndInnerClassesWithNoJavaDoc() throws Exception { | ||
|
||
String fileName = "NoJavaDocOuterAndInnerClasses.java"; | ||
/* | ||
* errors are expected at the lines where the classes are declared in | ||
* the file | ||
*/ | ||
int firstWarningLine = 1; | ||
int secondWarningLine = 3; | ||
boolean checkInnerClasses = true; | ||
checkFileForAuthorTags(checkInnerClasses, fileName, firstWarningLine, secondWarningLine); | ||
} | ||
|
||
@Test | ||
public void testOuterAndInnerClassesWithPresentAuthorTag() throws Exception { | ||
|
||
String fileName = "PresentAuthorTagOuterAndInnerClasses.java"; | ||
boolean checkInnerClasses = true; | ||
// no errors are expected so we don't pass any warning lines | ||
checkFileForAuthorTags(checkInnerClasses, fileName); | ||
} | ||
|
||
private void checkFileForAuthorTags(boolean checkInnerUnits, String fileName, Integer... warningLine) | ||
throws Exception { | ||
|
||
String filePath = getPath("authorTagCheckTest"+ File.separator + fileName); | ||
String[] expected = null; | ||
if (warningLine.length > 0) { | ||
expected = new String[warningLine.length]; | ||
for (int i = 0; i < warningLine.length; i++) { | ||
expected[i] = warningLine[i] + ": " + EXPECTED_WARNING_MESSAGE; | ||
} | ||
} else { | ||
expected = CommonUtils.EMPTY_STRING_ARRAY; | ||
} | ||
|
||
DefaultConfiguration configuration = createConfiguration(checkInnerUnits); | ||
verify(configuration, filePath, expected); | ||
} | ||
|
||
private DefaultConfiguration createConfiguration(boolean checkInnerUnits) { | ||
|
||
DefaultConfiguration configuration = createCheckConfig(AuthorTagCheck.class); | ||
/* | ||
* Modify the configuration with the needed attributes and message. They | ||
* should be the same as their corresponding properties defined in | ||
* rulesets.checkstyle/rules.xml file | ||
*/ | ||
configuration.addAttribute("tag", "@author"); | ||
configuration.addAttribute("tagFormat", "\\S"); | ||
configuration.addAttribute("tagSeverity", "ignore"); | ||
configuration.addAttribute("checkInnerUnits", String.valueOf(checkInnerUnits)); | ||
configuration.addMessage("type.missingTag", EXPECTED_WARNING_MESSAGE); | ||
|
||
return configuration; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/resources/checks/checkstyle/authorTagCheckTest/NoAuthorOuterAndInnerClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* java-doc of the outer class | ||
*/ | ||
class OuterClass { | ||
|
||
/** | ||
* java-doc of the inner class | ||
*/ | ||
private class InnerClass { | ||
|
||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/test/resources/checks/checkstyle/authorTagCheckTest/NoJavaDocOuterAndInnerClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class NoAuthorOuterClass { | ||
|
||
private class NoAuthorInnerClass { | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../resources/checks/checkstyle/authorTagCheckTest/PresentAuthorTagOuterAndInnerClasses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* | ||
* @author Author of the outer class | ||
* | ||
*/ | ||
public class OuterClass { | ||
|
||
/** | ||
* @author author of the inner class | ||
*/ | ||
private class InnerClass { | ||
|
||
} | ||
} |