Java Code Examples for com.github.javaparser.StaticJavaParser#parse()

The following examples show how to use com.github.javaparser.StaticJavaParser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PackageDocScanParser.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
private Map<String, JavadocComment> parseDoc(File classFile) {
    Map<String, JavadocComment> classDoc = new HashMap<>();
    try {
        CompilationUnit cu = StaticJavaParser.parse(classFile, StandardCharsets.UTF_8);
        new VoidVisitorAdapter<Object>() {
            @Override
            public void visit(JavadocComment comment, Object arg) {
                super.visit(comment, arg);
                if (comment.getCommentedNode().get() instanceof MethodDeclaration) {
                    MethodDeclaration node = (MethodDeclaration) comment.getCommentedNode().get();
                    classDoc.put(methodName(node), comment);
                }
            }
        }.visit(cu, null);
    } catch (Exception e) {
        logger.info("ERROR PROCESSING ", e);
    }
    return classDoc;
}
 
Example 2
Source File: RefactoringHelperTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
@Test
public void testGetQualifiedMethodSignatureAsString() throws FileNotFoundException, BotRefactoringException {
	configureStaticJavaParserForResolving();

	// arrange
	FileInputStream in = new FileInputStream(getTestResourcesFile());
	CompilationUnit cu = StaticJavaParser.parse(in);
	int lineNumber = TestDataClassRefactoringHelper.getLineOfMethod(true);
	MethodDeclaration targetMethod = RefactoringHelper.getMethodDeclarationByLineNumber(lineNumber, cu);
	assertThat(targetMethod).isNotNull();

	// act
	String qualifiedMethodSignature = RefactoringHelper.getQualifiedMethodSignatureAsString(targetMethod);

	// assert
	assertThat(qualifiedMethodSignature).isEqualTo(
			"de.refactoringbot.resources.refactoringhelper.TestDataClassRefactoringHelper.getLineOfMethod(boolean)");
}
 
Example 3
Source File: RenameMethodTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in an interface
 * implemented by the target class
 * 
 * @throws Exception
 */
@Test
public void testInterfaceRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileOfInterface);
	int lineNumberOfMethodToBeRenamed = renameMethodTestClass.getLineOfInterfaceMethod();
	String newMethodName = "newMethodName";

	// act
	performRenameMethod(filesToConsider, fileOfTestClass, lineNumberOfMethodToBeRenamed, newMethodName);

	// assert that method in interface has been refactored
	CompilationUnit cuRefactoredFileOfInterface = StaticJavaParser.parse(fileOfInterface);
	List<MethodDeclaration> methodDeclarations = cuRefactoredFileOfInterface.findAll(MethodDeclaration.class);
	assertThat(methodDeclarations).size().isEqualTo(1);
	assertThat(methodDeclarations.get(0).getNameAsString()).isEqualTo(newMethodName);
}
 
Example 4
Source File: RefactoringHelperTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
@Test
public void testGetClassOrInterfaceOfMethod() throws FileNotFoundException {
	// arrange
	FileInputStream in = new FileInputStream(getTestResourcesFile());
	CompilationUnit cu = StaticJavaParser.parse(in);
	int lineNumber = TestDataClassRefactoringHelper.getLineOfMethod(true);

	MethodDeclaration methodDeclaration = RefactoringHelper.getMethodDeclarationByLineNumber(lineNumber, cu);
	assertThat(methodDeclaration).isNotNull();

	// act
	ClassOrInterfaceDeclaration classOrInterface = RefactoringHelper.getClassOrInterfaceOfMethod(methodDeclaration);

	// assert
	assertThat(classOrInterface).isNotNull();
	assertThat(classOrInterface.getNameAsString()).isEqualTo(TARGET_CLASS_NAME);
}
 
