org.junit.rules.ExpectedException#expect()源码实例Demo

下面列出了org.junit.rules.ExpectedException#expect() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: hbase   文件: TestThriftHttpServer.java
@Test
public void testExceptionThrownWhenMisConfigured() throws IOException {
  Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
  conf.set("hbase.thrift.security.qop", "privacy");
  conf.setBoolean("hbase.thrift.ssl.enabled", false);
  ExpectedException thrown = ExpectedException.none();
  ThriftServerRunner tsr = null;
  try {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Thrift HTTP Server's QoP is privacy, " +
        "but hbase.thrift.ssl.enabled is false");
    tsr = TestThriftServerCmdLine.createBoundServer(() -> new ThriftServer(conf));
    fail("Thrift HTTP Server starts up even with wrong security configurations.");
  } catch (Exception e) {
    LOG.info("Expected!", e);
  } finally {
    if (tsr != null) {
      tsr.close();
    }
  }
}
 
源代码2 项目: beam   文件: SorterTestUtils.java
/** Tests trying to call add after calling sort. Should throw an exception. */
public static void testAddAfterSort(Sorter sorter, ExpectedException thrown) throws Exception {
  thrown.expect(IllegalStateException.class);
  thrown.expectMessage(is("Records can only be added before sort()"));
  KV<byte[], byte[]> kv = KV.of(new byte[] {4, 7}, new byte[] {1, 2});
  sorter.add(kv);
  sorter.sort();
  sorter.add(kv);
}
 
源代码3 项目: beam   文件: SorterTestUtils.java
/** Tests trying to calling sort twice. Should throw an exception. */
public static void testSortTwice(Sorter sorter, ExpectedException thrown) throws Exception {
  thrown.expect(IllegalStateException.class);
  thrown.expectMessage(is("sort() can only be called once."));
  KV<byte[], byte[]> kv = KV.of(new byte[] {4, 7}, new byte[] {1, 2});
  sorter.add(kv);
  sorter.sort();
  sorter.sort();
}
 
源代码4 项目: dhis2-core   文件: DhisConvenienceTest.java
/**
 * Asserts that a {@link IllegalQueryException} is thrown with the given {@link ErrorCode}.
 *
 * @param exception the {@link ExpectedException}.
 * @param errorCode the {@link ErrorCode}.
 */
public static void assertIllegalQueryEx( ExpectedException exception, ErrorCode errorCode )
{
    exception.expect( IllegalQueryException.class );
    exception.expect( Matchers.hasProperty( "errorCode", CoreMatchers.is( errorCode ) ) );
    exception.reportMissingExceptionWithMessage( String.format(
        "Test does not throw an IllegalQueryException with error code: '%s'", errorCode ) );
}
 
源代码5 项目: candybean   文件: WSSystemTest.java
@Test
public void testHTTPError() {
	ExpectedException exception = ExpectedException.none();
	try {
		exception.expect(CandybeanException.class);
		response = WS.request(WS.OP.POST, uri + "/get", headers, "", ContentType.DEFAULT_TEXT);
		Assert.fail();
	} catch (CandybeanException e) {
		Assert.assertEquals("HTTP request received HTTP code: 405",
				e.getMessage().split("\n")[0]);
	}
}
 
源代码6 项目: candybean   文件: WSSystemTest.java
@Test
@Ignore("This test should pass, but takes a full minute to do so because it" +
		"waits for the response to time out.")
public void testResponseError() {
	ExpectedException exception = ExpectedException.none();
	try {
		exception.expect(CandybeanException.class);
		// Send to an IP address that does not exist
		response = WS.request(WS.OP.POST, "http://240.0.0.0", headers, "", ContentType.DEFAULT_TEXT);
		Assert.fail();
	} catch (CandybeanException e) {
		Assert.assertEquals("Connect to 240.0.0.0:80 [/240.0.0.0] failed: Operation timed out", e.getMessage());
	}
}
 
源代码7 项目: Flink-CEPplus   文件: BlobServerCorruptionTest.java
/**
 * Checks the GET operation fails when the downloaded file (from HA store)
 * is corrupt, i.e. its content's hash does not match the {@link BlobKey}'s hash.
 *
 * @param config
 * 		blob server configuration (including HA settings like {@link HighAvailabilityOptions#HA_STORAGE_PATH}
 * 		and {@link HighAvailabilityOptions#HA_CLUSTER_ID}) used to set up <tt>blobStore</tt>
 * @param blobStore
 * 		shared HA blob store to use
 * @param expectedException
 * 		expected exception rule to use
 */
public static void testGetFailsFromCorruptFile(
		Configuration config, BlobStore blobStore, ExpectedException expectedException)
		throws IOException {

	Random rnd = new Random();
	JobID jobId = new JobID();

	try (BlobServer server = new BlobServer(config, blobStore)) {

		server.start();

		byte[] data = new byte[2000000];
		rnd.nextBytes(data);

		// put content addressable (like libraries)
		BlobKey key = put(server, jobId, data, PERMANENT_BLOB);
		assertNotNull(key);

		// delete local file to make sure that the GET requests downloads from HA
		File blobFile = server.getStorageLocation(jobId, key);
		assertTrue(blobFile.delete());

		// change HA store file contents to make sure that GET requests fail
		byte[] data2 = Arrays.copyOf(data, data.length);
		data2[0] ^= 1;
		File tmpFile = Files.createTempFile("blob", ".jar").toFile();
		try {
			FileUtils.writeByteArrayToFile(tmpFile, data2);
			blobStore.put(tmpFile, jobId, key);
		} finally {
			//noinspection ResultOfMethodCallIgnored
			tmpFile.delete();
		}

		// issue a GET request that fails
		expectedException.expect(IOException.class);
		expectedException.expectMessage("data corruption");

		get(server, jobId, key);
	}
}
 
