Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit 539abd5

Browse files
committed
Merge pull request #65 from sschaef/add-junit-rule
Add junit rule
2 parents 8cc9d71 + 3bf7817 commit 539abd5

File tree

7 files changed

+75
-43
lines changed

7 files changed

+75
-43
lines changed

org.scala-refactoring.library/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
33
Bundle-Name: Scala Refactoring
44
Bundle-SymbolicName: org.scala-refactoring.library
55
Bundle-Version: 0.6.3.qualifier
6-
Require-Bundle: org.junit;bundle-version="4.7.0",
6+
Require-Bundle: org.junit;bundle-version="4.11.0",
77
org.scala-lang.scala-library
88
Export-Package: scala.tools.refactoring,scala.tools.refactoring.analys
99
is,scala.tools.refactoring.common,scala.tools.refactoring.implementat
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scala.tools.refactoring.tests.util;
2+
3+
import org.junit.rules.TestRule;
4+
import org.junit.runner.Description;
5+
import org.junit.runners.model.Statement;
6+
7+
import scala.tools.nsc.util.FailedInterrupt;
8+
9+
/**
10+
* In case an assertion error is caught and wrapped by another error type (as it
11+
* is the case for exceptions that are thrown on the compiler thread), we need
12+
* to manually unwrap them later, in order to let JUnit "see" them.
13+
*/
14+
public final class ExceptionWrapper implements TestRule {
15+
16+
@Override
17+
public Statement apply(final Statement base, final Description description) {
18+
return new Statement() {
19+
@Override
20+
public void evaluate() throws Throwable {
21+
try {
22+
base.evaluate();
23+
} catch (FailedInterrupt e) {
24+
// a FailedInterrupt is thrown when an exception occurs on the compiler thread
25+
throw e.getCause();
26+
}
27+
}
28+
};
29+
}
30+
}
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package scala.tools.refactoring.tests.util;
22

3-
import org.junit.Rule;
3+
import org.junit.Assume;
44
import org.junit.rules.MethodRule;
55
import org.junit.runners.model.FrameworkMethod;
66
import org.junit.runners.model.Statement;
7+
78
import scala.util.Properties;
8-
import org.junit.Assume;
99

10-
abstract public class ScalaVersionTestRule {
10+
public class ScalaVersionTestRule implements MethodRule {
1111

1212
final class EmptyStatement extends Statement {
1313
@Override
@@ -16,23 +16,19 @@ public void evaluate() throws Throwable {
1616
}
1717
}
1818

19-
@Rule
20-
public MethodRule rule = new MethodRule() {
21-
22-
public Statement apply(Statement stmt, FrameworkMethod meth, Object arg2) {
23-
ScalaVersion onlyOn = meth.getAnnotation(ScalaVersion.class);
19+
public Statement apply(Statement stmt, FrameworkMethod meth, Object arg2) {
20+
ScalaVersion onlyOn = meth.getAnnotation(ScalaVersion.class);
2421

25-
if (onlyOn != null) {
26-
if (!onlyOn.doesNotMatch().isEmpty() && Properties.versionString().contains(onlyOn.doesNotMatch())) {
27-
return new EmptyStatement();
28-
} else if (Properties.versionString().contains(onlyOn.matches())) {
29-
return stmt;
30-
} else{
31-
return new EmptyStatement();
32-
}
33-
} else {
22+
if (onlyOn != null) {
23+
if (!onlyOn.doesNotMatch().isEmpty() && Properties.versionString().contains(onlyOn.doesNotMatch())) {
24+
return new EmptyStatement();
25+
} else if (Properties.versionString().contains(onlyOn.matches())) {
3426
return stmt;
27+
} else {
28+
return new EmptyStatement();
3529
}
30+
} else {
31+
return stmt;
3632
}
37-
};
33+
}
3834
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package scala.tools.refactoring.tests.util;
2+
3+
import org.junit.Rule;
4+
5+
public abstract class TestRules {
6+
7+
// all rules need to be public fields
8+
9+
@Rule
10+
public final ScalaVersionTestRule rule1 = new ScalaVersionTestRule();
11+
12+
@Rule
13+
public final ExceptionWrapper rule2 = new ExceptionWrapper();
14+
}

org.scala-refactoring.library/src/test/scala/scala/tools/refactoring/tests/sourcegen/ReusingPrinterTest.scala

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
2424
}
2525

2626
final implicit class ImplicitTreeHelper(original: Tree) {
27+
/** Needs to be executed on the PC thread. */
2728
def printsTo(expectedOutput: String): Unit = {
2829
val sourceFile = new BatchSourceFile("noname", expectedOutput)
2930
val expected = stripWhitespacePreservers(expectedOutput).trim()
30-
val actual = ask { () => generate(original, sourceFile = Some(sourceFile)).asText.trim() }
31+
val actual = generate(original, sourceFile = Some(sourceFile)).asText.trim()
3132
if (actual != expected)
3233
throw new ComparisonFailure("", expected, actual)
3334
}
@@ -36,15 +37,15 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
3637
def becomes(expectedOutput: String) = input -> expectedOutput
3738
}
3839
final implicit class OrToDieAfter(input: (String, String)) {
39-
def after(trans: Transformation[Tree, Tree]): Unit = {
40-
val t = ask { () => trans(treeFrom(input._1)) }
40+
def after(trans: Transformation[Tree, Tree]): Unit = ask { () =>
41+
val t = trans(treeFrom(input._1))
4142
require(t.isDefined, "transformation was not successful")
4243
t foreach (_.printsTo(input._2))
4344
}
4445
}
4546

4647
@Test
47-
def add_return_type_to_def() = global.ask { () => """
48+
def add_return_type_to_def() = """
4849
package add_return_type_to_def
4950
object X {
5051
def value = new java.io.File("")
@@ -57,10 +58,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
5758
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
5859
d.copy(tpt = newTpt) replaces d
5960
}}}
60-
}
6161

6262
@Test
63-
def add_return_type_to_val() = global.ask { () => """
63+
def add_return_type_to_val() = """
6464
package add_return_type_to_val
6565
object X {
6666
val value = new java.io.File("")
@@ -74,10 +74,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
7474
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
7575
d.copy(tpt = newTpt) replaces d
7676
}}}
77-
}
7877

