java.lang.IllegalStateException.getCause()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中java.lang.IllegalStateException.getCause()方法的一些代码示例,展示了IllegalStateException.getCause()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IllegalStateException.getCause()方法的具体详情如下:
包路径:java.lang.IllegalStateException
类名称:IllegalStateException
方法名:getCause

IllegalStateException.getCause介绍

暂无

代码示例

代码示例来源:origin: org.apache.hadoop/hadoop-common

@Override
 public IOException getCause() {
  return (IOException)super.getCause();
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void canonicalPath_fails_to_get_canonical_path() throws Exception {
 File file = mock(File.class);
 when(file.getCanonicalPath()).thenThrow(new IOException());
 try {
  PathUtils.canonicalPath(file);
  fail();
 } catch (IllegalStateException e) {
  assertThat(e.getCause()).isInstanceOf(IOException.class);
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void non_existing_file_should_throw_exception() {
 try {
  createInputFileWithMetadata(Paths.get(""), "non_existing");
  Assert.fail();
 } catch (IllegalStateException e) {
  assertThat(e.getMessage()).endsWith("Unable to read file " + Paths.get("").resolve("non_existing").toAbsolutePath());
  assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
 }
}

代码示例来源:origin: spring-projects/spring-framework

private void assertBogusActiveTestGroupBehavior(String testGroups) {
  // Should result in something similar to the following:
  //
  // java.lang.IllegalStateException: Failed to parse 'testGroups' system property:
  // Unable to find test group 'bogus' when parsing testGroups value: 'all-bogus'.
  // Available groups include: [LONG_RUNNING,PERFORMANCE,JMXMP,CI]
  setTestGroups(testGroups);
  try {
    Assume.group(JMXMP);
    fail("assumption should have failed");
  }
  catch (IllegalStateException ex) {
    assertThat(ex.getMessage(),
      startsWith("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY + "' system property: "));
    assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
    assertThat(ex.getCause().getMessage(),
      equalTo("Unable to find test group 'bogus' when parsing testGroups value: '" + testGroups
          + "'. Available groups include: [LONG_RUNNING,PERFORMANCE,JMXMP,CI]"));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void illegalArgumentException() {
  this.resolvers.add(stubResolver(1));
  Method method = ResolvableMethod.on(TestController.class).mockCall(o -> o.singleArg(null)).method();
  Mono<HandlerResult> mono = invoke(new TestController(), method);
  try {
    mono.block();
    fail("Expected IllegalStateException");
  }
  catch (IllegalStateException ex) {
    assertNotNull("Exception not wrapped", ex.getCause());
    assertTrue(ex.getCause() instanceof IllegalArgumentException);
    assertTrue(ex.getMessage().contains("Controller ["));
    assertTrue(ex.getMessage().contains("Method ["));
    assertTrue(ex.getMessage().contains("with argument values:"));
    assertTrue(ex.getMessage().contains("[0] [type=java.lang.Integer] [value=1]"));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void illegalArgumentException() throws Exception {
  this.composite.addResolver(new StubArgumentResolver(Integer.class, "__not_an_int__"));
  this.composite.addResolver(new StubArgumentResolver("value"));
  try {
    getInvocable(Integer.class, String.class).invokeForRequest(request, null);
    fail("Expected exception");
  }
  catch (IllegalStateException ex) {
    assertNotNull("Exception not wrapped", ex.getCause());
    assertTrue(ex.getCause() instanceof IllegalArgumentException);
    assertTrue(ex.getMessage().contains("Controller ["));
    assertTrue(ex.getMessage().contains("Method ["));
    assertTrue(ex.getMessage().contains("with argument values:"));
    assertTrue(ex.getMessage().contains("[0] [type=java.lang.String] [value=__not_an_int__]"));
    assertTrue(ex.getMessage().contains("[1] [type=java.lang.String] [value=value"));
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void close_throws_exception_on_error() {
 Closeable closeable = new Closeable() {
  @Override
  public void close() throws IOException {
   throw new IOException("expected");
  }
 };
 try {
  System2.INSTANCE.close(closeable);
  fail();
 } catch (IllegalStateException e) {
  assertThat(e.getCause().getMessage()).isEqualTo("expected");
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void failOnError() throws Exception {
 HttpsTrust.Ssl context = mock(HttpsTrust.Ssl.class);
 KeyManagementException cause = new KeyManagementException("foo");
 when(context.newFactory(any(TrustManager.class))).thenThrow(cause);
 try {
  new HttpsTrust(context);
  fail();
 } catch (IllegalStateException e) {
  assertThat(e.getMessage()).isEqualTo("Fail to build SSL factory");
  assertThat(e.getCause()).isSameAs(cause);
 }
}

代码示例来源:origin: checkstyle/checkstyle

@SuppressWarnings("rawtypes")
@Test
public void testInvokeConstructorThatFails() throws NoSuchMethodException {
  final Constructor<Dictionary> constructor = Dictionary.class.getConstructor();
  try {
    CommonUtil.invokeConstructor(constructor);
    fail("IllegalStateException is expected");
  }
  catch (IllegalStateException expected) {
    assertSame("Invalid exception cause", InstantiationException.class,
      expected.getCause().getClass());
  }
}

代码示例来源:origin: google/guava

public void testFailOnExceptionFromStartUp() {
 TestService service = new TestService();
 service.startUpException = new Exception();
 try {
  service.startAsync().awaitRunning();
  fail();
 } catch (IllegalStateException e) {
  assertEquals(service.startUpException, e.getCause());
 }
 assertEquals(0, service.numberOfTimesRunCalled.get());
 assertEquals(Service.State.FAILED, service.state());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void illegalArgumentException() throws Exception {
  this.resolvers.addResolver(new StubArgumentResolver(Integer.class, "__not_an_int__"));
  this.resolvers.addResolver(new StubArgumentResolver("value"));
  try {
    Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
    invoke(new Handler(), method);
    fail("Expected exception");
  }
  catch (IllegalStateException ex) {
    assertNotNull("Exception not wrapped", ex.getCause());
    assertTrue(ex.getCause() instanceof IllegalArgumentException);
    assertTrue(ex.getMessage().contains("Endpoint ["));
    assertTrue(ex.getMessage().contains("Method ["));
    assertTrue(ex.getMessage().contains("with argument values:"));
    assertTrue(ex.getMessage().contains("[0] [type=java.lang.String] [value=__not_an_int__]"));
    assertTrue(ex.getMessage().contains("[1] [type=java.lang.String] [value=value"));
  }
}

代码示例来源:origin: google/guava

public void testFailOnExceptionFromShutDown() throws Exception {
 TestService service = new TestService();
 service.shutDownException = new Exception();
 service.startAsync().awaitRunning();
 service.runFirstBarrier.await();
 service.stopAsync();
 service.runSecondBarrier.await();
 try {
  service.awaitTerminated();
  fail();
 } catch (IllegalStateException e) {
  assertEquals(service.shutDownException, e.getCause());
 }
 assertEquals(Service.State.FAILED, service.state());
}

代码示例来源:origin: checkstyle/checkstyle

@Test
public void testGetNonExistentConstructor() {
  try {
    CommonUtil.getConstructor(Math.class);
    fail("IllegalStateException is expected");
  }
  catch (IllegalStateException expected) {
    assertSame("Invalid exception cause",
      NoSuchMethodException.class, expected.getCause().getClass());
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testInvalidClassName() throws Exception {
  String bogusClassName = "foobar";
  DriverManagerDataSource ds = new DriverManagerDataSource();
  try {
    ds.setDriverClassName(bogusClassName);
    fail("Should have thrown IllegalStateException");
  }
  catch (IllegalStateException ex) {
    // OK
    assertTrue(ex.getCause() instanceof ClassNotFoundException);
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void fail_to_read_row() throws Exception {
 dbTester.prepareDbUnit(getClass(), "feed.xml");
 try (Connection connection = dbTester.openConnection()) {
  PreparedStatement stmt = connection.prepareStatement("select * from issues order by id");
  FailIterator iterator = new FailIterator(stmt);
  assertThat(iterator.hasNext()).isTrue();
  try {
   iterator.next();
   fail();
  } catch (IllegalStateException e) {
   assertThat(e.getCause()).isInstanceOf(SQLException.class);
  }
  iterator.close();
 }
}

代码示例来源:origin: apache/geode

@Test
@Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"})
public void runAndExpectExceptionAndCauseTypes_wrongExceptionTypeFails(Execution execution) {
 concurrencyRule.add(callWithExceptionAndCause)
   .expectExceptionType(NullPointerException.class)
   .expectExceptionCauseType(expectedExceptionWithCause.getCause().getClass());
 assertThatThrownBy(() -> execution.execute(concurrencyRule)).isInstanceOf(AssertionError.class);
}

代码示例来源:origin: apache/geode

@Test
@Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"})
public void runAndExpectExceptionAndCauseTypes(Execution execution) {
 concurrencyRule.add(callWithExceptionAndCause)
   .expectExceptionType(expectedExceptionWithCause.getClass())
   .expectExceptionCauseType(expectedExceptionWithCause.getCause().getClass());
 execution.execute(concurrencyRule);
}

代码示例来源:origin: apache/geode

@Test
@Parameters({"EXECUTE_IN_SERIES", "EXECUTE_IN_PARALLEL"})
public void runAndExpectExceptionCauseType(Execution execution) {
 concurrencyRule.add(callWithExceptionAndCause)
   .expectExceptionCauseType(expectedExceptionWithCause.getCause().getClass());
 execution.execute(concurrencyRule);
}

代码示例来源:origin: spring-projects/spring-framework

assertNotNull(actual.getCause());
assertSame(expected, actual.getCause());
assertTrue(actual.getMessage().contains("Invocation failure"));

代码示例来源:origin: spring-projects/spring-framework

assertNotNull(actual.getCause());
assertSame(expected, actual.getCause());
assertTrue(actual.getMessage().contains("Invocation failure"));

相关文章