...17import com.google.errorprone.BugCheckerRefactoringTestHelper;18import org.junit.Test;19import org.junit.runner.RunWith;20import org.junit.runners.JUnit4;21/** {@link ExpectedExceptionChecker}Test. */22@RunWith(JUnit4.class)23public class ExpectedExceptionCheckerTest {24 private final BugCheckerRefactoringTestHelper testHelper =25 BugCheckerRefactoringTestHelper.newInstance(ExpectedExceptionChecker.class, getClass());26 @Test27 public void expect() {28 testHelper29 .addInputLines(30 "in/ExceptionTest.java",31 "import static com.google.common.truth.Truth.assertThat;",32 "import java.io.IOException;",33 "import java.nio.file.*;",34 "import org.junit.Test;",35 "import org.junit.Rule;",36 "import org.hamcrest.CoreMatchers;",37 "import org.junit.rules.ExpectedException;",38 "class ExceptionTest {",39 " @Rule ExpectedException thrown = ExpectedException.none();",40 " @Test",41 " public void test() throws Exception {",42 " if (true) {",43 " Path p = Paths.get(\"NOSUCH\");",44 " thrown.expect(IOException.class);",45 " thrown.expect(CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",46 " thrown.expectCause(",47 " CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",48 " thrown.expectMessage(\"error\");",49 " thrown.expectMessage(CoreMatchers.containsString(\"error\"));",50 " Files.readAllBytes(p);",51 " assertThat(Files.exists(p)).isFalse();",52 " }",53 " }",54 "}")55 .addOutputLines(56 "out/ExceptionTest.java",57 "import static com.google.common.truth.Truth.assertThat;",58 "import static org.hamcrest.MatcherAssert.assertThat;",59 "import static org.junit.Assert.assertThrows;",60 "",61 "import java.io.IOException;",62 "import java.nio.file.*;",63 "import org.hamcrest.CoreMatchers;",64 "import org.junit.Rule;",65 "import org.junit.Test;",66 "import org.junit.rules.ExpectedException;",67 "class ExceptionTest {",68 " @Rule ExpectedException thrown = ExpectedException.none();",69 " @Test",70 " public void test() throws Exception {",71 " if (true) {",72 " Path p = Paths.get(\"NOSUCH\");",73 " IOException thrown =",74 " assertThrows(IOException.class, () -> Files.readAllBytes(p));",75 " assertThat(thrown,",76 " CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",77 " assertThat(thrown.getCause(),",78 " CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",79 " assertThat(thrown).hasMessageThat().contains(\"error\");",80 " assertThat(thrown.getMessage(), CoreMatchers.containsString(\"error\"));",81 " assertThat(Files.exists(p)).isFalse();",82 " }",83 " }",84 "}")85 .doTest();86 }87 @Test88 public void noExceptionType() {89 testHelper90 .addInputLines(91 "in/ExceptionTest.java",92 "import static com.google.common.truth.Truth.assertThat;",93 "import java.io.IOException;",94 "import java.nio.file.*;",95 "import org.hamcrest.CoreMatchers;",96 "import org.junit.Rule;",97 "import org.junit.Test;",98 "import org.junit.rules.ExpectedException;",99 "class ExceptionTest {",100 " @Rule ExpectedException thrown = ExpectedException.none();",101 " @Test",102 " public void test() throws Exception {",103 " Path p = Paths.get(\"NOSUCH\");",104 " thrown.expect(CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",105 " Files.readAllBytes(p);",106 " Files.readAllBytes(p);",107 " }",108 "}")109 .addOutputLines(110 "out/ExceptionTest.java",111 "import static com.google.common.truth.Truth.assertThat;",112 "import static org.hamcrest.MatcherAssert.assertThat;",113 "import static org.junit.Assert.assertThrows;",114 "",115 "import java.io.IOException;",116 "import java.nio.file.*;",117 "import org.hamcrest.CoreMatchers;",118 "import org.junit.Rule;",119 "import org.junit.Test;",120 "import org.junit.rules.ExpectedException;",121 "class ExceptionTest {",122 " @Rule ExpectedException thrown = ExpectedException.none();",123 " @Test",124 " public void test() throws Exception {",125 " Path p = Paths.get(\"NOSUCH\");",126 " Throwable thrown = assertThrows(Throwable.class, () -> {",127 " Files.readAllBytes(p);",128 " Files.readAllBytes(p);",129 " });",130 " assertThat(thrown, CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",131 " }",132 "}")133 .doTest();134 }135 @Test136 public void noExpectations() {137 testHelper138 .addInputLines(139 "in/ExceptionTest.java",140 "import static com.google.common.truth.Truth.assertThat;",141 "import java.io.IOException;",142 "import java.nio.file.*;",143 "import org.hamcrest.CoreMatchers;",144 "import org.junit.Rule;",145 "import org.junit.Test;",146 "import org.junit.rules.ExpectedException;",147 "class ExceptionTest {",148 " @Rule ExpectedException thrown = ExpectedException.none();",149 " @Test",150 " public void test() throws Exception {",151 " Path p = Paths.get(\"NOSUCH\");",152 " thrown.expect(IOException.class);",153 " Files.readAllBytes(p);",154 " assertThat(Files.exists(p)).isFalse();",155 " }",156 "}")157 .addOutputLines(158 "out/ExceptionTest.java",159 "import static com.google.common.truth.Truth.assertThat;",160 "import static org.junit.Assert.assertThrows;",161 "import java.io.IOException;",162 "import java.nio.file.*;",163 "import org.hamcrest.CoreMatchers;",164 "import org.junit.Rule;",165 "import org.junit.Test;",166 "import org.junit.rules.ExpectedException;",167 "class ExceptionTest {",168 " @Rule ExpectedException thrown = ExpectedException.none();",169 " @Test",170 " public void test() throws Exception {",171 " Path p = Paths.get(\"NOSUCH\");",172 " assertThrows(IOException.class, () -> Files.readAllBytes(p));",173 " assertThat(Files.exists(p)).isFalse();",174 " }",175 "}")176 .doTest();177 }178 @Test179 public void nonExpressionStatement() {180 testHelper181 .addInputLines(182 "in/ExceptionTest.java",183 "import static com.google.common.truth.Truth.assertThat;",184 "import java.io.IOException;",185 "import java.nio.file.*;",186 "import org.junit.Rule;",187 "import org.junit.Test;",188 "import org.junit.rules.ExpectedException;",189 "class ExceptionTest {",190 " @Rule ExpectedException thrown = ExpectedException.none();",191 " @Test",192 " public void test() throws Exception {",193 " Path p = Paths.get(\"NOSUCH\");",194 " thrown.expect(IOException.class);",195 " Files.readAllBytes(p);",196 " if (true) Files.readAllBytes(p);",197 " }",198 "}")199 .addOutputLines(200 "out/ExceptionTest.java",201 "import static com.google.common.truth.Truth.assertThat;",202 "import static org.junit.Assert.assertThrows;",203 "",204 "import java.io.IOException;",205 "import java.nio.file.*;",206 "import org.junit.Rule;",207 "import org.junit.Test;",208 "import org.junit.rules.ExpectedException;",209 "class ExceptionTest {",210 " @Rule ExpectedException thrown = ExpectedException.none();",211 " @Test",212 " public void test() throws Exception {",213 " Path p = Paths.get(\"NOSUCH\");",214 " assertThrows(IOException.class, () -> {",215 " Files.readAllBytes(p);",216 " if (true) Files.readAllBytes(p);",217 " });",218 " }",219 "}")220 .doTest();221 }222 // https://github.com/hamcrest/JavaHamcrest/issues/27223 @Test224 public void isA_hasCauseThat() {225 testHelper226 .addInputLines(227 "in/ExceptionTest.java",228 "import static com.google.common.truth.Truth.assertThat;",229 "import java.io.IOException;",230 "import java.nio.file.*;",231 "import org.junit.Test;",232 "import org.junit.Rule;",233 "import org.hamcrest.CoreMatchers;",234 "import org.junit.rules.ExpectedException;",235 "class ExceptionTest {",236 " @Rule ExpectedException thrown = ExpectedException.none();",237 " @Test",238 " public void test() throws Exception {",239 " Path p = Paths.get(\"NOSUCH\");",240 " thrown.expect(IOException.class);",241 " thrown.expectCause(CoreMatchers.isA(IOException.class));",242 " thrown.expectCause(org.hamcrest.core.Is.isA(IOException.class));",243 " Files.readAllBytes(p);",244 " assertThat(Files.exists(p)).isFalse();",245 " }",246 "}")247 .addOutputLines(248 "out/ExceptionTest.java",249 "import static com.google.common.truth.Truth.assertThat;",250 "import static org.junit.Assert.assertThrows;",251 "",252 "import java.io.IOException;",253 "import java.nio.file.*;",254 "import org.hamcrest.CoreMatchers;",255 "import org.junit.Rule;",256 "import org.junit.Test;",257 "import org.junit.rules.ExpectedException;",258 "class ExceptionTest {",259 " @Rule ExpectedException thrown = ExpectedException.none();",260 " @Test",261 " public void test() throws Exception {",262 " Path p = Paths.get(\"NOSUCH\");",263 " IOException thrown =",264 " assertThrows(IOException.class, () -> Files.readAllBytes(p));",265 " assertThat(thrown).hasCauseThat().isInstanceOf(IOException.class);",266 " assertThat(thrown).hasCauseThat().isInstanceOf(IOException.class);",267 " assertThat(Files.exists(p)).isFalse();",268 " }",269 "}")270 .doTest();271 }272 @Test273 public void typedMatcher() {274 testHelper275 .addInputLines(276 "in/ExceptionTest.java",277 "import static com.google.common.truth.Truth.assertThat;",278 "import java.io.IOException;",279 "import java.nio.file.*;",280 "import org.junit.Test;",281 "import org.junit.Rule;",282 "import org.hamcrest.Matcher;",283 "import org.junit.rules.ExpectedException;",284 "class ExceptionTest {",285 " @Rule ExpectedException thrown = ExpectedException.none();",286 " Matcher<IOException> matcher;",287 " @Test",288 " public void test() throws Exception {",289 " Path p = Paths.get(\"NOSUCH\");",290 " thrown.expect(matcher);",291 " Files.readAllBytes(p);",292 " assertThat(Files.exists(p)).isFalse();",293 " }",294 "}")295 .addOutputLines(296 "out/ExceptionTest.java",297 "import static com.google.common.truth.Truth.assertThat;",298 "import static org.hamcrest.MatcherAssert.assertThat;",299 "import static org.junit.Assert.assertThrows;",300 "",301 "import java.io.IOException;",302 "import java.nio.file.*;",303 "import org.hamcrest.Matcher;",304 "import org.junit.Rule;",305 "import org.junit.Test;",306 "import org.junit.rules.ExpectedException;",307 "class ExceptionTest {",308 " @Rule ExpectedException thrown = ExpectedException.none();",309 " Matcher<IOException> matcher;",310 " @Test",311 " public void test() throws Exception {",312 " Path p = Paths.get(\"NOSUCH\");",313 " IOException thrown =",314 " assertThrows(IOException.class, () -> Files.readAllBytes(p));",315 " assertThat(thrown, matcher);",316 " assertThat(Files.exists(p)).isFalse();",317 " }",318 "}")319 .doTest();320 }321 @Test322 public void nothingButAsserts() {323 testHelper324 .addInputLines(325 "in/ExceptionTest.java",326 "import static com.google.common.truth.Truth.assertThat;",327 "import org.junit.Rule;",328 "import org.junit.Test;",329 "import org.junit.rules.ExpectedException;",330 "class ExceptionTest {",331 " @Rule ExpectedException thrown = ExpectedException.none();",332 " @Test",333 " public void test() throws Exception {",334 " thrown.expect(RuntimeException.class);",335 " assertThat(false).isFalse();",336 " assertThat(true).isTrue();",337 " }",338 "}")339 .addOutputLines(340 "out/ExceptionTest.java",341 "import static com.google.common.truth.Truth.assertThat;",342 "import org.junit.Rule;",343 "import org.junit.Test;",344 "import org.junit.rules.ExpectedException;",345 "class ExceptionTest {",346 " @Rule ExpectedException thrown = ExpectedException.none();",347 " @Test",348 " public void test() throws Exception {",349 " assertThat(false).isFalse();",350 " assertThat(true).isTrue();",351 " }",352 "}")353 .doTest();354 }355 @Test356 public void removeExplicitFail() {357 testHelper358 .addInputLines(359 "in/ExceptionTest.java",360 "import static com.google.common.truth.Truth.assertThat;",361 "import static org.junit.Assert.fail;",362 "import java.io.IOException;",363 "import java.nio.file.*;",364 "import org.junit.Test;",365 "import org.junit.Rule;",366 "import org.junit.rules.ExpectedException;",367 "class ExceptionTest {",368 " @Rule ExpectedException thrown = ExpectedException.none();",369 " @Test",370 " public void testThrow() throws Exception {",371 " thrown.expect(IOException.class);",372 " throw new IOException();",373 " }",374 " @Test",375 " public void one() throws Exception {",376 " Path p = Paths.get(\"NOSUCH\");",377 " thrown.expect(IOException.class);",378 " Files.readAllBytes(p);",379 " assertThat(Files.exists(p)).isFalse();",380 " fail();",381 " }",382 " @Test",383 " public void two() throws Exception {",384 " Path p = Paths.get(\"NOSUCH\");",385 " thrown.expect(IOException.class);",386 " Files.readAllBytes(p);",387 " assertThat(Files.exists(p)).isFalse();",388 " throw new AssertionError();",389 " }",390 "}")391 .addOutputLines(392 "out/ExceptionTest.java",393 "import static com.google.common.truth.Truth.assertThat;",394 "import static org.junit.Assert.assertThrows;",395 "import static org.junit.Assert.fail;",396 "",397 "import java.io.IOException;",398 "import java.nio.file.*;",399 "import org.junit.Rule;",400 "import org.junit.Test;",401 "import org.junit.rules.ExpectedException;",402 "class ExceptionTest {",403 " @Rule ExpectedException thrown = ExpectedException.none();",404 " @Test",405 " public void testThrow() throws Exception {",406 " }",407 " @Test",408 " public void one() throws Exception {",409 " Path p = Paths.get(\"NOSUCH\");",410 " assertThrows(IOException.class, () -> Files.readAllBytes(p));",411 " assertThat(Files.exists(p)).isFalse();",412 " }",413 " @Test",414 " public void two() throws Exception {",415 " Path p = Paths.get(\"NOSUCH\");",416 " assertThrows(IOException.class, () -> Files.readAllBytes(p));",417 " assertThat(Files.exists(p)).isFalse();",418 " }",419 "}")420 .doTest();421 }422 // https://github.com/google/error-prone/issues/1072423 @Test424 public void i1072() {425 testHelper426 .addInputLines(427 "in/ExceptionTest.java",428 "import org.junit.Rule;",429 "import org.junit.Test;",430 "import org.junit.rules.ExpectedException;",431 "class ExceptionTest {",432 " @Rule ExpectedException thrown = ExpectedException.none();",433 " @Test",434 " public void testThrow(Class<? extends Throwable> clazz) throws Exception {",435 " thrown.expect(clazz);",436 " clazz.toString();",437 " }",438 "}")439 .addOutputLines(440 "in/ExceptionTest.java",441 "import static org.junit.Assert.assertThrows;",442 "import org.junit.Rule;",443 "import org.junit.Test;",444 "import org.junit.rules.ExpectedException;",445 "class ExceptionTest {",446 " @Rule ExpectedException thrown = ExpectedException.none();",447 " @Test",448 " public void testThrow(Class<? extends Throwable> clazz) throws Exception {",449 " assertThrows(Throwable.class, () -> clazz.toString());",450 " }",451 "}")452 .doTest();453 }454}...