Example 5
Source File: RefactoringHelperTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
@Test
public void testIsLocalMethodSignatureInClassOrInterface() throws FileNotFoundException {
	// arrange
	FileInputStream in = new FileInputStream(getTestResourcesFile());
	CompilationUnit cu = StaticJavaParser.parse(in);
	Optional<ClassOrInterfaceDeclaration> clazz = cu.getClassByName(TARGET_CLASS_NAME);
	assertThat(clazz).isPresent();

	// act
	boolean actual1 = RefactoringHelper.isLocalMethodSignatureInClassOrInterface(clazz.get(),
			LOCAL_METHOD_SIGNATURE);
	boolean actual2 = RefactoringHelper.isLocalMethodSignatureInClassOrInterface(clazz.get(),
			"not-present-in-class");

	// assert
	assertThat(actual1).isTrue();
	assertThat(actual2).isFalse();
}
 
Example 6
Source File: RemoveMethodParameterTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in an interface
 * implemented by the target class
 * 
 * @throws Exception
 */
@Test
public void testInterfaceRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileOfInterface);
	int lineNumberOfMethodWithParameterToBeRemoved = removeParameterTestClass.getLineOfMethodWithUnusedParameter(0,
			0, 0);
	String parameterName = "b";

	// act
	performRemoveParameter(filesToConsider, fileOfTestClass, lineNumberOfMethodWithParameterToBeRemoved,
			parameterName);

	// assert that method in interface has been refactored
	CompilationUnit cuRefactoredFileOfInterface = StaticJavaParser.parse(fileOfInterface);
	List<MethodDeclaration> methodDeclarations = cuRefactoredFileOfInterface.findAll(MethodDeclaration.class);
	assertThat(methodDeclarations).size().isEqualTo(1);
	assertThat(methodDeclarations.get(0).getParameterByName(parameterName).isPresent()).isFalse();
}
 
Example 7
Source File: RenameMethodTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in the sibling class of
 * the target class
 * 
 * @throws Exception
 */
@Test
public void testSiblingClassRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileOfSiblingClass);
	int lineNumberOfMethodToBeRenamed = renameMethodTestClass.getLineOfMethodToBeRenamed(true);
	String newMethodName = "newMethodName";

	// act
	performRenameMethod(filesToConsider, fileOfTestClass, lineNumberOfMethodToBeRenamed, newMethodName);

	// assert
	CompilationUnit cuRefactoredFileOfSiblingClass = StaticJavaParser.parse(fileOfSiblingClass);

	// assert that target's sibling has been refactored
	int lineNumberOfMethodInSiblingClass = renameMethodSiblingClass.getLineOfMethodToBeRenamed(true);
	assertThatMethodNameIsEqualToExpected(cuRefactoredFileOfSiblingClass, lineNumberOfMethodInSiblingClass,
			newMethodName);

	// assert that caller method in target's sibling class has been refactored
	int lineNumberOfCallerMethodInSiblingClass = renameMethodSiblingClass.getLineNumberOfCallerInSiblingClass();
	assertThatNumberOfMethodCallsIsEqualToExpected(cuRefactoredFileOfSiblingClass,
			lineNumberOfCallerMethodInSiblingClass, newMethodName, 1);
}
 
Example 8
Source File: RefactoringHelperTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
@Test
public void testGetMethodByLineNumberOfMethodName() throws FileNotFoundException {
	// arrange
	FileInputStream in = new FileInputStream(getTestResourcesFile());
	CompilationUnit cu = StaticJavaParser.parse(in);
	int lineNumber = TestDataClassRefactoringHelper.getLineOfMethod(true);

	// act
	MethodDeclaration method = RefactoringHelper.getMethodDeclarationByLineNumber(lineNumber, cu);

	// assert
	assertThat(method).isNotNull();
	assertThat(method.getDeclarationAsString()).isEqualTo("public static int getLineOfMethod(boolean parm)");

	// act
	boolean isMethodDeclarationAtLineNumber = RefactoringHelper.isMethodDeclarationAtLine(method, lineNumber);

	// assert
	assertThat(isMethodDeclarationAtLineNumber).isTrue();
}
 
