Skip to content

Commit

Permalink
fix usage of asserts where order or condition is missleading
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Oct 7, 2024
1 parent 0d7e5c5 commit 6cd0c8e
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -102,9 +101,7 @@ public void testExepectNoProposals() throws Exception {
List<ICompletionProposal> proposals= computeCompletionProposals(cu, completionIndex);

boolean fail= proposals.stream().anyMatch(p -> "new_class - create new class".equals(p.getDisplayString()));
if (fail) {
fail("Proposal '" + propDisplay + "' should not exist");
}
org.junit.Assert.assertFalse("Proposal '" + propDisplay + "' should not exist", fail);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jdt.ui.tests.refactoring;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -62,7 +61,7 @@ private void failHelper2(int startLine, int startColumn, int endLine, int endCol

assertNotNull("precondition was supposed to fail", result);
if(checkMsg)
assertTrue(errorMsg.equals(result.getEntryMatchingSeverity(RefactoringStatus.FATAL).getMessage()));
org.junit.Assert.assertEquals(errorMsg, result.getEntryMatchingSeverity(RefactoringStatus.FATAL).getMessage());
}
//--- TESTS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,8 @@ private ISourceRange findSelectionInSource(String source) throws Exception {
int begin= source.indexOf(SELECTION_START_HERALD) + SELECTION_START_HERALD.length();
int end= source.indexOf(SELECTION_END_HERALD);

if (begin < SELECTION_START_HERALD.length())
fail("No selection start comment in input source file!");
if (end < 0)
fail("No selection end comment in input source file!");
org.junit.Assert.assertFalse("No selection start comment in input source file!", begin < SELECTION_START_HERALD.length());
org.junit.Assert.assertFalse("No selection end comment in input source file!", end < 0);

return new SourceRange(begin, end-begin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ public void testArrayParameterAndReturnType() throws Exception {
public void testMethodNotFound() throws Exception {
//Method cannot be found
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 2, 0, 2, 1);
assertTrue(status.getEntryWithHighestSeverity().getMessage().equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_on_this_selection));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_on_this_selection);
}

@Test
public void testIsConstructor() throws Exception {
//Check if Constructor
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 2, 12, 2, 15);
assertTrue(status.getEntryWithHighestSeverity().getMessage().equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_constructors));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_constructors);
}

@Test
Expand Down Expand Up @@ -197,61 +197,57 @@ public void testMultipleFilesInSameProject() throws Exception {
public void testRecursive() throws Exception {
//MethodInvocation in MethodDeclaration with object of the same Class in parameter
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 3, 10, 3, 13);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods);
}

@Test
public void testRecursive2() throws Exception {
//recursive invocation after invoking a method that returns a new instance of the same class
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 6, 10, 6, 13);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods);
}

@Test
public void testRecursive3() throws Exception {
//simple recursive invocation of instance method
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 2, 17, 2, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods);
}

@Test
public void testInheritance() throws Exception {
//Refactor of method that overrides method of supertype (Selection is set to MethodDeclaration)
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 4, 19, 4, 22);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_explicit_super_method_invocation));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_explicit_super_method_invocation);
}

@Test
public void testInheritance2() throws Exception {
//Refactor of method in super type that has child type overriding the method -> should fail
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SuperClass", "p.SubClass" }, 3, 19, 3, 22);
assertTrue(status.getEntryWithHighestSeverity().getMessage().equals(RefactoringCoreMessages.MakeStaticRefactoring_method_is_overridden_in_subtype));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_method_is_overridden_in_subtype);
}

@Test
public void testInheritance3() throws Exception {
//Selecting SuperMethodInvocation -> should fail
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 5, 26, 5, 29);
assertTrue(status.getEntryWithHighestSeverity().getMessage().equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_super_method_invocations));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_super_method_invocations);
}

@Test
public void testInheritance4() throws Exception {
//Refactor method without parameters on the lowest hierarchy level ->
//After refactoring it is static but has the same signature as parent type method -> should fail
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 4, 19, 4, 22);
assertTrue(status.getEntryWithHighestSeverity().getMessage().equals(RefactoringCoreMessages.MakeStaticRefactoring_hiding_method_of_parent_type));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_hiding_method_of_parent_type);
}

@Test
public void testInheritance5() throws Exception {
//Inheritance with Recursion
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 4, 10, 4, 13);
assertTrue(status.getEntryWithHighestSeverity().getMessage().equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods);
}

@Test
Expand All @@ -265,8 +261,7 @@ public void testDuplicateParamName() throws Exception {
public void testDuplicateMethod() throws Exception {
//Selected method has instance usage and there is an existing method that is equal to the selected method after being refactored
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 5, 19, 5, 22);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_duplicate_method_signature));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_duplicate_method_signature);
}

@Test
Expand All @@ -280,8 +275,7 @@ public void testDuplicateMethod2() throws Exception {
public void testMethodAlreadyStatic() throws Exception {
//Selected method is already static
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 2, 24, 2, 27);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_method_already_static));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_method_already_static);
}

