Skip to content

Commit

Permalink
feat(#262): fix the bug with java 17 support - text blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Aug 11, 2023
1 parent 4fd2571 commit 425727d
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.lombrozo.testnames.javaparser;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
Expand All @@ -37,6 +38,7 @@
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -54,6 +56,7 @@
*
* @since 0.1.15
*/
@SuppressWarnings("PMD.TooManyMethods")
final class JavaParserClass {

/**
Expand All @@ -63,18 +66,18 @@ final class JavaParserClass {

/**
* Ctor.
* @param stream Input stream with java class.
* @param path Input stream with java class.
*/
JavaParserClass(final Path stream) {
this(JavaParserClass.parse(stream));
JavaParserClass(final Path path) {
this(JavaParserClass.parse(path));
}

/**
* Ctor.
* @param stream Input stream with java class.
*/
JavaParserClass(final InputStream stream) {
this(StaticJavaParser.parse(stream));
this(JavaParserClass.parse(stream));
}

/**
Expand Down Expand Up @@ -269,12 +272,25 @@ private static Node fromCompilation(final CompilationUnit unit) {
*/
private static CompilationUnit parse(final Path path) {
try {
return StaticJavaParser.parse(path);
StaticJavaParser.getParserConfiguration()
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17);
return JavaParserClass.parse(Files.newInputStream(path));
} catch (final IOException ex) {
throw new IllegalStateException(
String.format("Can't parse java file: %s", path.toAbsolutePath()),
ex
);
}
}

/**
* Parse java by input stream.
* @param stream Input stream.
* @return Compilation unit.
*/
private static CompilationUnit parse(final InputStream stream) {
StaticJavaParser.getParserConfiguration()
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17);
return StaticJavaParser.parse(stream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package com.github.lombrozo.testnames.javaparser;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.StaticJavaParser;
import com.github.lombrozo.testnames.TestCase;
import com.github.lombrozo.testnames.TestClass;
import com.github.lombrozo.testnames.TestClassCharacteristics;
Expand Down Expand Up @@ -191,7 +190,7 @@ public TestClassCharacteristics characteristics() {
* @return Parsed class.
*/
private static Sticky<JavaParserClass> parse(final Path path) {
return new Sticky<>(() -> new JavaParserClass(StaticJavaParser.parse(path)));
return new Sticky<>(() -> new JavaParserClass(path));
}

/**
Expand All @@ -200,6 +199,6 @@ private static Sticky<JavaParserClass> parse(final Path path) {
* @return Parsed class.
*/
private static Sticky<JavaParserClass> parse(final InputStream stream) {
return new Sticky<>(() -> new JavaParserClass(StaticJavaParser.parse(stream)));
return new Sticky<>(() -> new JavaParserClass(stream));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ enum JavaTestClasses {
*/
HAMCREST_ASSERT_TRUE_LINE_HITTER("HamcrestAssertTrueHitter.java"),

/**
* Java class with java 17 features.
*/
JAVA_17_TEST("Java17Test.java"),

/**
* Assert true line hitter for junit.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,15 @@ void parsesInterfaceWithSuppressedRules() {
MatcherAssert.assertThat(msg, all, Matchers.hasSize(1));
MatcherAssert.assertThat(msg, all, Matchers.hasItem(expected));
}

@Test
void parsesJavaClassWithTextBlock() {
MatcherAssert.assertThat(
"We expected that test class will parsed successfully and contain only one test case",
JavaTestClasses.JAVA_17_TEST
.toTestClass()
.all(),
Matchers.hasSize(1)
);
}
}
23 changes: 23 additions & 0 deletions src/test/resources/HamcrestAssertTrueHitter.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Volodya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down
43 changes: 43 additions & 0 deletions src/test/resources/Java17Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Volodya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class Java17Test {

final String cql =
"""
CREATE KEYSPACE test
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter1' : 1
};
""";

@Test
void checksQuery() {
MatcherAssert.assertThat("msg", cql, Matchers.notNullValue());
}
}
23 changes: 23 additions & 0 deletions src/test/resources/JunitAssertTrueHitter.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Volodya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
Expand Down

0 comments on commit 425727d

Please sign in to comment.