Example 9
Source File: RenameMethodTest.java    From Refactoring-Bot with MIT License 6 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in the sub class of the
 * target class (descendant)
 * 
 * @throws Exception
 */
@Test
public void testSubClassRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileOfSubClass);
	int lineNumberOfMethodToBeRenamed = renameMethodTestClass.getLineOfMethodToBeRenamed(true);
	String newMethodName = "newMethodName";

	// act
	performRenameMethod(filesToConsider, fileOfTestClass, lineNumberOfMethodToBeRenamed, newMethodName);

	// assert that target's sub class has been refactored
	CompilationUnit cuRefactoredFileOfSubClass = StaticJavaParser.parse(fileOfSubClass);
	int lineNumberOfMethodInSubClass = renameMethodSubClass.getLineOfMethodToBeRenamed(true);
	assertThatMethodNameIsEqualToExpected(cuRefactoredFileOfSubClass, lineNumberOfMethodInSubClass, newMethodName);
}
 
Example 10
Source File: PackageInfoReader.java    From jig with Apache License 2.0 6 votes vote down vote up
Optional<PackageAlias> read(PackageInfoSource packageInfoSource) {
    CompilationUnit cu = StaticJavaParser.parse(packageInfoSource.toInputStream());

    Optional<PackageIdentifier> optPackageIdentifier = cu.getPackageDeclaration()
            .map(NodeWithName::getNameAsString)
            .map(PackageIdentifier::new);

    Optional<Alias> optAlias = getJavadoc(cu)
            .map(Javadoc::getDescription)
            .map(JavadocDescription::toText)
            .map(JavadocAliasSource::new)
            .map(JavadocAliasSource::toAlias);

    return optPackageIdentifier.flatMap(packageIdentifier -> optAlias.map(alias ->
            new PackageAlias(packageIdentifier, alias)));
}
 
Example 11
Source File: RemoveMethodParameterTest.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in a different class
 * which contains a method calling the refactored target method
 * 
 * @throws Exception
 */
@Test
public void testCallerClassRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileWithCallerMethod);
	int lineNumberOfMethodWithParameterToBeRemoved = removeParameterTestClass.getLineOfMethodWithUnusedParameter(0,
			0, 0);
	String parameterName = "b";

	CompilationUnit cuOriginalFileOfTestClass = StaticJavaParser.parse(fileOfTestClass);
	CompilationUnit cuOriginalFileWithCallerMethod = StaticJavaParser.parse(fileWithCallerMethod);
	MethodDeclaration originalMethod = RefactoringHelper.getMethodDeclarationByLineNumber(
			lineNumberOfMethodWithParameterToBeRemoved, cuOriginalFileOfTestClass);
	MethodDeclaration originalCallerMethodInDifferentFile = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterCallerTestClass.getLineOfCallerMethodInDifferentFile(), cuOriginalFileWithCallerMethod);

	SoftAssertions softAssertions = new SoftAssertions();
	softAssertions.assertThat(originalMethod).isNotNull();
	softAssertions.assertThat(originalCallerMethodInDifferentFile).isNotNull();
	softAssertions.assertAll();

	// act
	performRemoveParameter(filesToConsider, fileOfTestClass, lineNumberOfMethodWithParameterToBeRemoved,
			parameterName);

	// assert
	CompilationUnit cuRefactoredFileOfTestClass = StaticJavaParser.parse(fileOfTestClass);
	CompilationUnit cuRefactoredFileWithCallerMethod = StaticJavaParser.parse(fileWithCallerMethod);
	MethodDeclaration refactoredMethod = getMethodByName(TARGET_CLASS_NAME, originalMethod.getNameAsString(),
			cuRefactoredFileOfTestClass);

	// assert that caller method in different file has been refactored
	MethodDeclaration methodInDifferentFileWithTargetMethodCalls = getMethodByName(CALL_OF_TARGET_METHOD_CLASS_NAME,
			originalCallerMethodInDifferentFile.getNameAsString(), cuRefactoredFileWithCallerMethod);
	assertThat(methodInDifferentFileWithTargetMethodCalls).isNotNull();
	assertAllMethodCallsArgumentSizeEqualToRefactoredMethodParameterCount(
			methodInDifferentFileWithTargetMethodCalls, refactoredMethod);
}
 
