-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test helper for refaster checks (#697)
- Loading branch information
1 parent
9efd8cc
commit f83a019
Showing
11 changed files
with
413 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
.project | ||
.settings | ||
build | ||
/out/ | ||
out/ | ||
gradle.properties | ||
docs/node_modules/ | ||
generated_src |
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
40 changes: 40 additions & 0 deletions
40
...e-refaster-rules/src/test/java/com/palantir/baseline/refaster/CollectionsIsEmptyTest.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,40 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class CollectionsIsEmptyTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(CollectionsIsEmpty.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.util.ArrayList;", | ||
"public class Test {", | ||
" boolean empty = new ArrayList<>().size() == 0;", | ||
"}") | ||
.hasOutputLines( | ||
"import java.util.ArrayList;", | ||
"public class Test {", | ||
" boolean empty = new ArrayList<>().isEmpty();", | ||
"}"); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...faster-rules/src/test/java/com/palantir/baseline/refaster/OptionalOrElseSupplierTest.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,44 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class OptionalOrElseSupplierTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(OptionalOrElseSupplier.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.util.*;", | ||
"import java.util.function.Supplier;", | ||
"public class Test {", | ||
" Supplier<String> supplier = () -> \"hello\";", | ||
" String s = Optional.ofNullable(\"world\").orElse(supplier.get());", | ||
"}") | ||
.hasOutputLines( | ||
"import java.util.*;", | ||
"import java.util.function.Supplier;", | ||
"public class Test {", | ||
" Supplier<String> supplier = () -> \"hello\";", | ||
" String s = Optional.ofNullable(\"world\").orElseGet(supplier);", | ||
"}"); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/SortedFirstTest.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,45 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class SortedFirstTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(SortedFirst.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.util.*;", | ||
"import java.util.stream.Stream;", | ||
"public class Test {", | ||
" Optional<Integer> i = Arrays.asList(5, -10, 7, -18, 23).stream()", | ||
" .sorted(Comparator.reverseOrder())", | ||
" .findFirst();", | ||
"}") | ||
.hasOutputLines( | ||
"import java.util.*;", | ||
"import java.util.stream.Stream;", | ||
"public class Test {", | ||
" Optional<Integer> i = Arrays.asList(5, -10, 7, -18, 23).stream()" | ||
+ ".min(Comparator.reverseOrder());", | ||
"}"); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/Utf8LengthTest.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,41 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class Utf8LengthTest { | ||
|
||
@Test | ||
public void test() { | ||
RefasterTestHelper | ||
.forRefactoring(Utf8Length.class) | ||
.withInputLines( | ||
"Test", | ||
"import java.nio.charset.StandardCharsets;", | ||
"public class Test {", | ||
" int i = \"hello world\".getBytes(StandardCharsets.UTF_8).length;", | ||
"}") | ||
.hasOutputLines( | ||
"import com.google.common.base.Utf8;", | ||
"import java.nio.charset.StandardCharsets;", | ||
"public class Test {", | ||
" int i = Utf8.encodedLength(\"hello world\");", | ||
"}"); | ||
} | ||
|
||
} |
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,27 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
apply plugin: 'java-library' | ||
apply from: "${rootDir}/gradle/publish-jar.gradle" | ||
|
||
dependencies { | ||
compile 'com.google.errorprone:error_prone_refaster' | ||
compile 'com.google.errorprone:error_prone_test_helpers' | ||
|
||
compile 'com.google.guava:guava' | ||
compile 'junit:junit' | ||
compile 'org.assertj:assertj-core' | ||
} |
88 changes: 88 additions & 0 deletions
88
baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/CompilerUtility.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,88 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://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 com.palantir.baseline.refaster; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.io.CharStreams; | ||
import com.sun.source.tree.CompilationUnitTree; | ||
import com.sun.tools.javac.api.JavacTaskImpl; | ||
import com.sun.tools.javac.api.JavacTool; | ||
import com.sun.tools.javac.util.Context; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import javax.tools.Diagnostic; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardJavaFileManager; | ||
|
||
final class CompilerUtility { | ||
|
||
private CompilerUtility() {} | ||
|
||
static CompilerResult compile(JavaFileObject javaFileObject) { | ||
JavaCompiler compiler = JavacTool.create(); | ||
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>(); | ||
StandardJavaFileManager fileManager = | ||
compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, StandardCharsets.UTF_8); | ||
|
||
JavacTaskImpl task = (JavacTaskImpl) compiler.getTask( | ||
CharStreams.nullWriter(), | ||
fileManager, | ||
diagnosticsCollector, | ||
ImmutableList.of(), | ||
null, | ||
ImmutableList.of(javaFileObject)); | ||
|
||
Iterable<? extends CompilationUnitTree> trees; | ||
try { | ||
trees = task.parse(); | ||
task.analyze(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
return new CompilerResult() { | ||
@Override | ||
public Context context() { | ||
return task.getContext(); | ||
} | ||
|
||
@Override | ||
public List<CompilationUnitTree> compilationUnits() { | ||
return ImmutableList.copyOf(trees); | ||
} | ||
|
||
@Override | ||
public List<Diagnostic<? extends JavaFileObject>> diagnostics() { | ||
return diagnosticsCollector.getDiagnostics(); | ||
} | ||
}; | ||
} | ||
|
||
interface CompilerResult { | ||
|
||
Context context(); | ||
|
||
List<CompilationUnitTree> compilationUnits(); | ||
|
||
List<Diagnostic<? extends JavaFileObject>> diagnostics(); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.