private static void failFormat(String template, Object... args) { assert_().fail(template, args); } }
/** * Returns a new {@code ThrowableSubject} that supports assertions on this throwable's direct * cause. This method can be invoked repeatedly (e.g. {@code * assertThat(e).hasCauseThat().hasCauseThat()....} to assert on a particular indirect cause. */ public final ThrowableSubject hasCauseThat() { // provides a more helpful error message if hasCauseThat() methods are chained too deep // e.g. assertThat(new Exception()).hCT().hCT().... // TODO(diamondm) in keeping with other subjects' behavior this should still NPE if the subject // *itself* is null, since there's no context to lose. See also b/37645583 if (actual() == null) { check("getCause()").fail("Causal chain is not deep enough - add a .isNotNull() check?"); return ignoreCheck() .that( new Throwable() { @Override public Throwable fillInStackTrace() { setStackTrace(new StackTraceElement[0]); // for old versions of Android return this; } }); } return check("getCause()").that(actual().getCause()); } }
@Test public void testUsingReleasedSimpleCacheThrowsException() throws Exception { SimpleCache simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); simpleCache.release(); try { simpleCache.startReadWriteNonBlocking(KEY_1, 0); assertWithMessage("Exception was expected").fail(); } catch (RuntimeException e) { // Expected. Do nothing. } }
@Test public void testExceptionDuringEvictionByLeastRecentlyUsedCacheEvictorNotHang() throws Exception { CachedContentIndex index = Mockito.spy(new CachedContentIndex(cacheDir)); SimpleCache simpleCache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(20), index); // Add some content. CacheSpan cacheSpan = simpleCache.startReadWrite(KEY_1, 0); addCache(simpleCache, KEY_1, 0, 15); // Make index.store() throw exception from now on. doAnswer( invocation -> { throw new CacheException("SimpleCacheTest"); }) .when(index) .store(); // Adding more content will make LeastRecentlyUsedCacheEvictor evict previous content. try { addCache(simpleCache, KEY_1, 15, 15); assertWithMessage("Exception was expected").fail(); } catch (CacheException e) { // do nothing. } simpleCache.releaseHoleSpan(cacheSpan); // Although store() has failed, it should remove the first span and add the new one. NavigableSet<CacheSpan> cachedSpans = simpleCache.getCachedSpans(KEY_1); assertThat(cachedSpans).isNotEmpty(); assertThat(cachedSpans).hasSize(1); assertThat(cachedSpans.pollFirst().position).isEqualTo(15); }
@Test public void testMultipleSimpleCacheWithSameCacheDirThrowsException() throws Exception { new SimpleCache(cacheDir, new NoOpCacheEvictor()); try { new SimpleCache(cacheDir, new NoOpCacheEvictor()); assertWithMessage("Exception was expected").fail(); } catch (IllegalStateException e) { // Expected. Do nothing. } }
@Test public void testGetSpanNameInvalid() { List<String> invalidNames = ImmutableList.of("BareMethod", "/MethodWithoutService"); for (String invalidName : invalidNames) { @SuppressWarnings("unchecked") MethodDescriptor descriptor = MethodDescriptor.newBuilder() .setType(MethodType.SERVER_STREAMING) .setFullMethodName(invalidName) .setRequestMarshaller(Mockito.mock(Marshaller.class)) .setResponseMarshaller(Mockito.mock(Marshaller.class)) .build(); IllegalArgumentException actualError = null; try { SpanName spanName = GrpcCallableFactory.getSpanName(descriptor); Truth.assertWithMessage("Invalid method descriptor should not have a valid span name") .fail("%s should not generate the spanName: %s", invalidName, spanName); } catch (IllegalArgumentException e) { actualError = e; } assertThat(actualError).isNotNull(); } } }
@Test public void testGetSpanNameInvalid() { List<String> invalidNames = ImmutableList.of("no_split", ".no_client"); for (String invalidName : invalidNames) { @SuppressWarnings("unchecked") ApiMethodDescriptor descriptor = ApiMethodDescriptor.newBuilder() .setFullMethodName(invalidName) .setHttpMethod(HttpMethods.POST) .setRequestFormatter(Mockito.mock(HttpRequestFormatter.class)) .setResponseParser(Mockito.mock(HttpResponseParser.class)) .build(); IllegalArgumentException actualError = null; try { SpanName spanName = HttpJsonCallableFactory.getSpanName(descriptor); assertWithMessage("Invalid method descriptor should not have a valid span name") .fail("%s should not generate the spanName: %s", invalidName, spanName); } catch (IllegalArgumentException e) { actualError = e; } assertThat(actualError).isNotNull(); } } }
private void searchSite(TorrentSite torrentSite) throws Exception { // Set test user and password if (torrentSite.getAdapter().getAuthType() != NONE) { String user = getResourceString(torrentSite.name() + "_user"); String pass = getResourceString(torrentSite.name() + "_pass"); String token = getResourceString(torrentSite.name() + "_token"); if (!(has(user, pass) || has(token))) assert_().fail(torrentSite.name() + " is private but no credentials found: untestable"); prefs.edit() .putString("pref_key_user_" + torrentSite.name(), user) .putString("pref_key_pass_" + torrentSite.name(), pass) .putString("pref_key_token_" + torrentSite.name(), token) .commit(); } List<SearchResult> results = torrentSite.search(prefs, QUERY, ORDER, RESULTS); assertThat(results).isNotEmpty(); for (SearchResult result : results) { assertThat(result.getTitle()).isNotEmpty(); assertThat(result.getTorrentUri()).isNotNull(); assertThat(result.getTorrentUri().toString()).isNotEmpty(); assertThat(result.getDetailsUrl()).isNotEmpty(); assertThat(result.getSize()).isNotEmpty(); assertThat(result.getAddedDate()).isNotNull(); assertThat(result.getAddedDate().getTime()).isGreaterThan(THE_YEAR_2000); assertThat(result.getSeeds()).isAtLeast(0); assertThat(result.getLeechers()).isAtLeast(0); } }