|
18 | 18 | */ |
19 | 19 | package bugs |
20 | 20 |
|
| 21 | +import org.apache.groovy.util.JavaShell |
21 | 22 | import org.junit.jupiter.api.Test |
22 | 23 |
|
23 | 24 | import static groovy.test.GroovyAssert.assertScript |
@@ -114,4 +115,89 @@ final class Groovy4721 { |
114 | 115 | assert 'foo' == new MyClass().myMethod() |
115 | 116 | ''' |
116 | 117 | } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testAccessingVariableInFinallyBlock_6() { |
| 121 | + def declareClass = { String lang -> |
| 122 | + """ |
| 123 | + import java.util.List; |
| 124 | + import java.util.ArrayList; |
| 125 | + import java.util.function.Function; |
| 126 | + public class TryFinally${lang}Test { |
| 127 | + public String test() { |
| 128 | + String result = "result: "; |
| 129 | + try { |
| 130 | + String x = "foo"; |
| 131 | + result += x; |
| 132 | + return result; |
| 133 | + } finally { |
| 134 | + String y = "bar"; |
| 135 | + result += y; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + """ |
| 140 | + } |
| 141 | + |
| 142 | + JavaShell js = new JavaShell() |
| 143 | + final mcn = "tests.TryFinallyJavaTest" |
| 144 | + js.compile(mcn, "package tests;\n${declareClass('Java')}") |
| 145 | + |
| 146 | + new GroovyShell(js.getClassLoader()).evaluate """\ |
| 147 | + package tests; |
| 148 | + import tests.TryFinallyJavaTest |
| 149 | +
|
| 150 | + ${declareClass('Groovy')} |
| 151 | +
|
| 152 | + final groovyResult = new TryFinallyGroovyTest().test() |
| 153 | + assert 'result: foo' == groovyResult |
| 154 | + assert new TryFinallyJavaTest().test() == groovyResult |
| 155 | + """ |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testAccessingVariableInFinallyBlock_7() { |
| 160 | + def declareClass = { String lang -> |
| 161 | + """ |
| 162 | + import java.util.List; |
| 163 | + import java.util.ArrayList; |
| 164 | + import java.util.function.Function; |
| 165 | + public class TryFinally${lang}Test { |
| 166 | + public List<String> test() { |
| 167 | + List<String> resultList = new ArrayList<>(); |
| 168 | + String result = "result: "; |
| 169 | + try { |
| 170 | + String x = "foo"; |
| 171 | + result += x; |
| 172 | + resultList.add(result); |
| 173 | + Function<String, List<String>> f = (String r) -> { |
| 174 | + resultList.add(r); |
| 175 | + return resultList; |
| 176 | + }; |
| 177 | + return f.apply(result); |
| 178 | + } finally { |
| 179 | + String y = "bar"; |
| 180 | + result += y; |
| 181 | + resultList.add(result); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + """ |
| 186 | + } |
| 187 | + |
| 188 | + JavaShell js = new JavaShell() |
| 189 | + final mcn = "tests.TryFinallyJavaTest" |
| 190 | + js.compile(mcn, "package tests;\n${declareClass('Java')}") |
| 191 | + |
| 192 | + new GroovyShell(js.getClassLoader()).evaluate """\ |
| 193 | + package tests; |
| 194 | + import tests.TryFinallyJavaTest |
| 195 | +
|
| 196 | + ${declareClass('Groovy')} |
| 197 | +
|
| 198 | + final groovyResult = new TryFinallyGroovyTest().test() |
| 199 | + assert ['result: foo', 'result: foo', 'result: foobar'] == groovyResult |
| 200 | + assert new TryFinallyJavaTest().test() == groovyResult |
| 201 | + """ |
| 202 | + } |
117 | 203 | } |
0 commit comments