源代码8 项目: flink   文件: BlobServerCorruptionTest.java
/**
 * Checks the GET operation fails when the downloaded file (from HA store)
 * is corrupt, i.e. its content's hash does not match the {@link BlobKey}'s hash.
 *
 * @param config
 * 		blob server configuration (including HA settings like {@link HighAvailabilityOptions#HA_STORAGE_PATH}
 * 		and {@link HighAvailabilityOptions#HA_CLUSTER_ID}) used to set up <tt>blobStore</tt>
 * @param blobStore
 * 		shared HA blob store to use
 * @param expectedException
 * 		expected exception rule to use
 */
public static void testGetFailsFromCorruptFile(
		Configuration config, BlobStore blobStore, ExpectedException expectedException)
		throws IOException {

	Random rnd = new Random();
	JobID jobId = new JobID();

	try (BlobServer server = new BlobServer(config, blobStore)) {

		server.start();

		byte[] data = new byte[2000000];
		rnd.nextBytes(data);

		// put content addressable (like libraries)
		BlobKey key = put(server, jobId, data, PERMANENT_BLOB);
		assertNotNull(key);

		// delete local file to make sure that the GET requests downloads from HA
		File blobFile = server.getStorageLocation(jobId, key);
		assertTrue(blobFile.delete());

		// change HA store file contents to make sure that GET requests fail
		byte[] data2 = Arrays.copyOf(data, data.length);
		data2[0] ^= 1;
		File tmpFile = Files.createTempFile("blob", ".jar").toFile();
		try {
			FileUtils.writeByteArrayToFile(tmpFile, data2);
			blobStore.put(tmpFile, jobId, key);
		} finally {
			//noinspection ResultOfMethodCallIgnored
			tmpFile.delete();
		}

		// issue a GET request that fails
		expectedException.expect(IOException.class);
		expectedException.expectMessage("data corruption");

		get(server, jobId, key);
	}
}
 
源代码9 项目: ArchUnit   文件: JavaClassTest.java
public static void expectInvalidSyntaxUsageForClassInsteadOfInterface(ExpectedException thrown, Class<?> nonInterface) {
    thrown.expect(InvalidSyntaxUsageException.class);
    thrown.expectMessage(nonInterface.getName());
    thrown.expectMessage("interface");
}
 
源代码10 项目: ArchUnit   文件: CanBeAnnotatedTest.java
public static void expectInvalidSyntaxUsageForRetentionSource(ExpectedException thrown) {
    thrown.expect(InvalidSyntaxUsageException.class);
    thrown.expectMessage(Retention.class.getSimpleName());
    thrown.expectMessage(RetentionPolicy.SOURCE.name());
    thrown.expectMessage("useless");
}
 
源代码11 项目: flink   文件: BlobServerCorruptionTest.java
/**
 * Checks the GET operation fails when the downloaded file (from HA store)
 * is corrupt, i.e. its content's hash does not match the {@link BlobKey}'s hash.
 *
 * @param config
 * 		blob server configuration (including HA settings like {@link HighAvailabilityOptions#HA_STORAGE_PATH}
 * 		and {@link HighAvailabilityOptions#HA_CLUSTER_ID}) used to set up <tt>blobStore</tt>
 * @param blobStore
 * 		shared HA blob store to use
 * @param expectedException
 * 		expected exception rule to use
 */
public static void testGetFailsFromCorruptFile(
		Configuration config, BlobStore blobStore, ExpectedException expectedException)
		throws IOException {

	Random rnd = new Random();
	JobID jobId = new JobID();

	try (BlobServer server = new BlobServer(config, blobStore)) {

		server.start();

		byte[] data = new byte[2000000];
		rnd.nextBytes(data);

		// put content addressable (like libraries)
		BlobKey key = put(server, jobId, data, PERMANENT_BLOB);
		assertNotNull(key);

		// delete local file to make sure that the GET requests downloads from HA
		File blobFile = server.getStorageLocation(jobId, key);
		assertTrue(blobFile.delete());

		// change HA store file contents to make sure that GET requests fail
		byte[] data2 = Arrays.copyOf(data, data.length);
		data2[0] ^= 1;
		File tmpFile = Files.createTempFile("blob", ".jar").toFile();
		try {
			FileUtils.writeByteArrayToFile(tmpFile, data2);
			blobStore.put(tmpFile, jobId, key);
		} finally {
			//noinspection ResultOfMethodCallIgnored
			tmpFile.delete();
		}

		// issue a GET request that fails
		expectedException.expect(IOException.class);
		expectedException.expectMessage("data corruption");

		get(server, jobId, key);
	}
}
 
源代码12 项目: cs-actions   文件: MockingHelper.java
@SuppressWarnings("unchecked")
public static void setExpectedExceptions(ExpectedException exception, Class<?> type, String message) {
    exception.expect((Class<? extends Throwable>) type);
    exception.expectMessage(message);
}
 
源代码13 项目: cs-actions   文件: TestUtils.java
@SuppressWarnings("unchecked")
public static void setExpectedExceptions(Class<?> type, ExpectedException exception, String message) {
    exception.expect((Class<? extends Throwable>) type);
    exception.expectMessage(message);
}