Example 12
Source File: RemoveMethodParameterTest.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in the sub class of the
 * target class (descendant)
 * 
 * @throws Exception
 */
@Test
public void testSubClassRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileOfSubClass);
	int lineNumberOfMethodWithParameterToBeRemoved = removeParameterTestClass.getLineOfMethodWithUnusedParameter(0,
			0, 0);
	String parameterName = "b";

	CompilationUnit cuOriginalFileOfSubClass = StaticJavaParser.parse(fileOfSubClass);
	MethodDeclaration originalMethodInSubClass = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterSubClass.getLineOfMethodWithUnusedParameter(0, 0, 0), cuOriginalFileOfSubClass);
	assertThat(originalMethodInSubClass).isNotNull();

	// act
	performRemoveParameter(filesToConsider, fileOfTestClass, lineNumberOfMethodWithParameterToBeRemoved,
			parameterName);

	// assert that target's sub class has been refactored
	CompilationUnit cuRefactoredFileOfSubClass = StaticJavaParser.parse(fileOfSubClass);
	String methodInSubClassName = originalMethodInSubClass.getNameAsString();
	MethodDeclaration methodInSubClass = getMethodByName(SUB_CLASS_NAME, methodInSubClassName,
			cuRefactoredFileOfSubClass);
	assertThat(methodInSubClass).isNotNull();
	assertThat(methodInSubClass.getParameterByName(parameterName).isPresent()).isFalse();
}
 
Example 13
Source File: ReorderModifiersTest.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
@Test
public void testReorderMethodModifiers() throws Exception {
	// arrange
	int lineOfMethod = TestDataClassReorderModifiers.getLineOfMethodWithStaticAndFinalInWrongOrder();

	// act
	File tempFile = performReorderModifiers(lineOfMethod);

	// assert
	FileInputStream in = new FileInputStream(tempFile);
	CompilationUnit cu = StaticJavaParser.parse(in);
	MethodDeclaration methodDeclarationAfterRefactoring = RefactoringHelper
			.getMethodDeclarationByLineNumber(lineOfMethod, cu);
	assertAllModifiersInCorrectOrder(methodDeclarationAfterRefactoring.getModifiers());
}
 
Example 14
Source File: RenameMethodTest.java    From Refactoring-Bot with MIT License 5 votes vote down vote up
/**
 * Two classes sharing the same interface should only lead to a refactoring in
 * both classes, if the common interface declares the target method signature.
 * This is not given in this test case
 * 
 * @throws Exception
 */
@Test
public void testTwoClassesWithSameMethodSigAndEmptyInterface() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClassImplementingEmptyInterface);
	filesToConsider.add(fileOfEmptyInterface);
	int lineNumberOfMethodToBeRenamed = renameMethodTestClassWithEmptyInterfaceImpl.getLineOfMethodToBeRenamed();
	String newMethodName = "newMethodName";

	CompilationUnit cuOriginalFileOfTestClassImplementingEmptyInterface = StaticJavaParser
			.parse(fileOfTestClassImplementingEmptyInterface);
	MethodDeclaration originalMethodInInnerClass = RefactoringHelper.getMethodDeclarationByLineNumber(
			renameMethodTestClassWithEmptyInterfaceImpl.getLineOfMethodToBeRenamed(),
			cuOriginalFileOfTestClassImplementingEmptyInterface);

	assertThat(originalMethodInInnerClass).isNotNull();

	// act
	performRenameMethod(filesToConsider, fileOfTestClassImplementingEmptyInterface, lineNumberOfMethodToBeRenamed,
			newMethodName);

	// assert
	CompilationUnit cuRefactoredFileOfTestClassImplementingEmptyInterface = StaticJavaParser
			.parse(fileOfTestClassImplementingEmptyInterface);

	// assert that method in outer class has been refactored
	assertThatMethodNameIsEqualToExpected(cuRefactoredFileOfTestClassImplementingEmptyInterface,
			lineNumberOfMethodToBeRenamed, newMethodName);

	// assert that inner class method remained unchanged
	int lineNumberOfInnerClassMethod = renameMethodInnerClassWithEmptyInterfaceImpl.getLineOfMethodToBeRenamed();
	assertThatMethodNameIsEqualToExpected(cuRefactoredFileOfTestClassImplementingEmptyInterface,
			lineNumberOfInnerClassMethod, originalMethodInInnerClass.getNameAsString());
}
 
