|
1 | 1 | /*
|
2 |
| - * Copyright 2009-2020 the original author or authors. |
| 2 | + * Copyright 2009-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.codehaus.groovy.eclipse.junit.test
|
17 | 17 |
|
| 18 | +import org.codehaus.groovy.eclipse.test.GroovyEclipseTestSuite |
| 19 | +import org.codehaus.groovy.eclipse.test.SynchronizationUtils |
18 | 20 | import org.eclipse.jdt.core.ICompilationUnit
|
19 | 21 | import org.eclipse.jdt.core.IType
|
20 | 22 | import org.eclipse.jdt.internal.junit.launcher.JUnit3TestFinder
|
| 23 | +import org.junit.Before |
21 | 24 | import org.junit.Test
|
22 | 25 |
|
23 |
| -final class JUnit3TestFinderTests extends JUnitTestSuite { |
| 26 | +final class JUnit3TestFinderTests extends GroovyEclipseTestSuite { |
24 | 27 |
|
25 |
| - private void assertTypeIsTest(boolean expected, ICompilationUnit unit, String typeName, String reasonText = '') { |
| 28 | + @Before |
| 29 | + void setUp() { |
| 30 | + addJUnit(3) |
| 31 | + } |
| 32 | + |
| 33 | + private Set<IType> getAllTests() { |
| 34 | + Set<IType> testTypes = [] |
| 35 | + SynchronizationUtils.waitForIndexingToComplete() |
| 36 | + new JUnit3TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) |
| 37 | + return testTypes |
| 38 | + } |
| 39 | + |
| 40 | + private boolean isTest(ICompilationUnit unit, String typeName = unit.types[0].elementName) { |
26 | 41 | def type = unit.getType(typeName)
|
27 | 42 | assert type.exists() : "Groovy type $typeName should exist"
|
28 |
| - assert new JUnit3TestFinder().isTest(type) == expected : "Groovy type $typeName should${expected ? '' : 'n\'t'} be a JUnit 3 test $reasonText" |
| 43 | + return new JUnit3TestFinder().isTest(type) |
29 | 44 | }
|
30 | 45 |
|
| 46 | + //-------------------------------------------------------------------------- |
| 47 | + |
31 | 48 | @Test
|
32 |
| - void testFinderWithSuite() { |
33 |
| - def test = addGroovySource ''' |
34 |
| - class A { |
35 |
| - static junit.framework.Test suite() throws Exception {} |
| 49 | + void testIsTest0() { |
| 50 | + def unit = addGroovySource ''' |
| 51 | + class C { |
| 52 | + void test() { } |
36 | 53 | }
|
37 |
| - ''' |
| 54 | + ''', 'C', 'p' |
| 55 | + assert !isTest(unit) |
| 56 | + } |
38 | 57 |
|
39 |
| - assertTypeIsTest(true, test, 'A') |
| 58 | + @Test |
| 59 | + void testIsTest1() { |
| 60 | + def unit = addGroovySource ''' |
| 61 | + class C extends junit.framework.TestCase { |
| 62 | + void test() { } |
| 63 | + } |
| 64 | + ''', 'C', 'p' |
| 65 | + assert isTest(unit) |
40 | 66 | }
|
41 | 67 |
|
42 | 68 | @Test
|
43 |
| - void testFinderOfSubclass() { |
44 |
| - def base = addGroovySource ''' |
45 |
| - abstract class TestBase extends junit.framework.TestCase { |
| 69 | + void testIsTest2() { |
| 70 | + def unit = addGroovySource ''' |
| 71 | + class C { |
| 72 | + static junit.framework.Test suite() { } |
46 | 73 | }
|
47 |
| - ''' |
| 74 | + ''', 'C', 'p' |
| 75 | + assert isTest(unit) |
| 76 | + } |
48 | 77 |
|
49 |
| - def test = addGroovySource ''' |
50 |
| - class B extends TestBase { |
| 78 | + @Test |
| 79 | + void testIsTest3() { |
| 80 | + def unit = addGroovySource ''' |
| 81 | + abstract class TestBase extends junit.framework.TestCase { |
51 | 82 | }
|
52 |
| - ''' |
| 83 | + ''', 'TestBase', 'p' |
| 84 | + assert !isTest(unit) |
53 | 85 |
|
54 |
| - assertTypeIsTest(false, base, 'TestBase', '(it is abstract)') |
55 |
| - assertTypeIsTest(true, test, 'B') |
| 86 | + unit = addGroovySource ''' |
| 87 | + class C extends TestBase { |
| 88 | + } |
| 89 | + ''', 'C', 'p' |
| 90 | + assert isTest(unit) |
56 | 91 | }
|
57 | 92 |
|
58 | 93 | @Test
|
59 |
| - void testFinderOfNonPublicSubclass() { |
60 |
| - def base = addGroovySource ''' |
| 94 | + void testIsTest4() { |
| 95 | + def unit = addGroovySource ''' |
61 | 96 | abstract class TestBase extends junit.framework.TestCase {
|
62 | 97 | }
|
63 |
| - ''' |
| 98 | + ''', 'TestBase', 'p' |
| 99 | + assert !isTest(unit) |
64 | 100 |
|
65 |
| - def test = addGroovySource ''' |
| 101 | + unit = addGroovySource ''' |
66 | 102 | @groovy.transform.PackageScope class C extends TestBase {
|
67 | 103 | }
|
68 |
| - ''' |
69 |
| - |
70 |
| - assertTypeIsTest(false, base, 'TestBase', '(it is abstract)') |
71 |
| - assertTypeIsTest(true, test, 'C') |
| 104 | + ''', 'C', 'p' |
| 105 | + assert isTest(unit) |
72 | 106 | }
|
73 | 107 |
|
| 108 | + // |
| 109 | + |
74 | 110 | @Test
|
75 |
| - void testFindAllTestSuites() { |
| 111 | + void testFindTests() { |
76 | 112 | addGroovySource '''
|
77 | 113 | abstract class TestBase extends junit.framework.TestCase {
|
78 | 114 | }
|
79 |
| - ''' |
80 |
| - |
| 115 | + ''', 'TestBase', 'p' |
81 | 116 | addGroovySource '''
|
82 | 117 | class X extends TestBase {
|
83 | 118 | }
|
84 |
| - ''' |
85 |
| - |
| 119 | + ''', 'X', 'p' |
86 | 120 | addGroovySource '''
|
87 | 121 | class Y extends junit.framework.TestCase {
|
88 | 122 | }
|
89 |
| - ''' |
90 |
| - |
| 123 | + ''', 'Y', 'p' |
91 | 124 | addGroovySource '''
|
92 | 125 | class Z {
|
93 | 126 | static junit.framework.Test suite() throws Exception {}
|
94 | 127 | }
|
95 |
| - ''' |
| 128 | + ''', 'Z', 'p' |
96 | 129 |
|
97 |
| - Set<IType> testTypes = [] |
98 |
| - new JUnit3TestFinder().findTestsInContainer(packageFragmentRoot, testTypes, null) |
| 130 | + Set<IType> testTypes = allTests |
99 | 131 |
|
100 | 132 | assert testTypes.any { it.elementName == 'X' } : 'X should be a test type'
|
101 | 133 | assert testTypes.any { it.elementName == 'Y' } : 'Y should be a test type'
|
|
0 commit comments