Skip to content

Commit

Permalink
Migrate Expression tests to JUnit 4
Browse files Browse the repository at this point in the history
Most of the tests in org.eclipse.core.expressions.tests are still based
on JUnit 3, i.e., implement the TestCase class. This change migrates the
test classes and the according test suite to JUnit 4.
  • Loading branch information
HeikoKlare committed Oct 23, 2023
1 parent 08a2b40 commit e854297
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
*******************************************************************************/
package org.eclipse.core.internal.expressions.tests;

import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ PropertyTesterTests.class, ExpressionTests.class, ExpressionInfoTests.class,
CountExpressionTest.class })
public class AllExpressionTests {

public static Test suite() {
TestSuite suite= new TestSuite("All Expression Language Tests"); //$NON-NLS-1$
suite.addTest(PropertyTesterTests.suite());
suite.addTest(new JUnit4TestAdapter(ExpressionTests.class));
suite.addTest(ExpressionInfoTests.suite());
suite.addTest(CountExpressionTest.suite());
return suite;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@
*******************************************************************************/
package org.eclipse.core.internal.expressions.tests;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import org.eclipse.core.expressions.CountExpression;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;

import org.eclipse.core.runtime.CoreException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class CountExpressionTest extends TestCase {
public static Test suite() {
return new TestSuite(CountExpressionTest.class);
}
public class CountExpressionTest {

private static EvaluationContext evaluationContext(int size) {
List<Integer> variable = new ArrayList<>(size + 1);
Expand All @@ -40,56 +35,64 @@ private static EvaluationContext evaluationContext(int size) {
return new EvaluationContext(null, variable);
}

@Test
public void testNoneExpression() throws CoreException {
CountExpression e = new CountExpression("!"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(0)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(1)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(0)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(1)));
}

@Test
public void testNoneOrOneExpression() throws CoreException {
CountExpression e = new CountExpression("?"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(0)));
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(0)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
}

@Test
public void testExactExpression() throws CoreException {
CountExpression e = new CountExpression("5"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(7)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(7)));
}

@Test
public void testAnyNumberExpression() throws CoreException {
CountExpression e = new CountExpression("*"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
}

@Test
public void testLessThanOrEqualToExpression() throws CoreException {
CountExpression e = new CountExpression("-3]"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
}

@Test
public void testLessThanExpression() throws CoreException {
CountExpression e = new CountExpression("-3)"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(3)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(1)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(3)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(4)));
}

@Test
public void testGreaterThanOrEqualToExpression() throws CoreException {
CountExpression e = new CountExpression("[3-"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(3)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
}

@Test
public void testGreaterThanExpression() throws CoreException {
CountExpression e = new CountExpression("(3-"); //$NON-NLS-1$
Assert.assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(3)));
Assert.assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
assertEquals(EvaluationResult.TRUE, e.evaluate(evaluationContext(5)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(3)));
assertEquals(EvaluationResult.FALSE, e.evaluate(evaluationContext(2)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@
*******************************************************************************/
package org.eclipse.core.internal.expressions.tests;

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

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

import org.eclipse.core.expressions.AndExpression;
import org.eclipse.core.expressions.CountExpression;
import org.eclipse.core.expressions.EqualsExpression;
Expand All @@ -30,26 +37,19 @@
import org.eclipse.core.internal.expressions.ResolveExpression;
import org.eclipse.core.internal.expressions.SystemTestExpression;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;


@SuppressWarnings("restriction")
public class ExpressionInfoTests extends TestCase {

public static Test suite() {
return new TestSuite(ExpressionInfoTests.class);
}
public class ExpressionInfoTests {

// ---- test merging ------------------------------------------------------------------------

@Test
public void testMergeEmpty() {
ExpressionInfo info= new ExpressionInfo();
info.merge(new ExpressionInfo());
assertNoAccess(info);
}

@Test
public void testMergeDefaultVariable() {
ExpressionInfo info;
ExpressionInfo other;
Expand All @@ -73,6 +73,7 @@ public void testMergeDefaultVariable() {
assertDefaultAccessOnly(info);
}

@Test
public void testMergeSystemProperty() {
ExpressionInfo info;
ExpressionInfo other;
Expand All @@ -96,6 +97,7 @@ public void testMergeSystemProperty() {
assertSystemPropertyOnly(info);
}

@Test
public void testMergeVariableNames() {
ExpressionInfo info;
ExpressionInfo other;
Expand Down Expand Up @@ -126,6 +128,7 @@ public void testMergeVariableNames() {
assertVariableAccess(info, new String[] {"variable_one", "variable_two"});
}

@Test
public void testMergePropertyNames() {
ExpressionInfo info;
ExpressionInfo other;
Expand Down Expand Up @@ -156,6 +159,7 @@ public void testMergePropertyNames() {
assertPropertyAccess(info, new String[] { "prop1", "prop2" }, false);
}

@Test
public void testMergeMisbehavingExpressionTypes() {
ExpressionInfo info;
ExpressionInfo other;
Expand Down Expand Up @@ -188,55 +192,66 @@ public void testMergeMisbehavingExpressionTypes() {

// ---- test expression ---------------------------------------------------------------------

@Test
public void testCountExpression() {
assertDefaultAccessOnly((new CountExpression("10")).computeExpressionInfo());
}

@Test
public void testEqualsExpression() {
assertDefaultAccessOnly((new EqualsExpression(new Object())).computeExpressionInfo());
}

@Test
public void testInstanceofExpression() {
assertDefaultAccessOnly((new InstanceofExpression("java.lang.Object")).computeExpressionInfo());
}

@Test
public void testNotExpression() {
assertDefaultAccessOnly((new NotExpression(new CountExpression("10"))).computeExpressionInfo());
}

@Test
public void testSystemExpression() {
assertSystemPropertyOnly((new SystemTestExpression("property", "value")).computeExpressionInfo());
}

@Test
public void testTestExpression() {
assertPropertyAccess((new TestExpression("namespace", "property", null,
new Object())).computeExpressionInfo(), "namespace.property", true);
}

// ---- composite expressions ---------------------------------------------------------

@Test
public void testAdaptExpression() throws Exception {
assertDefaultAccessOnly(new AdaptExpression("java.lang.Object").computeExpressionInfo());
}

@Test
public void testAndExpression() throws Exception {
AndExpression and= new AndExpression();
assertNoAccess(and.computeExpressionInfo());
and.add(new CountExpression("10"));
assertDefaultAccessOnly(and.computeExpressionInfo());
}

@Test
public void testIterateExpression() throws Exception {
assertDefaultAccessOnly(new IterateExpression("or").computeExpressionInfo());
}

@Test
public void testResolveExpression() {
ResolveExpression resolve= new ResolveExpression("variable", null);
assertNoAccess(resolve.computeExpressionInfo());
resolve.add(new CountExpression("10"));
assertVariableAccess(resolve.computeExpressionInfo(), "variable");
}

@Test
public void testWithExpression() {
WithExpression with= new WithExpression("variable");
assertNoAccess(with.computeExpressionInfo());
Expand Down
Loading

0 comments on commit e854297

Please sign in to comment.