Example 15
Source File: ClassReader.java    From jig with Apache License 2.0 5 votes vote down vote up
TypeSourceResult read(JavaSource javaSource) {
    CompilationUnit cu = StaticJavaParser.parse(javaSource.toInputStream());

    String packageName = cu.getPackageDeclaration()
            .map(PackageDeclaration::getNameAsString)
            .map(name -> name + ".")
            .orElse("");

    ClassVisitor typeVisitor = new ClassVisitor(packageName);
    cu.accept(typeVisitor, null);

    return typeVisitor.toTypeSourceResult();
}
 
Example 16
Source File: TestUtils.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public static void assertValidJavaSourceCode(String javaSourceCode, String filename) {
    try {
        CompilationUnit compilation = StaticJavaParser.parse(javaSourceCode);
        assertTrue(compilation.getTypes().size() > 0, "File: " + filename);
    }
    catch (ParseProblemException ex) {
        fail("Java parse problem: " + filename, ex);
    }
}
 
Example 17
Source File: RemoveMethodParameterTest.java    From Refactoring-Bot with MIT License 4 votes vote down vote up
/**
 * Test whether the refactoring was performed correctly in the target class
 * 
 * @throws Exception
 */
@Test
public void testTargetClassRefactored() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	int lineNumberOfMethodWithParameterToBeRemoved = removeParameterTestClass.getLineOfMethodWithUnusedParameter(0,
			0, 0);
	String parameterName = "b";

	CompilationUnit cuOriginalFileOfTestClass = StaticJavaParser.parse(fileOfTestClass);
	MethodDeclaration originalMethod = RefactoringHelper.getMethodDeclarationByLineNumber(
			lineNumberOfMethodWithParameterToBeRemoved, cuOriginalFileOfTestClass);
	MethodDeclaration originalDummyMethod = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterTestClass.getLineNumberOfDummyMethod(0, 0, 0), cuOriginalFileOfTestClass);
	MethodDeclaration originalCallerMethod = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterTestClass.getLineNumberOfCaller(), cuOriginalFileOfTestClass);
	MethodDeclaration originalCallerMethodInnerClass = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterInnerTestClass.getLineNumberOfCallerInInnerClass(), cuOriginalFileOfTestClass);
	MethodDeclaration originalMethodWithTargetMethodSignatureInInnerClass = RefactoringHelper
			.getMethodDeclarationByLineNumber(
					removeParameterInnerTestClass.getLineOfMethodWithUnusedParameter(0, 0, 0),
					cuOriginalFileOfTestClass);

	SoftAssertions softAssertions = new SoftAssertions();
	softAssertions.assertThat(originalMethod).isNotNull();
	softAssertions.assertThat(originalDummyMethod).isNotNull();
	softAssertions.assertThat(originalCallerMethod).isNotNull();
	softAssertions.assertThat(originalCallerMethodInnerClass).isNotNull();
	softAssertions.assertThat(originalMethodWithTargetMethodSignatureInInnerClass).isNotNull();
	softAssertions.assertAll();

	// act
	performRemoveParameter(filesToConsider, fileOfTestClass, lineNumberOfMethodWithParameterToBeRemoved,
			parameterName);

	// assert
	CompilationUnit cuRefactoredFileOfTestClass = StaticJavaParser.parse(fileOfTestClass);
	MethodDeclaration refactoredMethod = getMethodByName(TARGET_CLASS_NAME, originalMethod.getNameAsString(),
			cuRefactoredFileOfTestClass);

	// assert that parameter has been removed from the target method
	assertThat(refactoredMethod).isNotNull();
	assertThat(refactoredMethod.getParameterByName(parameterName).isPresent()).isFalse();

	// assert that parameter has been removed from the Javadoc
	assertParameterNotPresentInJavadoc(refactoredMethod, parameterName);

	// assert that dummy method is unchanged
	MethodDeclaration dummyMethod = getMethodByName(TARGET_CLASS_NAME, originalDummyMethod.getNameAsString(),
			cuRefactoredFileOfTestClass);
	assertThat(dummyMethod).isNotNull();
	assertThat(dummyMethod.getParameterByName(parameterName)).isPresent();

	// assert that inner class method with same name as target method is unchanged
	MethodDeclaration methodWithTargetMethodSignatureInInnerClass = getMethodByName(TARGET_INNER_CLASS_NAME,
			originalMethodWithTargetMethodSignatureInInnerClass.getNameAsString(), cuRefactoredFileOfTestClass);
	assertThat(methodWithTargetMethodSignatureInInnerClass).isNotNull();
	assertThat(methodWithTargetMethodSignatureInInnerClass.getParameterByName(parameterName)).isPresent();

	// assert that caller method in same file has been refactored
	MethodDeclaration methodWithTargetMethodCalls = getMethodByName(TARGET_CLASS_NAME,
			originalCallerMethod.getNameAsString(), cuRefactoredFileOfTestClass);
	assertThat(methodWithTargetMethodCalls).isNotNull();
	assertAllMethodCallsArgumentSizeEqualToRefactoredMethodParameterCount(methodWithTargetMethodCalls,
			refactoredMethod);

	// assert that caller method in same file in inner class has been refactored
	MethodDeclaration methodWithTargetMethodCallInInnerClass = getMethodByName(TARGET_INNER_CLASS_NAME,
			originalCallerMethodInnerClass.getNameAsString(), cuRefactoredFileOfTestClass);
	assertThat(methodWithTargetMethodCallInInnerClass).isNotNull();
	assertAllMethodCallsArgumentSizeEqualToRefactoredMethodParameterCount(methodWithTargetMethodCallInInnerClass,
			refactoredMethod);
}
 
