private static void containsExactlySanityCheck(Collection<?> collection, Object... varargs) { assertThat(collection).hasSize(varargs.length); for (Object obj : varargs) { assertThat(collection).contains(obj); } assertThat(collection).containsExactly(varargs); } }
public void testScan_classPathCycle() throws IOException { File jarFile = File.createTempFile("with_circular_class_path", ".jar"); try { writeSelfReferencingJarFile(jarFile, "test.txt"); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(jarFile, ClassPathTest.class.getClassLoader()); assertThat(scanner.getResources()).hasSize(1); } finally { jarFile.delete(); } }
public void testWildcardCaptured_wildcardWithImplicitBound() throws Exception { TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {}; ImmutableList<Parameter> parameters = type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters(); assertThat(parameters).hasSize(1); TypeToken<?> parameterType = parameters.get(0).getType(); Type[] typeArgs = ((ParameterizedType) parameterType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Object.class); assertThat(new TypeToken<List<?>>() {}.isSupertypeOf(parameterType)).isTrue(); }
public void testWildcardCaptured_wildcardWithExplicitBound() throws Exception { TypeToken<Holder<? extends Number>> type = new TypeToken<Holder<? extends Number>>() {}; ImmutableList<Parameter> parameters = type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters(); assertThat(parameters).hasSize(1); TypeToken<?> parameterType = parameters.get(0).getType(); Type[] typeArgs = ((ParameterizedType) parameterType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Number.class); assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(parameterType)).isTrue(); }
/** * In some cases our graph implementations return custom sets that define their own size() and * contains(). Verify that these sets are consistent with the elements of their iterator. */ @CanIgnoreReturnValue static <T> Set<T> sanityCheckSet(Set<T> set) { assertThat(set).hasSize(Iterators.size(set.iterator())); for (Object element : set) { assertThat(set).contains(element); } assertThat(set).doesNotContain(new Object()); assertThat(set).isEqualTo(ImmutableSet.copyOf(set)); return set; } }
/** Ensure that errors are always logged. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_error() throws Exception { try { getDone(allAsList(immediateFailedFuture(new MyError()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyError.class); } }
@Test public void testStopsAllocatingBitmapsUntilNextIdleCallIfAllocationsTakeLongerThanLimit() { PreFillType size = new PreFillType.Builder(1).setConfig(Bitmap.Config.ARGB_8888).build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 3); when(clock.now()).thenReturn(0L).thenReturn(0L).thenReturn(BitmapPreFillRunner.MAX_DURATION_MS); BitmapPreFillRunner handler = getHandler(allocationOrder); handler.run(); assertThat(addedBitmaps).hasSize(1); handler.run(); assertThat(addedBitmaps).hasSize(3); }
public void testWildcardCaptured_typeVariableDeclaresTypeBound_wildcardHasExplicitUpperBound() throws Exception { TypeToken<Counter<? extends Number>> type = new TypeToken<Counter<? extends Number>>() {}; TypeToken<?> fieldType = type.resolveType(Counter.class.getDeclaredField("counts").getGenericType()); Type[] typeArgs = ((ParameterizedType) fieldType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Number.class); assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(fieldType)).isTrue(); assertThat(new TypeToken<List<? extends Iterable<?>>>() {}.isSupertypeOf(fieldType)).isFalse(); }
public void testWildcardCaptured_typeVariableDeclaresTypeBound_wildcardHasNoExplicitUpperBound() throws Exception { TypeToken<Counter<?>> type = new TypeToken<Counter<?>>() {}; TypeToken<?> fieldType = type.resolveType(Counter.class.getDeclaredField("counts").getGenericType()); Type[] typeArgs = ((ParameterizedType) fieldType.getType()).getActualTypeArguments(); assertThat(typeArgs).asList().hasSize(1); TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0]; assertThat(captured.getBounds()).asList().containsExactly(Number.class); assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(fieldType)).isTrue(); assertThat(new TypeToken<List<? extends Iterable<?>>>() {}.isSupertypeOf(fieldType)).isFalse(); }
/** Ensure that errors are always logged. */ @SuppressWarnings("unchecked") public void testSuccessfulAsList_logging_error() throws Exception { assertEquals( newArrayList((Object) null), getDone(successfulAsList(immediateFailedFuture(new MyError())))); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyError.class); }
public void testWildcardCaptured_methodParameter_upperBound() throws Exception { TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {}; ImmutableList<Parameter> parameters = type.method(Holder.class.getDeclaredMethod("setList", List.class)).getParameters(); assertThat(parameters).hasSize(1); TypeToken<?> parameterType = parameters.get(0).getType(); assertEquals(List.class, parameterType.getRawType()); assertFalse( parameterType.getType().toString(), parameterType.isSupertypeOf(new TypeToken<List<Integer>>() {})); }
@Test public void testSizeCallbackIsNotCalledPreDrawIfNoDimensSetOnPreDraw() { target.getSize(cb); shadowObserver.fireOnPreDrawListeners(); verify(cb, never()).onSizeReady(anyInt(), anyInt()); assertThat(shadowObserver.getPreDrawListeners()).hasSize(1); }
@Test public void testParse_withSingleValidModuleName_returnsListContainingModule() { addModuleToManifest(TestModule1.class); List<GlideModule> modules = parser.parse(); assertThat(modules).hasSize(1); assertThat(modules.get(0)).isInstanceOf(TestModule1.class); }
@Test public void testRegistersReceiverOnStart() { monitor.onStart(); assertThat(getConnectivityReceivers()).hasSize(1); }
/** All as list will log extra exceptions that have already occurred. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_multipleExceptions_alreadyDone() throws Exception { try { getDone( allAsList( immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // the second failure is logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyException.class); } }
public void removeEdge_antiparallelEdges() { putEdge(N1, N2); putEdge(N2, N1); assertThat(graph.removeEdge(N1, N2)).isTrue(); assertThat(graph.successors(N1)).isEmpty(); assertThat(graph.predecessors(N1)).containsExactly(N2); assertThat(graph.edges()).hasSize(1); assertThat(graph.removeEdge(N2, N1)).isTrue(); assertThat(graph.successors(N1)).isEmpty(); assertThat(graph.predecessors(N1)).isEmpty(); assertThat(graph.edges()).isEmpty(); }
@Test public void testKeySetIterator() { mapCache.put("A", "A_value"); mapCache.put("B", "B_value"); mapCache.put("C", "C_value"); assertThat(mapCache.unmodifiableKeySet()).hasSize(3); for (String key : mapCache.unmodifiableKeySet()) { assertThat(mapCache.get(key)).isEqualTo(key + "_value"); } }
@Test public void testParse_withMultipleValidModuleNames_returnsListContainingModules() { addModuleToManifest(TestModule1.class); addModuleToManifest(TestModule2.class); List<GlideModule> modules = parser.parse(); assertThat(modules).hasSize(2); assertThat(modules).contains(new TestModule1()); assertThat(modules).contains(new TestModule2()); }
@Test public void endpointPair_directed_contains() { MutableGraph<Integer> directedGraph = GraphBuilder.directed().allowsSelfLoops(true).build(); directedGraph.putEdge(N1, N1); directedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = directedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.ordered(N1, N1)); assertThat(edges).contains(EndpointPair.ordered(N1, N2)); // unordered endpoints not OK for directed graph (undefined behavior) assertThat(edges).doesNotContain(EndpointPair.unordered(N1, N2)); assertThat(edges).doesNotContain(EndpointPair.ordered(N2, N1)); // wrong order assertThat(edges).doesNotContain(EndpointPair.ordered(N2, N2)); // edge not present assertThat(edges).doesNotContain(EndpointPair.ordered(N3, N4)); // nodes not in graph }
@Test public void endpointPair_undirected_contains() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(true).build(); undirectedGraph.putEdge(N1, N1); undirectedGraph.putEdge(N1, N2); Set<EndpointPair<Integer>> edges = undirectedGraph.edges(); assertThat(edges).hasSize(2); assertThat(edges).contains(EndpointPair.unordered(N1, N1)); assertThat(edges).contains(EndpointPair.unordered(N1, N2)); assertThat(edges).contains(EndpointPair.unordered(N2, N1)); // equal to unordered(N1, N2) // ordered endpoints OK for undirected graph (because ordering is irrelevant) assertThat(edges).contains(EndpointPair.ordered(N1, N2)); assertThat(edges).doesNotContain(EndpointPair.unordered(N2, N2)); // edge not present assertThat(edges).doesNotContain(EndpointPair.unordered(N3, N4)); // nodes not in graph }