|
4 | 4 |
|
5 | 5 | package net.sourceforge.pmd.cpd; |
6 | 6 |
|
7 | | -import java.io.File; |
8 | | -import java.io.IOException; |
9 | | -import java.nio.charset.Charset; |
10 | | -import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.Properties; |
11 | 8 |
|
12 | | -import org.apache.commons.io.FileUtils; |
13 | | -import org.apache.commons.io.IOUtils; |
14 | | -import org.junit.After; |
15 | | -import org.junit.Assert; |
16 | | -import org.junit.Before; |
17 | 9 | import org.junit.Test; |
| 10 | +import org.junit.rules.ExpectedException; |
18 | 11 |
|
19 | | -import net.sourceforge.pmd.testframework.AbstractTokenizerTest; |
| 12 | +import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest; |
| 13 | +import net.sourceforge.pmd.lang.ast.TokenMgrError; |
20 | 14 |
|
21 | | -public class ScalaTokenizerTest extends AbstractTokenizerTest { |
| 15 | +public class ScalaTokenizerTest extends CpdTextComparisonTest { |
22 | 16 |
|
23 | | - private static final Charset ENCODING = StandardCharsets.UTF_8; |
| 17 | + @org.junit.Rule |
| 18 | + public ExpectedException ex = ExpectedException.none(); |
24 | 19 |
|
25 | | - private static final String FILENAME = "/tokenizerFiles/sample-LiftActor.scala"; |
26 | | - |
27 | | - private File tempFile; |
28 | | - |
29 | | - @Before |
30 | | - @Override |
31 | | - public void buildTokenizer() throws IOException { |
32 | | - createTempFileOnDisk(); |
33 | | - this.tokenizer = new ScalaTokenizer(); |
| 20 | + public ScalaTokenizerTest() { |
| 21 | + super(".scala"); |
34 | 22 | } |
35 | 23 |
|
36 | | - private void createTempFileOnDisk() throws IOException { |
37 | | - this.tempFile = File.createTempFile("scala-tokenizer-test-", ".scala"); |
38 | | - FileUtils.writeStringToFile(tempFile, getSampleCode(), ENCODING); |
| 24 | + @Override |
| 25 | + protected String getResourcePrefix() { |
| 26 | + return "../lang/scala/cpd/testdata"; |
39 | 27 | } |
40 | 28 |
|
41 | 29 | @Override |
42 | | - public String getSampleCode() throws IOException { |
43 | | - return IOUtils.toString(getClass().getResourceAsStream(FILENAME), ENCODING); |
| 30 | + public Tokenizer newTokenizer(Properties properties) { |
| 31 | + return new ScalaTokenizer(); |
44 | 32 | } |
45 | 33 |
|
46 | 34 | @Test |
47 | | - public void tokenizeTest() throws IOException { |
48 | | - this.sourceCode = new SourceCode(new SourceCode.FileCodeLoader(tempFile, "UTF-8")); |
49 | | - this.expectedTokenCount = 2472; |
50 | | - super.tokenizeTest(); |
| 35 | + public void testSample() { |
| 36 | + doTest("sample-LiftActor"); |
51 | 37 | } |
52 | 38 |
|
53 | 39 | @Test |
54 | | - public void tokenizeFailTest() throws IOException { |
55 | | - this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader( |
56 | | - " object Main { " |
57 | | - + " def main(args: Array[String]): Unit = { " |
58 | | - + " println(\"Hello, World!) " //unclosed string literal |
59 | | - + " }" |
60 | | - + "}")); |
61 | | - try { |
62 | | - super.tokenizeTest(); |
63 | | - Assert.fail(); |
64 | | - } catch (Exception e) { |
65 | | - // intentional |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - @After |
70 | | - public void cleanUp() { |
71 | | - FileUtils.deleteQuietly(this.tempFile); |
72 | | - this.tempFile = null; |
| 40 | + public void tokenizeFailTest() { |
| 41 | + ex.expect(TokenMgrError.class); |
| 42 | + doTest("unlexable_sample"); |
73 | 43 | } |
74 | 44 | } |
0 commit comments