Example 18
Source File: ExtendsTest.java    From butterfly with MIT License 4 votes vote down vote up
private void test(String resourceName, Class superClass, boolean expects) throws ParseException {
    InputStream resourceAsStream = this.getClass().getResourceAsStream(resourceName);
    CompilationUnit compilationUnit = StaticJavaParser.parse(resourceAsStream);
    Extends extendsObj = new Extends(superClass);
    Assert.assertEquals(extendsObj.evaluate(compilationUnit), expects);
}
 
Example 19
Source File: RemoveMethodParameterTest.java    From Refactoring-Bot with MIT License 4 votes vote down vote up
/**
 * Test that a refactoring of an inner class (implementing the same interface as
 * the outer class) results in a correct refactoring of both, the inner and the
 * outer class. Also test whether the implemented interface and super class of
 * the outer class was successfully refactored
 * 
 * @throws Exception
 */
@Test
public void testInnerClassWithInterfaceRefactoring() throws Exception {
	// arrange
	List<File> filesToConsider = new ArrayList<File>();
	filesToConsider.add(fileOfTestClass);
	filesToConsider.add(fileOfInterface);
	filesToConsider.add(fileOfSuperClass);
	int lineNumberOfMethodWithParameterToBeRemoved = removeParameterInnerClassWithInterfaceImpl
			.getLineOfMethodWithUnusedParameter(0, 0, 0);
	String parameterName = "b";

	CompilationUnit cuOriginalFileOfTestClass = StaticJavaParser.parse(fileOfTestClass);
	CompilationUnit cuOriginalFileOfSuperClass = StaticJavaParser.parse(fileOfSuperClass);
	MethodDeclaration originalInnerClassMethod = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterInnerClassWithInterfaceImpl.getLineOfMethodWithUnusedParameter(0, 0, 0),
			cuOriginalFileOfTestClass);
	MethodDeclaration originalOuterClassMethod = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterTestClass.getLineOfMethodWithUnusedParameter(0, 0, 0), cuOriginalFileOfTestClass);
	MethodDeclaration originalMethodInSuperClass = RefactoringHelper.getMethodDeclarationByLineNumber(
			removeParameterSuperClass.getLineOfMethodWithUnusedParameter(0, 0, 0), cuOriginalFileOfSuperClass);

	SoftAssertions softAssertions = new SoftAssertions();
	softAssertions.assertThat(originalInnerClassMethod).isNotNull();
	softAssertions.assertThat(originalOuterClassMethod).isNotNull();
	softAssertions.assertThat(originalMethodInSuperClass).isNotNull();
	softAssertions.assertAll();

	// act
	performRemoveParameter(filesToConsider, fileOfTestClass, lineNumberOfMethodWithParameterToBeRemoved,
			parameterName);

	// assert
	CompilationUnit cuRefactoredFileOfTestClass = StaticJavaParser.parse(fileOfTestClass);

	// assert that method in inner class has been refactored
	MethodDeclaration refactoredInnerClassMethod = getMethodByName(TARGET_INNER_CLASS_WITH_INTERFACE_NAME,
			originalInnerClassMethod.getNameAsString(), cuRefactoredFileOfTestClass);
	assertThat(refactoredInnerClassMethod).isNotNull();
	assertThat(refactoredInnerClassMethod.getParameterByName(parameterName).isPresent()).isFalse();

	// assert that method in outer class has been refactored
	MethodDeclaration refactoredOuterClassMethod = getMethodByName(TARGET_CLASS_NAME,
			originalOuterClassMethod.getNameAsString(), cuRefactoredFileOfTestClass);
	assertThat(refactoredOuterClassMethod).isNotNull();
	assertThat(refactoredOuterClassMethod.getParameterByName(parameterName).isPresent()).isFalse();

	// assert that method in interface has been refactored
	CompilationUnit cuRefactoredFileOfInterface = StaticJavaParser.parse(fileOfInterface);
	List<MethodDeclaration> methodDeclarations = cuRefactoredFileOfInterface.findAll(MethodDeclaration.class);
	assertThat(methodDeclarations).size().isEqualTo(1);
	assertThat(methodDeclarations.get(0).getParameterByName(parameterName).isPresent()).isFalse();

	// assert that super class of outer class has been refactored
	CompilationUnit cuRefactoredFileOfSuperClass = StaticJavaParser.parse(fileOfSuperClass);
	String methodInSuperClassName = originalMethodInSuperClass.getNameAsString();
	MethodDeclaration methodInSuperClass = getMethodByName(SUPER_CLASS_NAME, methodInSuperClassName,
			cuRefactoredFileOfSuperClass);
	assertThat(methodInSuperClass).isNotNull();
	assertThat(methodInSuperClass.getParameterByName(parameterName).isPresent()).isFalse();
}
 
Example 20
Source File: AnnotatedWithTest.java    From butterfly with MIT License 4 votes vote down vote up
@BeforeClass
public void beforeClass() throws ParseException {
    InputStream resourceAsStream = this.getClass().getResourceAsStream("/test-app/src/main/java/com/testapp/Application.java");
    compilationUnit = StaticJavaParser.parse(resourceAsStream);
}