Issue #49. Added unit tests for generated parse trees of Javadoc comments

This commit is contained in:
Baratali Izmailov 2014-10-19 21:39:05 +04:00
parent ff12c1b012
commit 904a4fb779
32 changed files with 2885 additions and 0 deletions

View File

@ -0,0 +1,301 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2014 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.javadoc;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Assert;
import org.junit.Test;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocLexer;
import com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser;
public class JavadocParseTreeTest
{
private final File folderWithInputFiles = new File(
"src/test/resources/com/puppycrawl/tools/checkstyle/grammars/javadoc");
private JavadocParser parser;
private final BaseErrorListener mErrorListener = new FailOnErrorListener();
public ParseTree parseJavadoc(String aBlockComment)
throws IOException
{
final Charset utf8Charset = Charset.forName("UTF-8");
final InputStream in = new ByteArrayInputStream(aBlockComment.getBytes(utf8Charset));
final ANTLRInputStream input = new ANTLRInputStream(in);
final JavadocLexer lexer = new JavadocLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(mErrorListener);
final CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new JavadocParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(mErrorListener);
return parser.javadoc();
}
public static String getFileContent(File filename)
throws IOException
{
return Files.toString(filename, Charsets.UTF_8);
}
@Test
public void oneSimpleHtmlTag()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/OneSimpleHtmlTag.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeOneSimpleHtmlTag();
compareTrees(expectedTree, generatedTree);
}
@Test
public void textBeforeJavadocTags()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/TextBeforeJavadocTags.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeTextBeforeJavadocTags();
compareTrees(expectedTree, generatedTree);
}
@Test
public void customJavadocTags()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/CustomJavadocTags.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeCustomJavadocTags();
compareTrees(expectedTree, generatedTree);
}
@Test
public void javadocTagDescriptionWithInlineTags()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/JavadocTagDescriptionWithInlineTags.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeJavadocTagDescriptionWithInlineTags();
compareTrees(expectedTree, generatedTree);
}
@Test
public void leadingAsterisks()
throws IOException
{
String filename = folderWithInputFiles + "/LeadingAsterisks.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeLeadingAsterisks();
compareTrees(expectedTree, generatedTree);
}
@Test
public void authorWithMailto()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/AuthorWithMailto.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeAuthorWithMailto();
compareTrees(expectedTree, generatedTree);
}
@Test
public void htmlTagsInParagraph()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/HtmlTagsInParagraph.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeHtmlTagsInParagraph();
compareTrees(expectedTree, generatedTree);
}
@Test
public void linkInlineTags()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/LinkInlineTags.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeLinkInlineTags();
compareTrees(expectedTree, generatedTree);
}
@Test
public void seeReferenceWithFewNestedClasses()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/SeeReferenceWithFewNestedClasses.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeSeeReferenceWithFewNestedClasses();
compareTrees(expectedTree, generatedTree);
}
@Test
public void paramWithGeneric()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/ParamWithGeneric.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeParamWithGeneric();
compareTrees(expectedTree, generatedTree);
}
@Test
public void serial()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/Serial.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeSerial();
compareTrees(expectedTree, generatedTree);
}
@Test
public void since()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/Since.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeSince();
compareTrees(expectedTree, generatedTree);
}
@Test
public void unclosedAndClosedParagraphs()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/UnclosedAndClosedParagraphs.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeUnclosedAndClosedParagraphs();
compareTrees(expectedTree, generatedTree);
}
@Test
public void listWithUnclosedItemInUnclosedParagraph()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/ListWithUnclosedItemInUnclosedParagraph.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeListWithUnclosedItemInUnclosedParagraph();
compareTrees(expectedTree, generatedTree);
}
@Test
public void unclosedParagraphFollowedByJavadocTag()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/UnclosedParagraphFollowedByJavadocTag.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeUnclosedParagraphFollowedByJavadocTag();
compareTrees(expectedTree, generatedTree);
}
@Test
public void allJavadocInlineTags()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/AllJavadocInlineTags.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeAllJavadocInlineTags();
compareTrees(expectedTree, generatedTree);
}
@Test
public void docRootInheritDoc()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/DocRootInheritDoc.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeDocRootInheritDoc();
compareTrees(expectedTree, generatedTree);
}
@Test
public void fewWhiteSpacesAsSeparator()
throws IOException
{
String filename = folderWithInputFiles + "/javadocTags/FewWhiteSpacesAsSeparator.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeFewWhiteSpacesAsSeparator();
compareTrees(expectedTree, generatedTree);
}
@Test
public void mixedCaseOfHtmlTags()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/MixedCaseOfHtmlTags.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeMixedCaseOfHtmlTags();
compareTrees(expectedTree, generatedTree);
}
@Test
public void htmlComments()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/Comments.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeComments();
compareTrees(expectedTree, generatedTree);
}
@Test
public void negativeNumberInAttribute()
throws IOException
{
String filename = folderWithInputFiles + "/htmlTags/NegativeNumberInAttribute.txt";
ParseTree generatedTree = parseJavadoc(getFileContent(new File(filename)));
ParseTree expectedTree = ParseTreeBuilder.treeNegativeNumberInAttribute();
compareTrees(expectedTree, generatedTree);
}
private void compareTrees(ParseTree t1, ParseTree t2)
{
Assert.assertEquals(t1.toStringTree(parser), t2.toStringTree(parser));
}
public static class FailOnErrorListener extends BaseErrorListener
{
@Override
public void syntaxError(
Recognizer<?, ?> aRecognizer, Object aOffendingSymbol,
int aLine, int aCharPositionInLine,
String aMsg, RecognitionException aEx)
{
Assert.fail("[" + aLine + ", " + aCharPositionInLine + "] " + aMsg);
}
}
}

