if (string.contains("-")) { // Split it. } else { throw new IllegalArgumentException("String " + string + " does not contain -"); }
private void assertCastFails(long value) { try { Chars.checkedCast(value); fail("Cast to char should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue( value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } }
private static void assertCastFails(long value) { try { Shorts.checkedCast(value); fail("Cast to short should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue( value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } }
@Override public void onPreCall() { if (n < 0) { throw new IllegalArgumentException("n is less than 0"); } }
private static void assertCastFails(long value) { try { SignedBytes.checkedCast(value); fail("Cast to byte should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue( value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } }
private static void checkPercentageValidity(int percentage, String message) { if (percentage < 0 || percentage > 100) { throw new IllegalArgumentException(message); } } }
public void testToOptionalMultiple() { try { Stream.of(1, 2).collect(MoreCollectors.toOptional()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1, 2"); } }
public static int checkDuration(String name, long duration, TimeUnit unit) { if (duration < 0) throw new IllegalArgumentException(name + " < 0"); if (unit == null) throw new NullPointerException("unit == null"); long millis = unit.toMillis(duration); if (millis > Integer.MAX_VALUE) throw new IllegalArgumentException(name + " too large."); if (millis == 0 && duration > 0) throw new IllegalArgumentException(name + " too small."); return (int) millis; }
/** Can't create the map if more than one value maps to the same key. */ public void testUniqueIndexDuplicates() { try { Map<Integer, String> unused = Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1)); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("Multimaps.index"); } }
static void checkNotPrimitive(Type type) { if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) { throw new IllegalArgumentException(); } }
public void testOnlyElementMany() { try { Stream.of(1, 2, 3, 4, 5, 6).collect(MoreCollectors.onlyElement()); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("1, 2, 3, 4, 5, ..."); } } }
static Type getParameterUpperBound(int index, ParameterizedType type) { Type[] types = type.getActualTypeArguments(); if (index < 0 || index >= types.length) { throw new IllegalArgumentException( "Index " + index + " not in range [0," + types.length + ") for " + type); } Type paramType = types[index]; if (paramType instanceof WildcardType) { return ((WildcardType) paramType).getUpperBounds()[0]; } return paramType; }
@SuppressWarnings("unchecked") @Test public void badCapacityHint() throws Exception { Observable<Integer> source = Observable.just(1); try { Observable.concatEager(Arrays.asList(source, source, source), 1, -99); } catch (IllegalArgumentException ex) { assertEquals("prefetch > 0 required but it was -99", ex.getMessage()); } }
@Override public void setFixedLengthStreamingMode(long contentLength) { if (super.connected) throw new IllegalStateException("Already connected"); if (chunkLength > 0) throw new IllegalStateException("Already in chunked mode"); if (contentLength < 0) throw new IllegalArgumentException("contentLength < 0"); this.fixedContentLength = contentLength; super.fixedContentLength = (int) Math.min(contentLength, Integer.MAX_VALUE); }
public void testOfWithDuplicateKey() { try { ImmutableBiMap.of("one", 1, "one", 1); fail(); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).contains("one"); } }
static RuntimeException methodError(Method method, @Nullable Throwable cause, String message, Object... args) { message = String.format(message, args); return new IllegalArgumentException(message + "\n for method " + method.getDeclaringClass().getSimpleName() + "." + method.getName(), cause); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void mappingBadCapacityHint() throws Exception { Observable<Integer> source = Observable.just(1); try { Observable.just(source, source, source).concatMapEager((Function)Functions.identity(), 10, -99); } catch (IllegalArgumentException ex) { assertEquals("prefetch > 0 required but it was -99", ex.getMessage()); } }
@Override public boolean test(Integer t) throws Exception { throw new IllegalArgumentException(); } });
@Test public void takeNegative() { try { Observable.just(1).take(-99); fail("Should have thrown"); } catch (IllegalArgumentException ex) { assertEquals("count >= 0 required but it was -99", ex.getMessage()); } }
@Override public Integer apply(Integer t1) { throw new IllegalArgumentException("some error"); } }).blockingSingle();