7978
@Test
80-
def add_return_type_to_var() = global.ask { () => """
79+
def add_return_type_to_var() = """
8180
package add_return_type_to_var
8281
object X {
8382
var variable = new java.io.File("")
@@ -91,10 +90,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
9190
val newTpt = tpt setOriginal mkReturn(List(tpt.tpe.typeSymbol))
9291
d.copy(tpt = newTpt) replaces d
9392
}}}
94-
}
9593

9694
@Test
97-
def add_override_flag() = global.ask { () => """
95+
def add_override_flag() = """
9896
package add_override_flag
9997
trait T {
10098
def meth: Int
@@ -120,10 +118,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
120118
d.copy(mods = d.mods.withFlag(Flag.OVERRIDE)) replaces d
121119
}
122120
}}
123-
}
124121

125122
@Test
126-
def add_override_final_flags_to_lazy_val() = global.ask { () => """
123+
def add_override_final_flags_to_lazy_val() = """
127124
package add_override_final_flags_to_lazy_val
128125
trait T {
129126
def meth: Int
@@ -149,10 +146,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
149146
d.copy(mods = d.mods.withFlag(Flag.OVERRIDE).withFlag(Flag.FINAL).withFlag(Flag.LAZY).withFlag(Tokens.VAL)) replaces d
150147
}
151148
}}
152-
}
153149

154150
@Test
155-
def add_override_protected_abstract_flag() = global.ask { () => """
151+
def add_override_protected_abstract_flag() = """
156152
package add_override_protected_abstract_flag
157153
trait T {
158154
protected def meth: Int = 0
@@ -178,10 +174,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
178174
d.copy(mods = d.mods.withFlag(Flag.ABSTRACT).withFlag(Flag.OVERRIDE).withFlag(Flag.PROTECTED)) replaces d
179175
}
180176
}}
181-
}
182177

183178
@Test
184-
def add_final_case_flag() = global.ask { () => """
179+
def add_final_case_flag() = """
185180
package add_final_case_flag
186181
class C(i: Int)
187182
""" becomes """
@@ -193,10 +188,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
193188
d.copy(mods = d.mods.withFlag(Flag.FINAL).withFlag(Flag.CASE).withFlag(Flag.PRIVATE)) replaces d
194189
}
195190
}}
196-
}
197191

198192
@Test
199-
def add_modifier_to_def_without_return_type() = global.ask { () => """
193+
def add_modifier_to_def_without_return_type() = """
200194
package add_modifier_to_def_without_return_type
201195
trait T {
202196
def meth: Int
@@ -222,10 +216,9 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
222216
d.copy(mods = d.mods.withFlag(Flag.OVERRIDE)) replaces d
223217
}
224218
}}
225-
}
226219

227220
@Test
228-
def add_modifier_to_val_without_return_type() = global.ask { () => """
221+
def add_modifier_to_val_without_return_type() = """
229222
package add_modifier_to_val_without_return_type
230223
trait T {
231224
def meth: Int
@@ -252,5 +245,4 @@ class ReusingPrinterTest extends TestHelper with SilentTracing {
252245
d.copy(mods = d.mods.withFlag(Flag.OVERRIDE)) replaces d
253246
}
254247
}}
255-
}
256248
}

org.scala-refactoring.library/src/test/scala/scala/tools/refactoring/tests/util/TestHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import scala.tools.refactoring.common.Selections
2020

2121
import language.{ postfixOps, implicitConversions }
2222

23-
trait TestHelper extends ScalaVersionTestRule with Refactoring with CompilerProvider with common.InteractiveScalaCompiler {
23+
trait TestHelper extends TestRules with Refactoring with CompilerProvider with common.InteractiveScalaCompiler {
2424

2525
@Before
2626
def cleanup() = resetPresentationCompiler()

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>junit</groupId>
5353
<artifactId>junit</artifactId>
54-
<version>4.7</version>
54+
<version>4.11</version>
5555
<scope>test</scope>
5656
</dependency>
5757
<dependency>
@@ -219,7 +219,7 @@
219219
<repository>
220220
<id>helios</id>
221221
<layout>p2</layout>
222-
<url>http://download.eclipse.org/releases/indigo</url>
222+
<url>http://download.eclipse.org/releases/kepler</url>
223223
</repository>
224224
<repository>
225225
<id>codehaus-snapshots</id>

0 commit comments

Comments
 (0)