org.openrewrite.java.tree.JavadocTest Maven / Gradle / Ivy
Show all versions of rewrite-java-tck Show documentation
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.tree;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.java.MinimumJava11;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;
import static org.openrewrite.java.Assertions.java;
@SuppressWarnings({"JavadocDeclaration", "TrailingWhitespacesInTextBlock", "TextBlockMigration", "RedundantThrows", "ConcatenationWithEmptyString"})
class JavadocTest implements RewriteTest {
@SuppressWarnings("JavadocReference")
@Test
void javadocs() {
rewriteRun(
java(
"""
import java.util.List;
/**
* {@link List#add(Object) } refers to import
* @param Something that spans
* multiple lines.
*/
class Test {
/** the position of the first body element or tag is relative to the index beginning after the last contiguous whitespace following '**' */
Integer n;
/**
* {@link int}
*/
class Inner {
Integer n;
/**
* {@link #n} refers to Inner
*/
void test() {
}
}
}
"""
)
);
}
@Test
void singleLineJavadocText() {
rewriteRun(
java(
"""
/** test */
class Test {}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1076")
@Test
void javaDocWithMultipleLeadingAsterisks() {
rewriteRun(
java(
"""
/** ** * First '*' characters are treated as a margin until a non '*' is parsed.
** * @throws IOException validate cursor position.
*/
class Test {
}
"""
)
);
}
// All blank **********************************************************
@Test
void allBlank() {
rewriteRun(
java(
"""
/** */
class Test {
}
"""
)
);
}
@Test
void allBlankMultiline() {
rewriteRun(
java(
"""
/**
*
*
*/
class Test {
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/965")
@Test
void emptyJavadoc() {
rewriteRun(
java(
"""
class Test {
/***/
void empty() {}
/**
*/
void onlyNewLine() {}
}
"""
)
);
}
// Javadoc Annotations **********************************************
// author
@Test
void author() {
rewriteRun(
java(
"""
/**
* @author name
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void authorPostFixedNumber() {
rewriteRun(
java(
"""
/**
* @author FirstName LastName 42
*
*/
public class A {
void method() {}
}
"""
)
);
}
// code
@Test
void code() {
rewriteRun(
java(
"""
/** {@code int n = 1; } */
class Test {
}
"""
)
);
}
// deprecated
@Test
void deprecated() {
rewriteRun(
java(
"""
/**
* @deprecated reason
*/
public class A {
void method() {}
}
"""
)
);
}
// docComment
@Test
void htmlComment() {
rewriteRun(
java(
"""
/** */
class Test {
}
"""
)
);
}
// docRoot
@Test
void docRoot() {
rewriteRun(
java(
"""
/**
* See the Copyright.
*/
class Test {
}
"""
)
);
}
// docType
@Test
void doctype() {
rewriteRun(
java(
"""
/** test */
class Test {
}
"""
)
);
}
// attributes ()
@Test
void multilineAttribute() {
rewriteRun(
java(
"""
/**
*
* label.
*/
class Test {
}
"""
)
);
}
// elements (
or ).
@Issue("https://github.com/openrewrite/rewrite/issues/1026")
@Test
void selfClosingHTMLElement() {
rewriteRun(
java(
"""
/**
*
* text
*/
class Test {
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1047")
@Test
void preserveWhitespaceBeforeHTMLElement() {
rewriteRun(
java(
"""
/**
*
*
* text
* text
*/
class Test {
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1089")
@Test
void whitespaceBeforeSelfClosingElement() {
rewriteRun(
java(
"""
package org.foo;
/**
* Type of an Opening Time.
*
* - DELIVERY (text a)
* - PICKUP (text b)
*
*/
public enum OpenTimeType {
DELIVERY,
PICKUP
}
"""
)
);
}
// erroneous
@Test
void multipleLineErroneous() {
rewriteRun(
java(
"""
/**
* @see this
* or that
*/
class Test {
}
"""
)
);
}
@Test
void erroneous() {
rewriteRun(
java(
"""
/** {@version this is an erroneous tag } */
class Test {
}
"""
)
);
}
@Test
void unknownTags() {
rewriteRun(
java(
"""
/**
* See the {@unknown}.
* @unknown uh oh
*/
class Test {
}
"""
)
);
}
// entity
@Test
void htmlEntity() {
rewriteRun(
java(
"""
/** & & ; */
class Test {
}
"""
)
);
}
@SuppressWarnings("JavadocReference")
@Test
void exception() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
java(
"""
public class A {
/**
* @exception ex
*/
void method() {}
}
"""
)
);
}
@Test
void hidden() {
rewriteRun(
java(
"""
/**
* @hidden value
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void indexOnly() {
rewriteRun(
java(
"""
/**
* {@index}
* {@index
*/
public class A {
void method() {}
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/971")
@Test
void indexNoDescription() {
rewriteRun(
java(
"""
/**
* {@index term}
* {@index term
*/
class Test {
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/971")
@Test
void indexTermAndDescription() {
rewriteRun(
java(
"""
/**
* {@index term description}
* {@index term description
*/
class Test {
}
"""
)
);
}
@Test
void inheritDoc() {
rewriteRun(
java(
"""
class Test {
/**
* {@inheritDoc}
* @return {@inheritDoc}
*/
void test() {
}
}
"""
)
);
}
// link
// linkplain
@Test
void noMarginJavadocFirstLineTrailingWhitespace() {
rewriteRun(
java(
"""
/**
{@link int}
*/
class Test {}
"""
)
);
}
@Test
void leftMargin() {
rewriteRun(
java(
"""
class Test {
/**
{@link int}
*/
String s;
}
"""
)
);
}
@Test
void noMarginJavadoc() {
rewriteRun(
java(
"""
/**
{@link int}
*/
class Test {}
"""
)
);
}
@Test
void singleLineJavadoc() {
rewriteRun(
java(
"""
/** {@link int} */
class Test {}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1274")
@Test
void whitespaceBeforeAndAfterDelimiter() {
rewriteRun(
java(
"""
import java.util.Map;
/**
* {@link Map< String , Integer > }
*/
class Test {
}
"""
)
);
}
@Test
void fullyQualifiedLink() {
rewriteRun(
java(
"""
/**
* {@link java.util.List}
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void fullyQualifiedParameterizedTypeLink() {
rewriteRun(
java(
"""
/**
* {@link java.util.List}
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void fullyQualifiedMethodLink() {
rewriteRun(
java(
"""
/**
* {@link java.util.List#add(Object) }
*/
class Test {
}
"""
)
);
}
@Test
void fieldLink() {
rewriteRun(
java(
"""
import java.nio.charset.StandardCharsets;
/**
* {@link StandardCharsets#UTF_8 }
* {@linkplain StandardCharsets#UTF_8 }
*/
class Test {
}
"""
)
);
}
@Test
void thisFieldLink() {
rewriteRun(
java(
"""
/**
* {@link #n }
*/
class Test {
int n;
}
"""
)
);
}
@Test
void thisMethodLink() {
rewriteRun(
java(
"""
/**
* {@link #test() }
*/
class Test {
void test() {}
}
"""
)
);
}
@Test
void primitiveLink() {
rewriteRun(
java(
"""
/**
* Line 1
* Line 2
* {@link int}
* @param t
*/
class Test {}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1274")
@Test
void multiParameterizedType() {
rewriteRun(
java(
"""
import java.util.Map;
/**
* {@link Map>} multiple parameterized type
*/
class Test {
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1274")
@Test
void parameterizedType() {
rewriteRun(
java(
"""
import java.util.List;
class Test {
/**
* @return - {@link List} - description.
* @throws Exception - exception.
*/
List method() throws Exception {
}
}
"""
)
);
}
@SuppressWarnings("JavadocReference")
@Test
void multipleReferenceParameters() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
java(
"""
class Test {
/**
* {@link ListenerUtils#getExceptionFromHeader(ConsumerRecord, String, LogAccessor)}
*/
void test() {
}
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/963")
@Test
void blankLink() {
rewriteRun(
java(
"""
/**
* {@link}
*/
class Test {
}
"""
)
);
}
@SuppressWarnings("JavadocReference")
@Issue("https://github.com/openrewrite/rewrite/issues/968")
@Test
void missingBracket() {
rewriteRun(
java(
"""
/**
* {@link missing.bracket
*/
class Test {
}
"""
)
);
}
@Test
void methodReferenceNoParameters() {
rewriteRun(
java(
"""
interface Test {
/**
* {@linkplain Thread#interrupt}
*/
boolean test();
}
"""
)
);
}
@SuppressWarnings("JavadocReference")
@Issue("https://github.com/openrewrite/rewrite/issues/967")
@Test
void multilineLink() {
rewriteRun(
java(
"""
/**
* {@link
* multiline}.
*/
class Test {
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/964")
@Test
void constructorLink() {
rewriteRun(
java(
"""
class Test {
/**
* {@link java.util.List()}
*/
void test() {
}
}
"""
)
);
}
// literal
@Test
void literal() {
rewriteRun(
java(
"""
public class A {
/**
* @literal
*/
void method() {}
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/942")
@Test
void nullLiteral() {
rewriteRun(
java(
"""
interface Test {
/**
* {@literal null}.
*/
boolean test();
}
"""
)
);
}
@Test
void emptyParam() {
rewriteRun(
java(
"""
public class A {
/**
* @param
*/
void method(int val) {}
}
"""
)
);
}
@Test
void param() {
rewriteRun(
java(
"""
public class A {
/**
* @param val
*/
void method(int val) {}
}
"""
)
);
}
@Test
void singleLineParam() {
rewriteRun(
java(
"""
/** @param t */
class Test {}
"""
)
);
}
@Test
void lineBreakInParam() {
rewriteRun(
java(
"""
interface Test {
/**
* @param <
* T> t hi
* @param
* val
*/
boolean test(int val);
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/1412")
@Test
void paramWithMultilineHtmlAttributeNewLineBeforeEquals() {
rewriteRun(
java(
"""
interface Test {
/**
* @param contentType label
*/
boolean test(int contentType);
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/941")
@Test
void paramWithMultilineHtmlAttribute() {
rewriteRun(
java(
"""
interface Test {
/**
* @param contentType
* label
*/
boolean test(int contentType);
}
"""
)
);
}
@Test
void descriptionOnNewLine() {
rewriteRun(
java(
"""
public class Test {
/**
* @param name
* a name
*/
void test(String name) {
}
}
"""
)
);
}
// provides
@Test
void provide() {
rewriteRun(
java(
"""
public class A {
/**
* @provides int
*/
void method(int val) {}
}
"""
)
);
}
// return
@Test
void returnTag() {
rewriteRun(
java(
"""
public class A {
/**
* @return id
*/
int method(int val) {}
}
"""
)
);
}
@Test
void returnWithoutMargin() {
rewriteRun(
java(
"""
public class Test {
/** Text
@return No margin
*/
public int test() {
return 0;
}
}
"""
)
);
}
// see
@Test
void see() {
rewriteRun(
java(
"""
/**
* @see "Create link via quotes"
* @see java.lang.Comparable#compareTo(Object) label
* @see label
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void methodFound() {
rewriteRun(
java(
"""
interface Test {
/**
* @see java.io.ByteArrayOutputStream#toString(String)
*/
boolean test();
}
"""
)
);
}
@SuppressWarnings("JavadocReference")
@Issue("https://github.com/openrewrite/rewrite/issues/945")
@Test
void methodNotFound() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().methodInvocations(false).build()),
java(
"""
interface Test {
/**
* @see Math#cosine(double)
*/
boolean test();
}
"""
)
);
}
@SuppressWarnings("JavadocReference")
@Issue("https://github.com/openrewrite/rewrite/issues/944")
@Test
void typeNotFound() {
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.builder().identifiers(false).build()),
java(
"""
interface Test {
/**
* {@link SymbolThatCannotBeFound}
* @see Mathy#cos(double)
*/
boolean test();
}
"""
)
);
}
@Issue("https://github.com/openrewrite/rewrite/issues/941")
@Test
void seeWithMultilineAttribute() {
rewriteRun(
java(
"""
interface Test {
/**
* @see
* label
*/
boolean test();
}
"""
)
);
}
@Test
void serial() {
rewriteRun(
java(
"""
/**
* @serial
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void serialData() {
rewriteRun(
java(
"""
/**
* @serialData
*/
public class A {
void method() {}
}
"""
)
);
}
@Test
void since() {
rewriteRun(
java(
"""
/** @since 1.0 */
class Test {
}
"""
)
);
}
@Test
void summary() {
rewriteRun(
java(
"""
/** {@summary test description } */
class Test {
}
"""
)
);
}
@Test
void uses() {
rewriteRun(
java(
"""
/** @uses Test for something */
class Test {
/** @uses Test for something */
void method() {}
}
"""
)
);
}
@Test
void throwsException() {
rewriteRun(
java(
"""
class Test {
/**
* @throws Exception
*/
T test(String t) throws Exception {
return null;
}
}
"""
)
);
}
// value
@Test
void value() {
rewriteRun(
java(
"""
class Test {
/**
* The value of this constant is {@value}.
*/
public static final String SCRIPT_START = "