@Test
Expand Down Expand Up @@ -353,16 +347,14 @@ public void testGenericDeclaration8() throws Exception {
public void testGenericDeclaration9() throws Exception {
//check for wildcardTypes as bounds (T extends List<?>)
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 7, 17, 7, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_wildCardTypes_as_bound));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_wildCardTypes_as_bound);
}

@Test
public void testGenericDeclaration10() throws Exception {
//check for wildcardTypes as bounds (T extends Map<? extends Runnable, ? extends Throwable>)
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 7, 17, 7, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_wildCardTypes_as_bound));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_wildCardTypes_as_bound);
}

@Test
Expand Down Expand Up @@ -419,8 +411,7 @@ public void testMethodCallInNestedAnonymousClass() throws Exception {
public void testVariousInstanceCases() throws Exception {
//Various cases of instance access in many different forms
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 14, 17, 14, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_selected_method_uses_super_field_access));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_selected_method_uses_super_field_access);
}

@Test
Expand Down Expand Up @@ -455,8 +446,7 @@ public void testPassingInstanceReference() throws Exception {
public void testSuperMethodReference() throws Exception {
//Selected method is used in SuperMethodReference -> Should throw error
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SuperClass", "p.SubClass" }, 4, 19, 4, 22);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references);
}

@Test
Expand All @@ -477,8 +467,7 @@ public void testReturnField() throws Exception {
public void testExplicitSuperMethodInvocation() throws Exception {
//MethodDeclaration uses explcit SuperMethodInvocation to call method of parent type -> semantic change not allowed
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 3, 17, 3, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_explicit_super_method_invocation));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_explicit_super_method_invocation);
}

@Test
Expand All @@ -492,8 +481,7 @@ public void testImplicitSuperMethodInvocation() throws Exception {
public void testSuperFieldAccess() throws Exception {
//MethodDeclaration uses SuperFieldAccess -> throws warning but is possible
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.SubClass", "p.SuperClass" }, 6, 17, 6, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_selected_method_uses_super_field_access));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_selected_method_uses_super_field_access);
}

@Test
Expand All @@ -505,8 +493,7 @@ public void testConcatenatedFieldAccessAndQualifiedNames() throws Exception {
@Test
public void testSourceNotAvailable() throws Exception {
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 3, 20, 3, 27);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_source_not_available_for_selected_method));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_source_not_available_for_selected_method);
}

@Test
Expand All @@ -527,8 +514,7 @@ public void testClassInstanceCreation() throws Exception {
public void testConvertMethodReferenceToLambda() throws Exception {
//MethodReference needs to be co0nverted to lambda because refactored method accepts two parameters
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 10, 10, 10, 13);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references);
}

@Test
Expand All @@ -541,32 +527,28 @@ public void testNested() throws Exception {
public void testMethodReference() throws Exception {
//TypeMethodReference
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 8, 10, 8, 13);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references);
}

@Test
public void testMethodReference2() throws Exception {
//ExpressionMethodReference in anonymous class -> Refactoring not allowed in anonymous class and method references also not allowed
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 4, 26, 4, 29);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_local_or_anonymous_types));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_local_or_anonymous_types);
}

@Test
public void testMethodReference3() throws Exception {
//ExpressionMethodReference with recursion
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 2, 17, 2, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_recursive_methods);
}

@Test
public void testMethodReference4() throws Exception {
//ExpressionMethodReference
RefactoringStatus status= performRefactoringAndMatchFiles(new String[] { "p.Foo" }, 8, 17, 8, 20);
assertTrue(status.getEntryWithHighestSeverity().getMessage()
.equals(RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references));
org.junit.Assert.assertEquals(status.getEntryWithHighestSeverity().getMessage(), RefactoringCoreMessages.MakeStaticRefactoring_not_available_for_method_references);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.eclipse.jdt.ui.tests.hover;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -204,7 +203,7 @@ int check (String value, String[] strings) {

// value should be expanded:
int index= actualHtmlContent.indexOf("<pre><code>");
assertFalse(index == -1);
org.junit.Assert.assertNotEquals(-1, index);
String actualSnippet= actualHtmlContent.substring(index, index + expectedCodeSequence.length());
assertEquals("sequence doesn't match", expectedCodeSequence, actualSnippet);
}
Expand Down Expand Up @@ -255,7 +254,7 @@ int check (String value, String[] strings) {

// value should be expanded:
int index= actualHtmlContent.indexOf("<pre>{");
assertFalse(index == -1);
org.junit.Assert.assertNotEquals(-1, index);
String actualSnippet= actualHtmlContent.substring(index, index + expectedCodeSequence.length());
assertEquals("sequence doesn't match", expectedCodeSequence, actualSnippet);
}
Expand Down Expand Up @@ -319,7 +318,7 @@ int check (String value, String[] strings) {

// value should be expanded:
int index= actualHtmlContent.indexOf("<pre><code>");
assertFalse(index == -1);
org.junit.Assert.assertNotEquals(-1, index);
String actualSnippet= actualHtmlContent.substring(index, index + expectedCodeSequence.length());
assertEquals("sequence doesn't match", actualSnippet, expectedCodeSequence);
}
Expand Down
Loading

0 comments on commit 6cd0c8e

Please sign in to comment.