View File

@ -0,0 +1,4 @@
/**
* <a>
*/
class A{}

View File

@ -0,0 +1,3 @@
* Leading asterisk
* Another one

View File

@ -0,0 +1 @@
<a href=http://www.ietf.org/rfc/rfc3758.txt>RFC 3758</a>.

View File

@ -0,0 +1,2 @@
* Use <!-- standard
* java type --> <b>String</b>

View File

@ -0,0 +1,6 @@
* <p>
* <b>Description</b> <i>of</i> my class.
* </p>
* Another line of text

View File

@ -0,0 +1,5 @@
<p>
<ul>
<li>item 1
<li>item 2</li>
</ul>

View File

@ -0,0 +1,10 @@
<P>
<br>
<br/>
<BR>
<BR/>
<TABLE>
<tr></TR>
<TR></tr>
</table>
</p>

View File

@ -0,0 +1,5 @@
<p>
first paragraph
<p>
second paragraph
</p>

View File

@ -0,0 +1,3 @@
<p style="color: red;">
text text
@author Baratali

View File

@ -0,0 +1 @@
{@code <code>} {@literal <literal>} {@docRoot} {@inheritDoc} {@link A A class} {@linkplain A another link} {@value lang.Integer#MAX_VALUE}

View File

@ -0,0 +1,64 @@
@author John Locke
@author Man in black, Sub Zero
@author <a href="mailto:meth@albuquerque.com">Walter White</a>
@deprecated This method is deprecated
because we created new one.
@exception IOException throws exception if connection refused.
@param arg String argument
@param <E> it is generic
@param type I can describe 'type'
on several lines.
One more line of description.
@return something
@return a value of element
that is passed as argument.
@see "A book of that famous man"
@see <a href="http://google.com">Google</a>
@see <a href="http://google.com">
Google</a>
@see java.lang.Integer#valueOf
@see java.lang.Integer#Integer
@see java.lang.Integer#MAX_VALUE
@see java.lang.Integer#valueOf valueOf method
@see java.lang.Integer#Integer Integer
@see java.lang.Integer#MAX_VALUE maxValue
@see java.lang.Integer#intValue method
@see java.lang.Integer#intValue() method
@see java.lang.Void#Void constructor
@see java.lang.Void#Void() constructor
@see java.lang.Integer#valueOf(String) method
@see java.lang.Integer#toString(int,int) method
@see java.lang.Integer#toString(int, int) method
@see #field
@see #method(Type, Type)
@see #method(Type argname, Type argname)
@see #constructor(Type, Type)
@see #constructor(Type argname, Type argname)
@see Class#field
@see Class#method(Type, Type, Type)
@see Class#method(Type argname, Type argname)
@see Class#constructor(Type, Type)
@see Class#constructor(Type argname, Type argname)
@see Class.NestedClass
@see Class
@see com.github.checkstyle.Class#field
@see com.github.checkstyle.Class#method(Type, Type)
@see com.github.checkstyle.Class#method(Type argname, Type argname)
@see com.github.checkstyle.Class#constructor(Type, Type)
@see com.github.checkstyle.Class#constructor(Type argname, Type argname)
@see com.github.checkstyle.Class.NestedClass
@see com.github.checkstyle.Class
@see com.github.checkstyle
@serial field description
on few lines
and even here
@serial include
@serial exclude
@serialField myField myObjectStreamField description of my serial field
@serialData The data-description documents the types and order of data in the serialized form. Specifically, this data includes the optional data written by the writeObject method and all data (including base classes) written by the Externalizable.writeExternal method.
@since 1.5
@since Release 3.4.5
@throws IOException throws exception if connection refused.
@version 1.2
@version Release 1.1.1

View File

@ -0,0 +1 @@
@author <a href="mailto:barataliba@gmail.com">Baratali Izmailov</a>

View File

@ -0,0 +1,4 @@
* @myTag yes it is
* @anotherTagWithoutParameter
* @author I

View File

@ -0,0 +1,10 @@
* {@docRoot}
* {@docRoot }
* {@docRoot
* }
* {@inheritDoc}
* {@inheritDoc }
* {@inheritDoc
* }

View File

@ -0,0 +1 @@
* @param initialCapacity the initial capacity of the ByteBuffer.

View File

@ -0,0 +1 @@
@deprecated use {@link java.lang.Integer Integer{@code <a>Wat</a>}} instead

View File

@ -0,0 +1,2 @@
{@link java.lang.Integer#valueOf}
{@link java.lang.Integer#valueOf valueOf}

View File

@ -0,0 +1,3 @@
* {@link ImmutableSortedMap#of(
*
* )}

View File

@ -0,0 +1 @@
@param <E> my favorite type

View File

@ -0,0 +1 @@
@see java.lang.Integer.Nested.AnotherNested#someMethod(String, int) Description of the link

View File

@ -0,0 +1,7 @@
@serial field description
on few lines
and even here
@serial include
@serial exclude
@serialField myField myObjectStreamField description of my serial field
@serialData The data-description documents

View File

@ -0,0 +1,2 @@
* @since 1.5
* @since Release 3.4.5

View File

@ -0,0 +1,7 @@
* a b
* c
* @see Integer Int
* @author Walter White
* @author John Snow

View File

@ -0,0 +1 @@
{@link #WHITESPACE WHITESPACE}{@link #trimFrom trimFrom}