Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test helper for refaster checks #697

Merged
merged 13 commits into from
Jul 18, 2019
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
.project
.settings
build
/out/
out/
gradle.properties
docs/node_modules/
generated_src
3 changes: 3 additions & 0 deletions baseline-refaster-rules/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ apply from: "${rootDir}/gradle/publish-jar.gradle"

dependencies {
compile 'com.google.errorprone:error_prone_refaster'

testCompile 'junit:junit'
testCompile project(':baseline-refaster-testing')
}
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();",
"}");
}

}
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);",
"}");
}

}
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());",
"}");
}

}
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\");",
"}");
}

}
29 changes: 29 additions & 0 deletions baseline-refaster-testing/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* (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: 'nebula.maven-publish'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah also don't apply this, publish-jar does the right thing here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of

// Intentionally not applying nebula.maven-publish, but most of its constituent plugins,
// because we do _not_ want nebula.maven-compile-only

apply plugin: 'nebula.source-jar'
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'
}
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();

}

}
Loading