method2testcases
stringlengths
118
6.63k
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> reverse() { return of(IterableUtils.reversedIterable(iterable)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void reverse() { List<Integer> result = FluentIterable.of(iterableA).reverse().toList(); List<Integer> expected = IterableUtils.toList(iterableA); Collections.reverse(expected); assertEquals(expected, result); result = FluentIterable.of(emptyIterable).reverse().toList(); assertEquals(0, result.size()); }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> skip(final long elementsToSkip) { return of(IterableUtils.skippingIterable(iterable, elementsToSkip)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void skip() { List<Integer> result = FluentIterable.of(iterableA).skip(4).toList(); assertEquals(6, result.size()); assertEquals(Arrays.asList(3, 3, 4, 4, 4, 4), result); result = FluentIterable.of(iterableA).skip(100).toList(); assertEquals(0, result.size()); result = FluentIterable.of(iterableA).skip(0).toList(); List<Integer> expected = IterableUtils.toList(iterableA); assertEquals(expected.size(), result.size()); assertEquals(expected, result); result = FluentIterable.of(emptyIterable).skip(3).toList(); assertEquals(0, result.size()); try { FluentIterable.of(iterableA).skip(-4).toList(); fail("expecting IllegalArgumentException"); } catch (IllegalArgumentException iae) { } }
### Question: FluentIterable implements Iterable<E> { public <O> FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer) { return of(IterableUtils.transformedIterable(iterable, transformer)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void transform() { Transformer<Integer, Integer> squared = new Transformer<Integer, Integer>() { @Override public Integer transform(Integer object) { return object * object; } }; List<Integer> result = FluentIterable.of(iterableA).transform(squared).toList(); assertEquals(10, result.size()); assertEquals(Arrays.asList(1, 4, 4, 9, 9, 9, 16, 16, 16, 16), result); result = FluentIterable.of(emptyIterable).transform(squared).toList(); assertEquals(0, result.size()); try { FluentIterable.of(iterableA).transform(null).toList(); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> unique() { return of(IterableUtils.uniqueIterable(iterable)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void unique() { List<Integer> result = FluentIterable.of(iterableA).unique().toList(); assertEquals(4, result.size()); assertEquals(Arrays.asList(1, 2, 3, 4), result); result = FluentIterable.of(emptyIterable).unique().toList(); assertEquals(0, result.size()); }
### Question: CacheableUpdateOperator extends UpdateOperator { @Override public Object execute(Object[] values, InvocationStat stat) { InvocationContext context = invocationContextFactory.newInvocationContext(values); Object r = execute(context, stat); if (driver.isUseMultipleKeys()) { Set<String> keys = driver.getCacheKeys(context); if (!keys.isEmpty()) { if (logger.isDebugEnabled()) { logger.debug("Cache delete for multiple keys {}", keys); } driver.batchDeleteFromCache(keys, stat); } } else { String key = driver.getCacheKey(context); if (logger.isDebugEnabled()) { logger.debug("Cache delete for single key [{}]", key); } driver.deleteFromCache(key, stat); } return r; } CacheableUpdateOperator(ASTRootNode rootNode, MethodDescriptor md, CacheDriver cacheDriver, Config config); @Override Object execute(Object[] values, InvocationStat stat); }### Answer: @Test public void testUpdate() throws Exception { TypeToken<User> pt = TypeToken.of(User.class); TypeToken<Integer> rt = TypeToken.of(int.class); String srcSql = "update user set name=:1.name where id=:1.id"; AbstractOperator operator = getOperator(pt, rt, srcSql, new CacheHandlerAdapter() { @Override public void delete(String key, Class<?> daoClass) { assertThat(key, equalTo("user_100")); } }, new MockCacheBy("id")); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public int update(DataSource ds, BoundSql boundSql) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "update user set name=? where id=?"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(2)); assertThat(args[0], equalTo((Object) "ash")); assertThat(args[1], equalTo((Object) 100)); return 1; } }); User user = new User(); user.setId(100); user.setName("ash"); InvocationStat stat = InvocationStat.create(); operator.execute(new Object[]{user}, stat); assertThat(stat.getCacheDeleteSuccessCount(), equalTo(1L)); } @Test public void testUpdateWithIn() throws Exception { TypeToken<List<Integer>> pt = new TypeToken<List<Integer>>() { }; TypeToken<Integer> rt = TypeToken.of(int.class); String srcSql = "update user set name=ash where id in (:1)"; AbstractOperator operator = getOperator(pt, rt, srcSql, new CacheHandlerAdapter() { @Override public void batchDelete(Set<String> keys, Class<?> daoClass) { Set<String> set = new HashSet<String>(); set.add("user_100"); set.add("user_200"); assertThat(keys, equalTo(set)); } }, new MockCacheBy("")); operator.setJdbcOperations(new JdbcOperationsAdapter() { @Override public int update(DataSource ds, BoundSql boundSql) { String sql = boundSql.getSql(); Object[] args = boundSql.getArgs().toArray(); String descSql = "update user set name=ash where id in (?,?)"; assertThat(sql, equalTo(descSql)); assertThat(args.length, equalTo(2)); assertThat(args[0], equalTo((Object) 100)); assertThat(args[1], equalTo((Object) 200)); return 1; } }); List<Integer> ids = Arrays.asList(100, 200); InvocationStat stat = InvocationStat.create(); operator.execute(new Object[]{ids}, stat); assertThat(stat.getCacheBatchDeleteSuccessCount(), equalTo(1L)); }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> unmodifiable() { return of(IterableUtils.unmodifiableIterable(iterable)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void unmodifiable() { FluentIterable<Integer> iterable1 = FluentIterable.of(iterableA).unmodifiable(); Iterator<Integer> it = iterable1.iterator(); assertEquals(1, it.next().intValue()); try { it.remove(); fail("expecting UnsupportedOperationException"); } catch (UnsupportedOperationException ise) { } FluentIterable<Integer> iterable2 = iterable1.unmodifiable(); assertSame(iterable1, iterable2); }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> zip(final Iterable<? extends E> other) { return of(IterableUtils.zippingIterable(iterable, other)); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @SuppressWarnings("unchecked") @Test public void zip() { List<Integer> result = FluentIterable.of(iterableOdd).zip(iterableEven).toList(); List<Integer> combinedList = new ArrayList<Integer>(); CollectionUtils.addAll(combinedList, iterableOdd); CollectionUtils.addAll(combinedList, iterableEven); Collections.sort(combinedList); assertEquals(combinedList, result); try { FluentIterable.of(iterableOdd).zip((Iterable<Integer>) null).toList(); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } result = FluentIterable .of(Arrays.asList(1, 4, 7)) .zip(Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9)) .toList(); combinedList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); assertEquals(combinedList, result); }
### Question: FluentIterable implements Iterable<E> { public Enumeration<E> asEnumeration() { return IteratorUtils.asEnumeration(iterator()); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void asEnumeration() { Enumeration<Long> enumeration = FluentIterable.of(iterableB).asEnumeration(); List<Long> result = EnumerationUtils.toList(enumeration); assertEquals(iterableB, result); enumeration = FluentIterable.<Long>empty().asEnumeration(); assertFalse(enumeration.hasMoreElements()); }
### Question: FluentIterable implements Iterable<E> { public boolean allMatch(final Predicate<? super E> predicate) { return IterableUtils.matchesAll(iterable, predicate); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void allMatch() { assertTrue(FluentIterable.of(iterableEven).allMatch(EVEN)); assertFalse(FluentIterable.of(iterableOdd).allMatch(EVEN)); assertFalse(FluentIterable.of(iterableA).allMatch(EVEN)); try { FluentIterable.of(iterableEven).allMatch(null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { public boolean anyMatch(final Predicate<? super E> predicate) { return IterableUtils.matchesAny(iterable, predicate); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void anyMatch() { assertTrue(FluentIterable.of(iterableEven).anyMatch(EVEN)); assertFalse(FluentIterable.of(iterableOdd).anyMatch(EVEN)); assertTrue(FluentIterable.of(iterableA).anyMatch(EVEN)); try { FluentIterable.of(iterableEven).anyMatch(null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { public boolean isEmpty() { return IterableUtils.isEmpty(iterable); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void isEmpty() { assertTrue(FluentIterable.of(emptyIterable).isEmpty()); assertFalse(FluentIterable.of(iterableOdd).isEmpty()); }
### Question: FluentIterable implements Iterable<E> { public int size() { return IterableUtils.size(iterable); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void size() { try { FluentIterable.of((Iterable<?>) null).size(); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } assertEquals(0, FluentIterable.of(emptyIterable).size()); assertEquals(IterableUtils.toList(iterableOdd).size(), FluentIterable.of(iterableOdd).size()); }
### Question: FluentIterable implements Iterable<E> { public FluentIterable<E> eval() { return of(toList()); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void eval() { List<Integer> listNumbers = new ArrayList<Integer>(); listNumbers.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); FluentIterable<Integer> iterable = FluentIterable.of(listNumbers).filter(EVEN); FluentIterable<Integer> materialized = iterable.eval(); listNumbers.addAll(Arrays.asList(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)); assertEquals(5, materialized.size()); assertEquals(10, iterable.size()); assertEquals(Arrays.asList(2, 4, 6, 8, 10), materialized.toList()); assertEquals(Arrays.asList(2, 4, 6, 8, 10, 12, 14, 16, 18, 20), iterable.toList()); }
### Question: FluentIterable implements Iterable<E> { public boolean contains(final Object object) { return IterableUtils.contains(iterable, object); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void contains() { assertTrue(FluentIterable.of(iterableEven).contains(2)); assertFalse(FluentIterable.of(iterableEven).contains(1)); assertFalse(FluentIterable.of(iterableEven).contains(null)); assertTrue(FluentIterable.of(iterableEven).append((Integer) null).contains(null)); }
### Question: FluentIterable implements Iterable<E> { public void copyInto(final Collection<? super E> collection) { if (collection == null) { throw new NullPointerException("Collection must not be null"); } CollectionUtils.addAll(collection, iterable); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void copyInto() { List<Integer> result = new ArrayList<Integer>(); FluentIterable.of(iterableA).copyInto(result); List<Integer> expected = IterableUtils.toList(iterableA); assertEquals(expected.size(), result.size()); assertEquals(expected, result); result = new ArrayList<Integer>(); result.add(10); result.add(9); result.add(8); FluentIterable.of(iterableA).copyInto(result); expected = new ArrayList<Integer>(); expected.addAll(Arrays.asList(10, 9, 8)); expected.addAll(IterableUtils.toList(iterableA)); assertEquals(expected.size(), result.size()); assertEquals(expected, result); try { FluentIterable.of(iterableA).copyInto(null); fail("expecting NullPointerException"); } catch (NullPointerException npe) { } }
### Question: FluentIterable implements Iterable<E> { @Override public Iterator<E> iterator() { return iterable.iterator(); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void iterator() { Iterator<Integer> iterator = FluentIterable.of(iterableA).iterator(); assertTrue(iterator.hasNext()); iterator = FluentIterable.<Integer>empty().iterator(); assertFalse(iterator.hasNext()); }
### Question: FluentIterable implements Iterable<E> { public E get(final int position) { return IterableUtils.get(iterable, position); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void get() { assertEquals(2, FluentIterable.of(iterableEven).get(0).intValue()); try { FluentIterable.of(iterableEven).get(-1); fail("expecting IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException ioe) { } try { FluentIterable.of(iterableEven).get(IterableUtils.size(iterableEven)); fail("expecting IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException ioe) { } }
### Question: FluentIterable implements Iterable<E> { public E[] toArray(final Class<E> arrayClass) { return IteratorUtils.toArray(iterator(), arrayClass); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void toArray() { Long[] arr = new Long[] {1L, 2L, 3L, 4L, 5L}; Long[] result = FluentIterable.of(arr).toArray(Long.class); assertNotNull(result); assertArrayEquals(arr, result); try { FluentIterable.of(arr).toArray((Class) String.class); } catch (ArrayStoreException ase) { } }
### Question: FluentIterable implements Iterable<E> { @Override public String toString() { return IterableUtils.toString(iterable); } FluentIterable(); private FluentIterable(final Iterable<E> iterable); @SuppressWarnings("unchecked") static FluentIterable<T> empty(); static FluentIterable<T> of(final T singleton); static FluentIterable<T> of(final T... elements); static FluentIterable<T> of(final Iterable<T> iterable); FluentIterable<E> append(final E... elements); FluentIterable<E> append(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other); FluentIterable<E> collate(final Iterable<? extends E> other, final Comparator<? super E> comparator); FluentIterable<E> eval(); FluentIterable<E> filter(final Predicate<? super E> predicate); FluentIterable<E> limit(final long maxSize); FluentIterable<E> loop(); FluentIterable<E> reverse(); FluentIterable<E> skip(final long elementsToSkip); FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer); FluentIterable<E> unique(); FluentIterable<E> unmodifiable(); FluentIterable<E> zip(final Iterable<? extends E> other); FluentIterable<E> zip(final Iterable<? extends E>... others); @Override Iterator<E> iterator(); Enumeration<E> asEnumeration(); boolean allMatch(final Predicate<? super E> predicate); boolean anyMatch(final Predicate<? super E> predicate); boolean isEmpty(); boolean contains(final Object object); void forEach(final Closure<? super E> closure); E get(final int position); int size(); void copyInto(final Collection<? super E> collection); E[] toArray(final Class<E> arrayClass); List<E> toList(); @Override String toString(); }### Answer: @Test public void testToString() { String result = FluentIterable.of(iterableA).toString(); assertEquals(iterableA.toString(), result); result = FluentIterable.empty().toString(); assertEquals("[]", result); }
### Question: Charsets { public static Charset toCharset(final Charset charset) { return charset == null ? Charset.defaultCharset() : charset; } static Charset toCharset(final Charset charset); static Charset toCharset(final String charset); @Deprecated static final Charset ISO_8859_1; @Deprecated static final Charset US_ASCII; @Deprecated static final Charset UTF_16; @Deprecated static final Charset UTF_16BE; @Deprecated static final Charset UTF_16LE; @Deprecated static final Charset UTF_8; }### Answer: @Test public void testToCharset() { Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((String) null)); Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset((Charset) null)); Assert.assertEquals(Charset.defaultCharset(), Charsets.toCharset(Charset.defaultCharset())); Assert.assertEquals(Charset.forName("UTF-8"), Charsets.toCharset(Charset.forName("UTF-8"))); }
### Question: StringEncoderComparator implements Comparator { @Override public int compare(final Object o1, final Object o2) { int compareCode = 0; try { @SuppressWarnings("unchecked") final Comparable<Comparable<?>> s1 = (Comparable<Comparable<?>>) this.stringEncoder.encode(o1); final Comparable<?> s2 = (Comparable<?>) this.stringEncoder.encode(o2); compareCode = s1.compareTo(s2); } catch (final EncoderException ee) { compareCode = 0; } return compareCode; } @Deprecated StringEncoderComparator(); StringEncoderComparator(final StringEncoder stringEncoder); @Override int compare(final Object o1, final Object o2); }### Answer: @Test public void testComparatorWithSoundex() throws Exception { final StringEncoderComparator sCompare = new StringEncoderComparator( new Soundex() ); assertTrue( "O'Brien and O'Brian didn't come out with " + "the same Soundex, something must be wrong here", 0 == sCompare.compare( "O'Brien", "O'Brian" ) ); } @Test public void testComparatorWithDoubleMetaphoneAndInvalidInput() throws Exception { final StringEncoderComparator sCompare = new StringEncoderComparator( new DoubleMetaphone() ); final int compare = sCompare.compare(new Double(3.0), Long.valueOf(3)); assertEquals( "Trying to compare objects that make no sense to the underlying encoder should return a zero compare code", 0, compare); }
### Question: UnixCrypt { public static String crypt(final byte[] original) { return crypt(original, null); } static String crypt(final byte[] original); static String crypt(final byte[] original, String salt); static String crypt(final String original); static String crypt(final String original, final String salt); }### Answer: @Test public void testUnixCryptStrings() { assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xx")); assertEquals("12UFlHxel6uMM", Crypt.crypt("", "12")); assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12")); assertEquals("12FJgqDtVOg7Q", Crypt.crypt("secret", "12345678")); } @Test public void testUnixCryptBytes() { assertEquals("12UFlHxel6uMM", Crypt.crypt(new byte[0], "12")); assertEquals("./287bds2PjVw", Crypt.crypt("t\u00e4st", "./")); assertEquals("./bLIFNqo9XKQ", Crypt.crypt("t\u00e4st".getBytes(Charsets.ISO_8859_1), "./")); assertEquals("./bLIFNqo9XKQ", Crypt.crypt(new byte[]{(byte) 0x74, (byte) 0xe4, (byte) 0x73, (byte) 0x74}, "./")); } @Test public void testUnixCryptExplicitCall() { assertTrue(UnixCrypt.crypt("secret".getBytes()).matches("^[a-zA-Z0-9./]{13}$")); assertTrue(UnixCrypt.crypt("secret".getBytes(), null).matches("^[a-zA-Z0-9./]{13}$")); } @Test(expected = IllegalArgumentException.class) public void testUnixCryptWithHalfSalt() { UnixCrypt.crypt("secret", "x"); } @Test(expected = IllegalArgumentException.class) public void testUnicCryptInvalidSalt() { UnixCrypt.crypt("secret", "$a"); } @Test(expected = NullPointerException.class) public void testUnixCryptNullData() { UnixCrypt.crypt((byte[]) null); } @Test(expected = IllegalArgumentException.class) public void testUnixCryptWithEmptySalt() { UnixCrypt.crypt("secret", ""); } @Test public void testUnixCryptWithoutSalt() { final String hash = UnixCrypt.crypt("foo"); assertTrue(hash.matches("^[a-zA-Z0-9./]{13}$")); final String hash2 = UnixCrypt.crypt("foo"); assertNotSame(hash, hash2); }
### Question: B64 { static void b64from24bit(final byte b2, final byte b1, final byte b0, final int outLen, final StringBuilder buffer) { int w = ((b2 << 16) & 0x00ffffff) | ((b1 << 8) & 0x00ffff) | (b0 & 0xff); int n = outLen; while (n-- > 0) { buffer.append(B64T.charAt(w & 0x3f)); w >>= 6; } } }### Answer: @Test public void testB64from24bit() { final StringBuilder buffer = new StringBuilder(""); B64.b64from24bit((byte) 8, (byte) 16, (byte) 64, 2, buffer); B64.b64from24bit((byte) 7, (byte) 77, (byte) 120, 4, buffer); assertEquals("./spo/", buffer.toString()); }
### Question: DigestUtils { public static MessageDigest getDigest(final String algorithm) { try { return MessageDigest.getInstance(algorithm); } catch (final NoSuchAlgorithmException e) { throw new IllegalArgumentException(e); } } static MessageDigest getDigest(final String algorithm); static MessageDigest getMd2Digest(); static MessageDigest getMd5Digest(); static MessageDigest getSha1Digest(); static MessageDigest getSha256Digest(); static MessageDigest getSha384Digest(); static MessageDigest getSha512Digest(); @Deprecated static MessageDigest getShaDigest(); static byte[] md2(final byte[] data); static byte[] md2(final InputStream data); static byte[] md2(final String data); static String md2Hex(final byte[] data); static String md2Hex(final InputStream data); static String md2Hex(final String data); static byte[] md5(final byte[] data); static byte[] md5(final InputStream data); static byte[] md5(final String data); static String md5Hex(final byte[] data); static String md5Hex(final InputStream data); static String md5Hex(final String data); @Deprecated static byte[] sha(final byte[] data); @Deprecated static byte[] sha(final InputStream data); @Deprecated static byte[] sha(final String data); static byte[] sha1(final byte[] data); static byte[] sha1(final InputStream data); static byte[] sha1(final String data); static String sha1Hex(final byte[] data); static String sha1Hex(final InputStream data); static String sha1Hex(final String data); static byte[] sha256(final byte[] data); static byte[] sha256(final InputStream data); static byte[] sha256(final String data); static String sha256Hex(final byte[] data); static String sha256Hex(final InputStream data); static String sha256Hex(final String data); static byte[] sha384(final byte[] data); static byte[] sha384(final InputStream data); static byte[] sha384(final String data); static String sha384Hex(final byte[] data); static String sha384Hex(final InputStream data); static String sha384Hex(final String data); static byte[] sha512(final byte[] data); static byte[] sha512(final InputStream data); static byte[] sha512(final String data); static String sha512Hex(final byte[] data); static String sha512Hex(final InputStream data); static String sha512Hex(final String data); @Deprecated static String shaHex(final byte[] data); @Deprecated static String shaHex(final InputStream data); @Deprecated static String shaHex(final String data); static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest); static MessageDigest updateDigest(final MessageDigest digest, final InputStream data); static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest); }### Answer: @Test(expected=IllegalArgumentException.class) public void testInternalNoSuchAlgorithmException() { DigestUtils.getDigest("Bogus Bogus"); }
### Question: DigestUtils { public static byte[] md2(final byte[] data) { return getMd2Digest().digest(data); } static MessageDigest getDigest(final String algorithm); static MessageDigest getMd2Digest(); static MessageDigest getMd5Digest(); static MessageDigest getSha1Digest(); static MessageDigest getSha256Digest(); static MessageDigest getSha384Digest(); static MessageDigest getSha512Digest(); @Deprecated static MessageDigest getShaDigest(); static byte[] md2(final byte[] data); static byte[] md2(final InputStream data); static byte[] md2(final String data); static String md2Hex(final byte[] data); static String md2Hex(final InputStream data); static String md2Hex(final String data); static byte[] md5(final byte[] data); static byte[] md5(final InputStream data); static byte[] md5(final String data); static String md5Hex(final byte[] data); static String md5Hex(final InputStream data); static String md5Hex(final String data); @Deprecated static byte[] sha(final byte[] data); @Deprecated static byte[] sha(final InputStream data); @Deprecated static byte[] sha(final String data); static byte[] sha1(final byte[] data); static byte[] sha1(final InputStream data); static byte[] sha1(final String data); static String sha1Hex(final byte[] data); static String sha1Hex(final InputStream data); static String sha1Hex(final String data); static byte[] sha256(final byte[] data); static byte[] sha256(final InputStream data); static byte[] sha256(final String data); static String sha256Hex(final byte[] data); static String sha256Hex(final InputStream data); static String sha256Hex(final String data); static byte[] sha384(final byte[] data); static byte[] sha384(final InputStream data); static byte[] sha384(final String data); static String sha384Hex(final byte[] data); static String sha384Hex(final InputStream data); static String sha384Hex(final String data); static byte[] sha512(final byte[] data); static byte[] sha512(final InputStream data); static byte[] sha512(final String data); static String sha512Hex(final byte[] data); static String sha512Hex(final InputStream data); static String sha512Hex(final String data); @Deprecated static String shaHex(final byte[] data); @Deprecated static String shaHex(final InputStream data); @Deprecated static String shaHex(final String data); static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest); static MessageDigest updateDigest(final MessageDigest digest, final InputStream data); static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest); }### Answer: @Test public void testMd2Length() { String hashMe = "this is some string that is longer than 16 characters"; byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe)); assertEquals(16, hash.length); hashMe = "length < 16"; hash = DigestUtils.md2(getBytesUtf8(hashMe)); assertEquals(16, hash.length); }
### Question: DigestUtils { public static byte[] md5(final byte[] data) { return getMd5Digest().digest(data); } static MessageDigest getDigest(final String algorithm); static MessageDigest getMd2Digest(); static MessageDigest getMd5Digest(); static MessageDigest getSha1Digest(); static MessageDigest getSha256Digest(); static MessageDigest getSha384Digest(); static MessageDigest getSha512Digest(); @Deprecated static MessageDigest getShaDigest(); static byte[] md2(final byte[] data); static byte[] md2(final InputStream data); static byte[] md2(final String data); static String md2Hex(final byte[] data); static String md2Hex(final InputStream data); static String md2Hex(final String data); static byte[] md5(final byte[] data); static byte[] md5(final InputStream data); static byte[] md5(final String data); static String md5Hex(final byte[] data); static String md5Hex(final InputStream data); static String md5Hex(final String data); @Deprecated static byte[] sha(final byte[] data); @Deprecated static byte[] sha(final InputStream data); @Deprecated static byte[] sha(final String data); static byte[] sha1(final byte[] data); static byte[] sha1(final InputStream data); static byte[] sha1(final String data); static String sha1Hex(final byte[] data); static String sha1Hex(final InputStream data); static String sha1Hex(final String data); static byte[] sha256(final byte[] data); static byte[] sha256(final InputStream data); static byte[] sha256(final String data); static String sha256Hex(final byte[] data); static String sha256Hex(final InputStream data); static String sha256Hex(final String data); static byte[] sha384(final byte[] data); static byte[] sha384(final InputStream data); static byte[] sha384(final String data); static String sha384Hex(final byte[] data); static String sha384Hex(final InputStream data); static String sha384Hex(final String data); static byte[] sha512(final byte[] data); static byte[] sha512(final InputStream data); static byte[] sha512(final String data); static String sha512Hex(final byte[] data); static String sha512Hex(final InputStream data); static String sha512Hex(final String data); @Deprecated static String shaHex(final byte[] data); @Deprecated static String shaHex(final InputStream data); @Deprecated static String shaHex(final String data); static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest); static MessageDigest updateDigest(final MessageDigest digest, final InputStream data); static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest); }### Answer: @Test public void testMd5Length() { String hashMe = "this is some string that is longer than 16 characters"; byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe)); assertEquals(16, hash.length); hashMe = "length < 16"; hash = DigestUtils.md5(getBytesUtf8(hashMe)); assertEquals(16, hash.length); }
### Question: DigestUtils { public static String sha1Hex(final byte[] data) { return Hex.encodeHexString(sha1(data)); } static MessageDigest getDigest(final String algorithm); static MessageDigest getMd2Digest(); static MessageDigest getMd5Digest(); static MessageDigest getSha1Digest(); static MessageDigest getSha256Digest(); static MessageDigest getSha384Digest(); static MessageDigest getSha512Digest(); @Deprecated static MessageDigest getShaDigest(); static byte[] md2(final byte[] data); static byte[] md2(final InputStream data); static byte[] md2(final String data); static String md2Hex(final byte[] data); static String md2Hex(final InputStream data); static String md2Hex(final String data); static byte[] md5(final byte[] data); static byte[] md5(final InputStream data); static byte[] md5(final String data); static String md5Hex(final byte[] data); static String md5Hex(final InputStream data); static String md5Hex(final String data); @Deprecated static byte[] sha(final byte[] data); @Deprecated static byte[] sha(final InputStream data); @Deprecated static byte[] sha(final String data); static byte[] sha1(final byte[] data); static byte[] sha1(final InputStream data); static byte[] sha1(final String data); static String sha1Hex(final byte[] data); static String sha1Hex(final InputStream data); static String sha1Hex(final String data); static byte[] sha256(final byte[] data); static byte[] sha256(final InputStream data); static byte[] sha256(final String data); static String sha256Hex(final byte[] data); static String sha256Hex(final InputStream data); static String sha256Hex(final String data); static byte[] sha384(final byte[] data); static byte[] sha384(final InputStream data); static byte[] sha384(final String data); static String sha384Hex(final byte[] data); static String sha384Hex(final InputStream data); static String sha384Hex(final String data); static byte[] sha512(final byte[] data); static byte[] sha512(final InputStream data); static byte[] sha512(final String data); static String sha512Hex(final byte[] data); static String sha512Hex(final InputStream data); static String sha512Hex(final String data); @Deprecated static String shaHex(final byte[] data); @Deprecated static String shaHex(final InputStream data); @Deprecated static String shaHex(final String data); static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest); static MessageDigest updateDigest(final MessageDigest digest, final InputStream data); static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest); }### Answer: @Test public void testSha1Hex() throws IOException { assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc")); assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc"))); assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.sha1Hex(testData), DigestUtils.sha1Hex(new ByteArrayInputStream(testData))); }
### Question: DigestUtils { public static byte[] sha256(final byte[] data) { return getSha256Digest().digest(data); } static MessageDigest getDigest(final String algorithm); static MessageDigest getMd2Digest(); static MessageDigest getMd5Digest(); static MessageDigest getSha1Digest(); static MessageDigest getSha256Digest(); static MessageDigest getSha384Digest(); static MessageDigest getSha512Digest(); @Deprecated static MessageDigest getShaDigest(); static byte[] md2(final byte[] data); static byte[] md2(final InputStream data); static byte[] md2(final String data); static String md2Hex(final byte[] data); static String md2Hex(final InputStream data); static String md2Hex(final String data); static byte[] md5(final byte[] data); static byte[] md5(final InputStream data); static byte[] md5(final String data); static String md5Hex(final byte[] data); static String md5Hex(final InputStream data); static String md5Hex(final String data); @Deprecated static byte[] sha(final byte[] data); @Deprecated static byte[] sha(final InputStream data); @Deprecated static byte[] sha(final String data); static byte[] sha1(final byte[] data); static byte[] sha1(final InputStream data); static byte[] sha1(final String data); static String sha1Hex(final byte[] data); static String sha1Hex(final InputStream data); static String sha1Hex(final String data); static byte[] sha256(final byte[] data); static byte[] sha256(final InputStream data); static byte[] sha256(final String data); static String sha256Hex(final byte[] data); static String sha256Hex(final InputStream data); static String sha256Hex(final String data); static byte[] sha384(final byte[] data); static byte[] sha384(final InputStream data); static byte[] sha384(final String data); static String sha384Hex(final byte[] data); static String sha384Hex(final InputStream data); static String sha384Hex(final String data); static byte[] sha512(final byte[] data); static byte[] sha512(final InputStream data); static byte[] sha512(final String data); static String sha512Hex(final byte[] data); static String sha512Hex(final InputStream data); static String sha512Hex(final String data); @Deprecated static String shaHex(final byte[] data); @Deprecated static String shaHex(final InputStream data); @Deprecated static String shaHex(final String data); static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest); static MessageDigest updateDigest(final MessageDigest digest, final InputStream data); static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest); }### Answer: @Test public void testSha256() throws IOException { assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex("abc")); assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex(getBytesUtf8("abc"))); assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.sha256Hex(testData), DigestUtils.sha256Hex(new ByteArrayInputStream(testData))); }
### Question: DigestUtils { @Deprecated public static String shaHex(final byte[] data) { return sha1Hex(data); } static MessageDigest getDigest(final String algorithm); static MessageDigest getMd2Digest(); static MessageDigest getMd5Digest(); static MessageDigest getSha1Digest(); static MessageDigest getSha256Digest(); static MessageDigest getSha384Digest(); static MessageDigest getSha512Digest(); @Deprecated static MessageDigest getShaDigest(); static byte[] md2(final byte[] data); static byte[] md2(final InputStream data); static byte[] md2(final String data); static String md2Hex(final byte[] data); static String md2Hex(final InputStream data); static String md2Hex(final String data); static byte[] md5(final byte[] data); static byte[] md5(final InputStream data); static byte[] md5(final String data); static String md5Hex(final byte[] data); static String md5Hex(final InputStream data); static String md5Hex(final String data); @Deprecated static byte[] sha(final byte[] data); @Deprecated static byte[] sha(final InputStream data); @Deprecated static byte[] sha(final String data); static byte[] sha1(final byte[] data); static byte[] sha1(final InputStream data); static byte[] sha1(final String data); static String sha1Hex(final byte[] data); static String sha1Hex(final InputStream data); static String sha1Hex(final String data); static byte[] sha256(final byte[] data); static byte[] sha256(final InputStream data); static byte[] sha256(final String data); static String sha256Hex(final byte[] data); static String sha256Hex(final InputStream data); static String sha256Hex(final String data); static byte[] sha384(final byte[] data); static byte[] sha384(final InputStream data); static byte[] sha384(final String data); static String sha384Hex(final byte[] data); static String sha384Hex(final InputStream data); static String sha384Hex(final String data); static byte[] sha512(final byte[] data); static byte[] sha512(final InputStream data); static byte[] sha512(final String data); static String sha512Hex(final byte[] data); static String sha512Hex(final InputStream data); static String sha512Hex(final String data); @Deprecated static String shaHex(final byte[] data); @Deprecated static String shaHex(final InputStream data); @Deprecated static String shaHex(final String data); static MessageDigest updateDigest(final MessageDigest messageDigest, final byte[] valueToDigest); static MessageDigest updateDigest(final MessageDigest digest, final InputStream data); static MessageDigest updateDigest(final MessageDigest messageDigest, final String valueToDigest); }### Answer: @SuppressWarnings("deprecation") @Test public void testShaHex() throws IOException { assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc"))); assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.shaHex(testData), DigestUtils.shaHex(new ByteArrayInputStream(testData))); }
### Question: HmacUtils { public static Mac getHmacMd5(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_MD5, key); } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testEmptyKey() { HmacUtils.getHmacMd5(new byte[] {}); } @Test(expected = IllegalArgumentException.class) public void testNullKey() { HmacUtils.getHmacMd5(null); }
### Question: HmacUtils { public static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key) { return getInitializedMac(algorithm.toString(), key); } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testInitializedMacNullAlgo() throws IOException { HmacUtils.getInitializedMac((String) null, STANDARD_KEY_BYTES); } @Test(expected = IllegalArgumentException.class) public void testInitializedMacNullKey() throws IOException { HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, null); } @Test(expected = IllegalArgumentException.class) public void testInternalNoSuchAlgorithmException() { HmacUtils.getInitializedMac("Bogus Bogus", StringUtils.getBytesUtf8("akey")); }
### Question: HmacUtils { public static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest) { try { return getHmacMd5(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { throw new IllegalArgumentException(e); } } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testMd5HMacFail() throws IOException { HmacUtils.hmacMd5((byte[]) null, STANDARD_PHRASE_BYTES); }
### Question: HmacUtils { public static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha1(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { throw new IllegalArgumentException(e); } } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testSha1HMacFail() throws IOException { HmacUtils.hmacSha1((byte[]) null, STANDARD_PHRASE_BYTES); }
### Question: HmacUtils { public static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha256(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { throw new IllegalArgumentException(e); } } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testSha256HMacFail() throws IOException { HmacUtils.hmacSha256((byte[]) null, STANDARD_PHRASE_BYTES); }
### Question: HmacUtils { public static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha384(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { throw new IllegalArgumentException(e); } } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testSha384HMacFail() throws IOException { HmacUtils.hmacSha384((byte[]) null, STANDARD_PHRASE_BYTES); }
### Question: HmacUtils { public static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha512(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { throw new IllegalArgumentException(e); } } static Mac getHmacMd5(final byte[] key); static Mac getHmacSha1(final byte[] key); static Mac getHmacSha256(final byte[] key); static Mac getHmacSha384(final byte[] key); static Mac getHmacSha512(final byte[] key); static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key); static Mac getInitializedMac(final String algorithm, final byte[] key); static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest); static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest); static byte[] hmacMd5(final String key, final String valueToDigest); static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest); static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest); static String hmacMd5Hex(final String key, final String valueToDigest); static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha1(final String key, final String valueToDigest); static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha1Hex(final String key, final String valueToDigest); static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha256(final String key, final String valueToDigest); static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha256Hex(final String key, final String valueToDigest); static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha384(final String key, final String valueToDigest); static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha384Hex(final String key, final String valueToDigest); static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest); static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest); static byte[] hmacSha512(final String key, final String valueToDigest); static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest); static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest); static String hmacSha512Hex(final String key, final String valueToDigest); static Mac updateHmac(final Mac mac, final byte[] valueToDigest); static Mac updateHmac(final Mac mac, final InputStream valueToDigest); static Mac updateHmac(final Mac mac, final String valueToDigest); }### Answer: @Test(expected = IllegalArgumentException.class) public void testSha512HMacFail() throws IOException { HmacUtils.hmacSha512((byte[]) null, STANDARD_PHRASE_BYTES); }
### Question: MethodNameParser { public static MethodNameInfo parse(String str) { OrderUnit ou = parseOrderUnit(str); if (ou != null) { str = str.substring(0, str.length() - ou.getOrderStrSize()); } List<OpUnit> opUnits = new ArrayList<OpUnit>(); List<String> logics = new ArrayList<String>(); Pattern p = Pattern.compile(LOGIC_REGEX); Matcher m = p.matcher(str); int index = 0; while (m.find()) { opUnits.add(OpUnit.create(str.substring(index, m.start()))); logics.add(Strings.firstLetterToLowerCase(m.group())); index = m.end(); } opUnits.add(OpUnit.create(str.substring(index))); return new MethodNameInfo(opUnits, logics, ou); } static MethodNameInfo parse(String str); }### Answer: @Test public void parse() throws Exception { MethodNameInfo info = MethodNameParser.parse("EmailAddressAndLastnameLessThanOrIdOrderByUserIdDesc"); assertThat(info.getLogics(), equalTo(Arrays.asList("and", "or"))); OrderUnit ou = info.getOrderUnit(); assertThat(ou.getOrderType(), equalTo(OrderType.DESC)); assertThat(ou.getProperty(), equalTo("userId")); assertThat(ou.getOrderStrSize(), equalTo(17)); List<OpUnit> ous = info.getOpUnits(); assertThat(ous.size(), equalTo(3)); assertThat(ous.get(0).getOp(), equalTo((Op) new EqualsOp())); assertThat(ous.get(0).getProperty(), equalTo("emailAddress")); assertThat(ous.get(1).getOp(), equalTo((Op) new LessThanOp())); assertThat(ous.get(1).getProperty(), equalTo("lastname")); assertThat(ous.get(2).getOp(), equalTo((Op) new EqualsOp())); assertThat(ous.get(2).getProperty(), equalTo("id")); }
### Question: DynamicTokens { public static <E> TypeToken<List<E>> listToken(TypeToken<E> entityToken) { return new TypeToken<List<E>>() {} .where(new TypeParameter<E>() {}, entityToken); } static TypeToken<Collection<E>> collectionToken(TypeToken<E> entityToken); static TypeToken<List<E>> listToken(TypeToken<E> entityToken); }### Answer: @Test public void testListToken() throws Exception { TypeToken<List<String>> token = DynamicTokens.listToken(TypeToken.of(String.class)); Type expectedType = DynamicTokensTest.class.getMethod("func2").getGenericReturnType(); assertThat(token.getType(), equalTo(expectedType)); }
### Question: Crypt { public static String crypt(final byte[] keyBytes) { return crypt(keyBytes, null); } static String crypt(final byte[] keyBytes); static String crypt(final byte[] keyBytes, final String salt); static String crypt(final String key); static String crypt(final String key, final String salt); }### Answer: @Test public void testCrypt() { assertNotNull(new Crypt()); } @Test public void testDefaultCryptVariant() { assertTrue(Crypt.crypt("secret").startsWith("$6$")); assertTrue(Crypt.crypt("secret", null).startsWith("$6$")); } @Test public void testCryptWithBytes() { final byte[] keyBytes = new byte[] { 'b', 'y', 't', 'e' }; final String hash = Crypt.crypt(keyBytes); assertEquals(hash, Crypt.crypt("byte", hash)); } @Test(expected = IllegalArgumentException.class) public void testCryptWithEmptySalt() { Crypt.crypt("secret", ""); }
### Question: Md5Crypt { public static String md5Crypt(final byte[] keyBytes) { return md5Crypt(keyBytes, MD5_PREFIX + B64.getRandomSalt(8)); } static String apr1Crypt(final byte[] keyBytes); static String apr1Crypt(final byte[] keyBytes, String salt); static String apr1Crypt(final String keyBytes); static String apr1Crypt(final String keyBytes, final String salt); static String md5Crypt(final byte[] keyBytes); static String md5Crypt(final byte[] keyBytes, final String salt); static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix); }### Answer: @Test public void testMd5CryptExplicitCall() { assertTrue(Md5Crypt.md5Crypt("secret".getBytes()).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$")); assertTrue(Md5Crypt.md5Crypt("secret".getBytes(), null).matches("^\\$1\\$[a-zA-Z0-9./]{0,8}\\$.{1,}$")); } @Test(expected = NullPointerException.class) public void testMd5CryptNullData() { Md5Crypt.md5Crypt((byte[]) null); } @Test(expected = IllegalArgumentException.class) public void testMd5CryptWithEmptySalt() { Md5Crypt.md5Crypt("secret".getBytes(), ""); }
### Question: StringUtils { public static byte[] getBytesIso8859_1(final String string) { return getBytes(string, Charsets.ISO_8859_1); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesIso8859_1() throws UnsupportedEncodingException { final String charsetName = "ISO-8859-1"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesIso8859_1(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
### Question: StringUtils { public static byte[] getBytesUsAscii(final String string) { return getBytes(string, Charsets.US_ASCII); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesUsAscii() throws UnsupportedEncodingException { final String charsetName = "US-ASCII"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesUsAscii(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
### Question: StringUtils { public static byte[] getBytesUtf16(final String string) { return getBytes(string, Charsets.UTF_16); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesUtf16() throws UnsupportedEncodingException { final String charsetName = "UTF-16"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesUtf16(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
### Question: MethodNameParser { @Nullable static OrderUnit parseOrderUnit(String str) { Pattern p = Pattern.compile(ORDER_BY_REGEX); Matcher m = p.matcher(str); if (m.find()) { String tailStr = Strings.firstLetterToLowerCase(str.substring(m.end() - 1)); int size = ORDER_BY.length() + tailStr.length(); String property; OrderType orderType; if (tailStr.endsWith(DESC)) { property = tailStr.substring(0, tailStr.length() - DESC.length()); orderType = OrderType.DESC; } else if (tailStr.endsWith(ASC)) { property = tailStr.substring(0, tailStr.length() - ASC.length()); orderType = OrderType.ASC; } else { property = tailStr; orderType = OrderType.NONE; } return new OrderUnit(property, orderType, size); } return null; } static MethodNameInfo parse(String str); }### Answer: @Test public void parseOrderUnit() throws Exception { OrderUnit ou = MethodNameParser.parseOrderUnit("AgeAndNameOrderByIdDesc"); assertThat(ou.getProperty(), equalTo("id")); assertThat(ou.getOrderType(), equalTo(OrderType.DESC)); assertThat(ou.getOrderStrSize(), equalTo("OrderByIdDesc".length())); ou = MethodNameParser.parseOrderUnit("AgeAndNameOrderByUserNameAsc"); assertThat(ou.getProperty(), equalTo("userName")); assertThat(ou.getOrderType(), equalTo(OrderType.ASC)); assertThat(ou.getOrderStrSize(), equalTo("OrderByUserNameAsc".length())); ou = MethodNameParser.parseOrderUnit("AgeAndNameOrderByAgeU"); assertThat(ou.getProperty(), equalTo("ageU")); assertThat(ou.getOrderType(), equalTo(OrderType.NONE)); assertThat(ou.getOrderStrSize(), equalTo("OrderByAgeU".length())); }
### Question: StringUtils { public static byte[] getBytesUtf16Be(final String string) { return getBytes(string, Charsets.UTF_16BE); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesUtf16Be() throws UnsupportedEncodingException { final String charsetName = "UTF-16BE"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesUtf16Be(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
### Question: StringUtils { public static byte[] getBytesUtf16Le(final String string) { return getBytes(string, Charsets.UTF_16LE); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesUtf16Le() throws UnsupportedEncodingException { final String charsetName = "UTF-16LE"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesUtf16Le(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
### Question: StringUtils { public static byte[] getBytesUtf8(final String string) { return getBytes(string, Charsets.UTF_8); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesUtf8() throws UnsupportedEncodingException { final String charsetName = "UTF-8"; testGetBytesUnchecked(charsetName); final byte[] expected = STRING_FIXTURE.getBytes(charsetName); final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE); Assert.assertTrue(Arrays.equals(expected, actual)); }
### Question: StringUtils { public static byte[] getBytesUnchecked(final String string, final String charsetName) { if (string == null) { return null; } try { return string.getBytes(charsetName); } catch (final UnsupportedEncodingException e) { throw StringUtils.newIllegalStateException(charsetName, e); } } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testGetBytesUncheckedBadName() { try { StringUtils.getBytesUnchecked(STRING_FIXTURE, "UNKNOWN"); Assert.fail("Expected " + IllegalStateException.class.getName()); } catch (final IllegalStateException e) { } } @Test public void testGetBytesUncheckedNullInput() { Assert.assertNull(StringUtils.getBytesUnchecked(null, "UNKNOWN")); }
### Question: StringUtils { private static String newString(final byte[] bytes, final Charset charset) { return bytes == null ? null : new String(bytes, charset); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringBadEnc() { try { StringUtils.newString(BYTES_FIXTURE, "UNKNOWN"); Assert.fail("Expected " + IllegalStateException.class.getName()); } catch (final IllegalStateException e) { } } @Test public void testNewStringNullInput() { Assert.assertNull(StringUtils.newString(null, "UNKNOWN")); }
### Question: StringUtils { public static String newStringIso8859_1(final byte[] bytes) { return new String(bytes, Charsets.ISO_8859_1); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringIso8859_1() throws UnsupportedEncodingException { final String charsetName = "ISO-8859-1"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE, charsetName); final String actual = StringUtils.newStringIso8859_1(BYTES_FIXTURE); Assert.assertEquals(expected, actual); }
### Question: StringUtils { public static String newStringUsAscii(final byte[] bytes) { return new String(bytes, Charsets.US_ASCII); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringUsAscii() throws UnsupportedEncodingException { final String charsetName = "US-ASCII"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE, charsetName); final String actual = StringUtils.newStringUsAscii(BYTES_FIXTURE); Assert.assertEquals(expected, actual); }
### Question: StringUtils { public static String newStringUtf16(final byte[] bytes) { return new String(bytes, Charsets.UTF_16); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringUtf16() throws UnsupportedEncodingException { final String charsetName = "UTF-16"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE, charsetName); final String actual = StringUtils.newStringUtf16(BYTES_FIXTURE); Assert.assertEquals(expected, actual); }
### Question: LessThanOp extends Param1Op { @Override public String keyword() { return "LessThan"; } @Override String keyword(); @Override String operator(); }### Answer: @Test public void test() throws Exception { Op op = new LessThanOp(); assertThat(op.keyword(), equalTo("LessThan")); assertThat(op.paramCount(), equalTo(1)); assertThat(op.render("id", new String[] {":1"}), equalTo("id < :1")); }
### Question: StringUtils { public static String newStringUtf16Be(final byte[] bytes) { return new String(bytes, Charsets.UTF_16BE); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringUtf16Be() throws UnsupportedEncodingException { final String charsetName = "UTF-16BE"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE_16BE, charsetName); final String actual = StringUtils.newStringUtf16Be(BYTES_FIXTURE_16BE); Assert.assertEquals(expected, actual); }
### Question: StringUtils { public static String newStringUtf16Le(final byte[] bytes) { return new String(bytes, Charsets.UTF_16LE); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringUtf16Le() throws UnsupportedEncodingException { final String charsetName = "UTF-16LE"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE_16LE, charsetName); final String actual = StringUtils.newStringUtf16Le(BYTES_FIXTURE_16LE); Assert.assertEquals(expected, actual); }
### Question: StringUtils { public static String newStringUtf8(final byte[] bytes) { return newString(bytes, Charsets.UTF_8); } static boolean equals(final CharSequence cs1, final CharSequence cs2); static byte[] getBytesIso8859_1(final String string); static byte[] getBytesUnchecked(final String string, final String charsetName); static byte[] getBytesUsAscii(final String string); static byte[] getBytesUtf16(final String string); static byte[] getBytesUtf16Be(final String string); static byte[] getBytesUtf16Le(final String string); static byte[] getBytesUtf8(final String string); static String newString(final byte[] bytes, final String charsetName); static String newStringIso8859_1(final byte[] bytes); static String newStringUsAscii(final byte[] bytes); static String newStringUtf16(final byte[] bytes); static String newStringUtf16Be(final byte[] bytes); static String newStringUtf16Le(final byte[] bytes); static String newStringUtf8(final byte[] bytes); }### Answer: @Test public void testNewStringUtf8() throws UnsupportedEncodingException { final String charsetName = "UTF-8"; testNewString(charsetName); final String expected = new String(BYTES_FIXTURE, charsetName); final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE); Assert.assertEquals(expected, actual); }
### Question: BaseNCodec implements BinaryEncoder, BinaryDecoder { protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength) { this(unencodedBlockSize, encodedBlockSize, lineLength, chunkSeparatorLength, PAD_DEFAULT); } protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength); protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad); @Override Object encode(final Object obj); String encodeToString(final byte[] pArray); String encodeAsString(final byte[] pArray); @Override Object decode(final Object obj); byte[] decode(final String pArray); @Override byte[] decode(final byte[] pArray); @Override byte[] encode(final byte[] pArray); boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad); boolean isInAlphabet(final String basen); long getEncodedLength(final byte[] pArray); static final int MIME_CHUNK_SIZE; static final int PEM_CHUNK_SIZE; }### Answer: @Test public void testBaseNCodec() { assertNotNull(codec); }
### Question: BaseNCodec implements BinaryEncoder, BinaryDecoder { protected static boolean isWhiteSpace(final byte byteToCheck) { switch (byteToCheck) { case ' ' : case '\n' : case '\r' : case '\t' : return true; default : return false; } } protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength); protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad); @Override Object encode(final Object obj); String encodeToString(final byte[] pArray); String encodeAsString(final byte[] pArray); @Override Object decode(final Object obj); byte[] decode(final String pArray); @Override byte[] decode(final byte[] pArray); @Override byte[] encode(final byte[] pArray); boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad); boolean isInAlphabet(final String basen); long getEncodedLength(final byte[] pArray); static final int MIME_CHUNK_SIZE; static final int PEM_CHUNK_SIZE; }### Answer: @Test public void testIsWhiteSpace() { assertTrue(BaseNCodec.isWhiteSpace((byte) ' ')); assertTrue(BaseNCodec.isWhiteSpace((byte) '\n')); assertTrue(BaseNCodec.isWhiteSpace((byte) '\r')); assertTrue(BaseNCodec.isWhiteSpace((byte) '\t')); }
### Question: BaseNCodec implements BinaryEncoder, BinaryDecoder { protected abstract boolean isInAlphabet(byte value); protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength); protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad); @Override Object encode(final Object obj); String encodeToString(final byte[] pArray); String encodeAsString(final byte[] pArray); @Override Object decode(final Object obj); byte[] decode(final String pArray); @Override byte[] decode(final byte[] pArray); @Override byte[] encode(final byte[] pArray); boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad); boolean isInAlphabet(final String basen); long getEncodedLength(final byte[] pArray); static final int MIME_CHUNK_SIZE; static final int PEM_CHUNK_SIZE; }### Answer: @Test public void testIsInAlphabetByte() { assertFalse(codec.isInAlphabet((byte) 0)); assertFalse(codec.isInAlphabet((byte) 'a')); assertTrue(codec.isInAlphabet((byte) 'O')); assertTrue(codec.isInAlphabet((byte) 'K')); } @Test public void testIsInAlphabetByteArrayBoolean() { assertTrue(codec.isInAlphabet(new byte[]{}, false)); assertTrue(codec.isInAlphabet(new byte[]{'O'}, false)); assertFalse(codec.isInAlphabet(new byte[]{'O',' '}, false)); assertFalse(codec.isInAlphabet(new byte[]{' '}, false)); assertTrue(codec.isInAlphabet(new byte[]{}, true)); assertTrue(codec.isInAlphabet(new byte[]{'O'}, true)); assertTrue(codec.isInAlphabet(new byte[]{'O',' '}, true)); assertTrue(codec.isInAlphabet(new byte[]{' '}, true)); } @Test public void testIsInAlphabetString() { assertTrue(codec.isInAlphabet("OK")); assertTrue(codec.isInAlphabet("O=K= \t\n\r")); }
### Question: BaseNCodec implements BinaryEncoder, BinaryDecoder { protected boolean containsAlphabetOrPad(final byte[] arrayOctet) { if (arrayOctet == null) { return false; } for (final byte element : arrayOctet) { if (pad == element || isInAlphabet(element)) { return true; } } return false; } protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength); protected BaseNCodec(final int unencodedBlockSize, final int encodedBlockSize, final int lineLength, final int chunkSeparatorLength, final byte pad); @Override Object encode(final Object obj); String encodeToString(final byte[] pArray); String encodeAsString(final byte[] pArray); @Override Object decode(final Object obj); byte[] decode(final String pArray); @Override byte[] decode(final byte[] pArray); @Override byte[] encode(final byte[] pArray); boolean isInAlphabet(final byte[] arrayOctet, final boolean allowWSPad); boolean isInAlphabet(final String basen); long getEncodedLength(final byte[] pArray); static final int MIME_CHUNK_SIZE; static final int PEM_CHUNK_SIZE; }### Answer: @Test public void testContainsAlphabetOrPad() { assertFalse(codec.containsAlphabetOrPad(null)); assertFalse(codec.containsAlphabetOrPad(new byte[]{})); assertTrue(codec.containsAlphabetOrPad("OK".getBytes())); assertTrue(codec.containsAlphabetOrPad("OK ".getBytes())); assertFalse(codec.containsAlphabetOrPad("ok ".getBytes())); assertTrue(codec.containsAlphabetOrPad(new byte[]{codec.pad})); }
### Question: Base64 extends BaseNCodec { public static boolean isBase64(final byte octet) { return octet == PAD_DEFAULT || (octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1); } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testIsStringBase64() { final String nullString = null; final String emptyString = ""; final String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL=============="; final String invalidString = validString + (char)0; try { Base64.isBase64(nullString); fail("Base64.isStringBase64() should not be null-safe."); } catch (final NullPointerException npe) { assertNotNull("Base64.isStringBase64() should not be null-safe.", npe); } assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isBase64(emptyString)); assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isBase64(validString)); assertFalse("Base64.isStringBase64(invalid-string) is false", Base64.isBase64(invalidString)); }
### Question: EqualsOp extends Param1Op { @Override public String keyword() { return "Equals"; } @Override String keyword(); @Override String operator(); }### Answer: @Test public void test() throws Exception { Op op = new EqualsOp(); assertThat(op.keyword(), equalTo("Equals")); assertThat(op.paramCount(), equalTo(1)); assertThat(op.render("id", new String[] {":1"}), equalTo("id = :1")); }
### Question: Base64 extends BaseNCodec { public Base64() { this(0); } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testBase64() { final String content = "Hello World"; String encodedContent; byte[] encodedBytes = Base64.encodeBase64(StringUtils.getBytesUtf8(content)); encodedContent = StringUtils.newStringUtf8(encodedBytes); assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent); Base64 b64 = new Base64(BaseNCodec.MIME_CHUNK_SIZE, null); encodedBytes = b64.encode(StringUtils.getBytesUtf8(content)); encodedContent = StringUtils.newStringUtf8(encodedBytes); assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent); b64 = new Base64(0, null); encodedBytes = b64.encode(StringUtils.getBytesUtf8(content)); encodedContent = StringUtils.newStringUtf8(encodedBytes); assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent); final byte[] decode = b64.decode("SGVsbG{\u00e9\u00e9\u00e9\u00e9\u00e9\u00e9}8gV29ybGQ="); final String decodeString = StringUtils.newStringUtf8(decode); assertEquals("decode hello world", "Hello World", decodeString); }
### Question: Base64 extends BaseNCodec { public static byte[] encodeInteger(final BigInteger bigInt) { if (bigInt == null) { throw new NullPointerException("encodeInteger called with null parameter"); } return encodeBase64(toIntegerBytes(bigInt), false); } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testCodeIntegerNull() { try { Base64.encodeInteger(null); fail("Exception not thrown when passing in null to encodeInteger(BigInteger)"); } catch (final NullPointerException npe) { } catch (final Exception e) { fail("Incorrect Exception caught when passing in null to encodeInteger(BigInteger)"); } }
### Question: CustomQueryBuilder extends AbstractCustomBuilder { @Override public String buildSql() { String s1 = Joiner.on(", ").join(columns); return String.format(SQL_TEMPLATE, s1, tailOfSql); } CustomQueryBuilder(List<String> columns, String tailOfSql); @Override String buildSql(); }### Answer: @Test public void buildSql() throws Exception { List<String> columns = Lists.newArrayList("id2", "user_name", "user_age"); CustomQueryBuilder b = new CustomQueryBuilder(columns, "where id = :1"); assertThat(b.buildSql(), equalTo("select id2, user_name, user_age from #table where id = :1")); }
### Question: Base64 extends BaseNCodec { @Deprecated public static boolean isArrayByteBase64(final byte[] arrayOctet) { return isBase64(arrayOctet); } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testIsArrayByteBase64() { assertFalse(Base64.isBase64(new byte[]{Byte.MIN_VALUE})); assertFalse(Base64.isBase64(new byte[]{-125})); assertFalse(Base64.isBase64(new byte[]{-10})); assertFalse(Base64.isBase64(new byte[]{0})); assertFalse(Base64.isBase64(new byte[]{64, Byte.MAX_VALUE})); assertFalse(Base64.isBase64(new byte[]{Byte.MAX_VALUE})); assertTrue(Base64.isBase64(new byte[]{'A'})); assertFalse(Base64.isBase64(new byte[]{'A', Byte.MIN_VALUE})); assertTrue(Base64.isBase64(new byte[]{'A', 'Z', 'a'})); assertTrue(Base64.isBase64(new byte[]{'/', '=', '+'})); assertFalse(Base64.isBase64(new byte[]{'$'})); }
### Question: Base64 extends BaseNCodec { public boolean isUrlSafe() { return this.encodeTable == URL_SAFE_ENCODE_TABLE; } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testIsUrlSafe() { final Base64 base64Standard = new Base64(false); final Base64 base64URLSafe = new Base64(true); assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe()); assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe()); final byte[] whiteSpace = {' ', '\n', '\r', '\t'}; assertTrue("Base64.isBase64(whiteSpace)=true", Base64.isBase64(whiteSpace)); }
### Question: Base64 extends BaseNCodec { @Override void decode(final byte[] in, int inPos, final int inAvail, final Context context) { if (context.eof) { return; } if (inAvail < 0) { context.eof = true; } for (int i = 0; i < inAvail; i++) { final byte[] buffer = ensureBufferSize(decodeSize, context); final byte b = in[inPos++]; if (b == pad) { context.eof = true; break; } else { if (b >= 0 && b < DECODE_TABLE.length) { final int result = DECODE_TABLE[b]; if (result >= 0) { context.modulus = (context.modulus+1) % BYTES_PER_ENCODED_BLOCK; context.ibitWorkArea = (context.ibitWorkArea << BITS_PER_ENCODED_BYTE) + result; if (context.modulus == 0) { buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 16) & MASK_8BITS); buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 8) & MASK_8BITS); buffer[context.pos++] = (byte) (context.ibitWorkArea & MASK_8BITS); } } } } } if (context.eof && context.modulus != 0) { final byte[] buffer = ensureBufferSize(decodeSize, context); switch (context.modulus) { case 1 : break; case 2 : context.ibitWorkArea = context.ibitWorkArea >> 4; buffer[context.pos++] = (byte) ((context.ibitWorkArea) & MASK_8BITS); break; case 3 : context.ibitWorkArea = context.ibitWorkArea >> 2; buffer[context.pos++] = (byte) ((context.ibitWorkArea >> 8) & MASK_8BITS); buffer[context.pos++] = (byte) ((context.ibitWorkArea) & MASK_8BITS); break; default: throw new IllegalStateException("Impossible modulus "+context.modulus); } } } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testObjectDecodeWithInvalidParameter() throws Exception { final Base64 b64 = new Base64(); try { b64.decode(Integer.valueOf(5)); fail("decode(Object) didn't throw an exception when passed an Integer object"); } catch (final DecoderException e) { } }
### Question: CustomCountBuilder extends AbstractCustomBuilder { @Override public String buildSql() { return String.format(SQL_TEMPLATE, tailOfSql); } CustomCountBuilder(String tailOfSql); @Override String buildSql(); }### Answer: @Test public void buildSql() throws Exception { CustomCountBuilder b = new CustomCountBuilder("where id = :1"); assertThat(b.buildSql(), equalTo("select count(1) from #table where id = :1")); }
### Question: Base64 extends BaseNCodec { public static String encodeBase64String(final byte[] binaryData) { return StringUtils.newStringUtf8(encodeBase64(binaryData, false)); } Base64(); Base64(final boolean urlSafe); Base64(final int lineLength); Base64(final int lineLength, final byte[] lineSeparator); Base64(final int lineLength, final byte[] lineSeparator, final boolean urlSafe); boolean isUrlSafe(); @Deprecated static boolean isArrayByteBase64(final byte[] arrayOctet); static boolean isBase64(final byte octet); static boolean isBase64(final String base64); static boolean isBase64(final byte[] arrayOctet); static byte[] encodeBase64(final byte[] binaryData); static String encodeBase64String(final byte[] binaryData); static byte[] encodeBase64URLSafe(final byte[] binaryData); static String encodeBase64URLSafeString(final byte[] binaryData); static byte[] encodeBase64Chunked(final byte[] binaryData); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe); static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize); static byte[] decodeBase64(final String base64String); static byte[] decodeBase64(final byte[] base64Data); static BigInteger decodeInteger(final byte[] pArray); static byte[] encodeInteger(final BigInteger bigInt); }### Answer: @Test public void testRfc4648Section10Encode() { assertEquals("", Base64.encodeBase64String(StringUtils.getBytesUtf8(""))); assertEquals("Zg==", Base64.encodeBase64String(StringUtils.getBytesUtf8("f"))); assertEquals("Zm8=", Base64.encodeBase64String(StringUtils.getBytesUtf8("fo"))); assertEquals("Zm9v", Base64.encodeBase64String(StringUtils.getBytesUtf8("foo"))); assertEquals("Zm9vYg==", Base64.encodeBase64String(StringUtils.getBytesUtf8("foob"))); assertEquals("Zm9vYmE=", Base64.encodeBase64String(StringUtils.getBytesUtf8("fooba"))); assertEquals("Zm9vYmFy", Base64.encodeBase64String(StringUtils.getBytesUtf8("foobar"))); }
### Question: CustomDeleteBuilder extends AbstractCustomBuilder { @Override public String buildSql() { return String.format(SQL_TEMPLATE, tailOfSql); } CustomDeleteBuilder(String tailOfSql); @Override String buildSql(); }### Answer: @Test public void buildSql() throws Exception { CustomDeleteBuilder b = new CustomDeleteBuilder("where id = :1"); assertThat(b.buildSql(), equalTo("delete from #table where id = :1")); }
### Question: Hex implements BinaryEncoder, BinaryDecoder { @Override public String toString() { return super.toString() + "[charsetName=" + this.charset + "]"; } Hex(); Hex(final Charset charset); Hex(final String charsetName); static byte[] decodeHex(final char[] data); static char[] encodeHex(final byte[] data); static char[] encodeHex(final byte[] data, final boolean toLowerCase); static String encodeHexString(final byte[] data); @Override byte[] decode(final byte[] array); @Override Object decode(final Object object); @Override byte[] encode(final byte[] array); @Override Object encode(final Object object); Charset getCharset(); String getCharsetName(); @Override String toString(); static final Charset DEFAULT_CHARSET; static final String DEFAULT_CHARSET_NAME; }### Answer: @Test public void testCustomCharsetToString() { assertTrue(new Hex().toString().indexOf(Hex.DEFAULT_CHARSET_NAME) >= 0); }
### Question: Hex implements BinaryEncoder, BinaryDecoder { @Override public byte[] decode(final byte[] array) throws DecoderException { return decodeHex(new String(array, getCharset()).toCharArray()); } Hex(); Hex(final Charset charset); Hex(final String charsetName); static byte[] decodeHex(final char[] data); static char[] encodeHex(final byte[] data); static char[] encodeHex(final byte[] data, final boolean toLowerCase); static String encodeHexString(final byte[] data); @Override byte[] decode(final byte[] array); @Override Object decode(final Object object); @Override byte[] encode(final byte[] array); @Override Object encode(final Object object); Charset getCharset(); String getCharsetName(); @Override String toString(); static final Charset DEFAULT_CHARSET; static final String DEFAULT_CHARSET_NAME; }### Answer: @Test public void testDecodeArrayOddCharacters() { try { new Hex().decode(new byte[]{65}); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { } } @Test public void testDecodeBadCharacterPos0() { try { new Hex().decode("q0"); fail("An exception wasn't thrown when trying to decode an illegal character"); } catch (final DecoderException e) { } } @Test public void testDecodeBadCharacterPos1() { try { new Hex().decode("0q"); fail("An exception wasn't thrown when trying to decode an illegal character"); } catch (final DecoderException e) { } } @Test public void testDecodeClassCastException() { try { new Hex().decode(new int[]{65}); fail("An exception wasn't thrown when trying to decode."); } catch (final DecoderException e) { } } @Test public void testDecodeStringOddCharacters() { try { new Hex().decode("6"); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { } }
### Question: CommonGetBuilder extends AbstractCommonBuilder { @Override public String buildSql() { String s1 = Joiner.on(", ").join(columns); return isBatch ? String.format(BATCH_SQL_TEMPLATE, s1, columnId) : String.format(SQL_TEMPLATE, s1, columnId); } CommonGetBuilder(String colId, List<String> cols, boolean isBatch); @Override String buildSql(); }### Answer: @Test public void build() throws Exception { List<String> columns = Lists.newArrayList("id2", "user_name", "user_age"); CommonGetBuilder b = new CommonGetBuilder("id2", columns, false); assertThat(b.buildSql(), equalTo("select id2, user_name, user_age from #table where id2 = :1")); b = new CommonGetBuilder("id2", columns, true); assertThat(b.buildSql(), equalTo("select id2, user_name, user_age from #table where id2 in (:1)")); }
### Question: Hex implements BinaryEncoder, BinaryDecoder { @Override public byte[] encode(final byte[] array) { return encodeHexString(array).getBytes(this.getCharset()); } Hex(); Hex(final Charset charset); Hex(final String charsetName); static byte[] decodeHex(final char[] data); static char[] encodeHex(final byte[] data); static char[] encodeHex(final byte[] data, final boolean toLowerCase); static String encodeHexString(final byte[] data); @Override byte[] decode(final byte[] array); @Override Object decode(final Object object); @Override byte[] encode(final byte[] array); @Override Object encode(final Object object); Charset getCharset(); String getCharsetName(); @Override String toString(); static final Charset DEFAULT_CHARSET; static final String DEFAULT_CHARSET_NAME; }### Answer: @Test public void testEncodeClassCastException() { try { new Hex().encode(new int[]{65}); fail("An exception wasn't thrown when trying to encode."); } catch (final EncoderException e) { } }
### Question: Hex implements BinaryEncoder, BinaryDecoder { public static char[] encodeHex(final byte[] data) { return encodeHex(data, true); } Hex(); Hex(final Charset charset); Hex(final String charsetName); static byte[] decodeHex(final char[] data); static char[] encodeHex(final byte[] data); static char[] encodeHex(final byte[] data, final boolean toLowerCase); static String encodeHexString(final byte[] data); @Override byte[] decode(final byte[] array); @Override Object decode(final Object object); @Override byte[] encode(final byte[] array); @Override Object encode(final Object object); Charset getCharset(); String getCharsetName(); @Override String toString(); static final Charset DEFAULT_CHARSET; static final String DEFAULT_CHARSET_NAME; }### Answer: @Test public void testEncodeZeroes() { final char[] c = Hex.encodeHex(new byte[36]); assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", new String(c)); } @Test public void testHelloWorldLowerCaseHex() { final byte[] b = StringUtils.getBytesUtf8("Hello World"); final String expected = "48656c6c6f20576f726c64"; char[] actual; actual = Hex.encodeHex(b); assertEquals(expected, new String(actual)); actual = Hex.encodeHex(b, true); assertEquals(expected, new String(actual)); actual = Hex.encodeHex(b, false); assertFalse(expected.equals(new String(actual))); } @Test public void testHelloWorldUpperCaseHex() { final byte[] b = StringUtils.getBytesUtf8("Hello World"); final String expected = "48656C6C6F20576F726C64"; char[] actual; actual = Hex.encodeHex(b); assertFalse(expected.equals(new String(actual))); actual = Hex.encodeHex(b, true); assertFalse(expected.equals(new String(actual))); actual = Hex.encodeHex(b, false); assertTrue(expected.equals(new String(actual))); }
### Question: QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { @Override public byte[] decode(final byte[] bytes) throws DecoderException { return decodeQuotedPrintable(bytes); } QuotedPrintableCodec(); QuotedPrintableCodec(final boolean strict); QuotedPrintableCodec(final Charset charset); QuotedPrintableCodec(final Charset charset, final boolean strict); QuotedPrintableCodec(final String charsetName); static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] bytes); static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] bytes, boolean strict); static final byte[] decodeQuotedPrintable(final byte[] bytes); @Override byte[] encode(final byte[] bytes); @Override byte[] decode(final byte[] bytes); @Override String encode(final String str); String decode(final String str, final Charset charset); String decode(final String str, final String charset); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); Charset getCharset(); String getDefaultCharset(); String encode(final String str, final Charset charset); String encode(final String str, final String charset); }### Answer: @Test public void testDecodeInvalid() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); try { qpcodec.decode("="); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } try { qpcodec.decode("=A"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } try { qpcodec.decode("=WW"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } } @Test public void testDecodeStringWithNull() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String test = null; final String result = qpcodec.decode( test, "charset" ); assertEquals("Result should be null", null, result); } @Test public void testDecodeObjects() throws Exception { final QuotedPrintableCodec qpcodec = new QuotedPrintableCodec(); final String plain = "1+1 =3D 2"; String decoded = (String) qpcodec.decode((Object) plain); assertEquals("Basic quoted-printable decoding test", "1+1 = 2", decoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] decodedBA = (byte[]) qpcodec.decode((Object) plainBA); decoded = new String(decodedBA); assertEquals("Basic quoted-printable decoding test", "1+1 = 2", decoded); final Object result = qpcodec.decode((Object) null); assertEquals( "Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qpcodec.decode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final DecoderException ee) { } }
### Question: CommonDeleteBuilder extends AbstractCommonBuilder { @Override public String buildSql() { return String.format(SQL_TEMPLATE, columnId); } CommonDeleteBuilder(String colId); @Override String buildSql(); }### Answer: @Test public void build() throws Exception { CommonDeleteBuilder b = new CommonDeleteBuilder("id2"); assertThat(b.buildSql(), equalTo("delete from #table where id2 = :1")); }
### Question: QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { public static final byte[] decodeQuotedPrintable(final byte[] bytes) throws DecoderException { if (bytes == null) { return null; } final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int i = 0; i < bytes.length; i++) { final int b = bytes[i]; if (b == ESCAPE_CHAR) { try { if (bytes[++i] == CR) { continue; } final int u = Utils.digit16(bytes[i]); final int l = Utils.digit16(bytes[++i]); buffer.write((char) ((u << 4) + l)); } catch (final ArrayIndexOutOfBoundsException e) { throw new DecoderException("Invalid quoted-printable encoding", e); } } else if (b != CR && b != LF) { buffer.write(b); } } return buffer.toByteArray(); } QuotedPrintableCodec(); QuotedPrintableCodec(final boolean strict); QuotedPrintableCodec(final Charset charset); QuotedPrintableCodec(final Charset charset, final boolean strict); QuotedPrintableCodec(final String charsetName); static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] bytes); static final byte[] encodeQuotedPrintable(BitSet printable, final byte[] bytes, boolean strict); static final byte[] decodeQuotedPrintable(final byte[] bytes); @Override byte[] encode(final byte[] bytes); @Override byte[] decode(final byte[] bytes); @Override String encode(final String str); String decode(final String str, final Charset charset); String decode(final String str, final String charset); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); Charset getCharset(); String getDefaultCharset(); String encode(final String str, final Charset charset); String encode(final String str, final String charset); }### Answer: @Test public void testDecodeWithNullArray() throws Exception { final byte[] plain = null; final byte[] result = QuotedPrintableCodec.decodeQuotedPrintable( plain ); assertEquals("Result should be null", null, result); }
### Question: CommonUpdateBuilder extends AbstractCommonBuilder { @Override public String buildSql() { List<String> exps = new ArrayList<String>(); for (int i = 0; i < properties.size(); i++) { String exp = columns.get(i) + " = :" + properties.get(i); exps.add(exp); } String s1 = Joiner.on(", ").join(exps); String s2 = columnId + " = :" + propertyId; return String.format(SQL_TEMPLATE, s1, s2); } CommonUpdateBuilder(String propId, List<String> props, List<String> cols); @Override String buildSql(); }### Answer: @Test public void build() throws Exception { List<String> properties = Lists.newArrayList("id", "userName", "userAge"); List<String> columns = Lists.newArrayList("id", "user_name", "user_age"); CommonUpdateBuilder b = new CommonUpdateBuilder("id", properties, columns); assertThat(b.buildSql(), equalTo("update #table set user_name = :userName, user_age = :userAge where id = :id")); }
### Question: URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { @Override public byte[] decode(final byte[] bytes) throws DecoderException { return decodeUrl(bytes); } URLCodec(); URLCodec(final String charset); static final byte[] encodeUrl(BitSet urlsafe, final byte[] bytes); static final byte[] decodeUrl(final byte[] bytes); @Override byte[] encode(final byte[] bytes); @Override byte[] decode(final byte[] bytes); String encode(final String str, final String charset); @Override String encode(final String str); String decode(final String str, final String charset); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); String getDefaultCharset(); @Deprecated String getEncoding(); }### Answer: @Test public void testDecodeInvalid() throws Exception { final URLCodec urlCodec = new URLCodec(); try { urlCodec.decode("%"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } try { urlCodec.decode("%A"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } try { urlCodec.decode("%WW"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } try { urlCodec.decode("%0W"); fail("DecoderException should have been thrown"); } catch (final DecoderException e) { } this.validateState(urlCodec); } @Test public void testDecodeInvalidContent() throws UnsupportedEncodingException, DecoderException { final String ch_msg = constructString(SWISS_GERMAN_STUFF_UNICODE); final URLCodec urlCodec = new URLCodec(); final byte[] input = ch_msg.getBytes("ISO-8859-1"); final byte[] output = urlCodec.decode(input); assertEquals(input.length, output.length); for (int i = 0; i < input.length; i++) { assertEquals(input[i], output[i]); } this.validateState(urlCodec); } @Test public void testDecodeStringWithNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final String test = null; final String result = urlCodec.decode( test, "charset" ); assertEquals("Result should be null", null, result); } @Test public void testDecodeObjects() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello+there%21"; String decoded = (String) urlCodec.decode((Object) plain); assertEquals("Basic URL decoding test", "Hello there!", decoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] decodedBA = (byte[]) urlCodec.decode((Object) plainBA); decoded = new String(decodedBA); assertEquals("Basic URL decoding test", "Hello there!", decoded); final Object result = urlCodec.decode((Object) null); assertEquals( "Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); urlCodec.decode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final DecoderException ee) { } this.validateState(urlCodec); }
### Question: URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { @Override public byte[] encode(final byte[] bytes) { return encodeUrl(WWW_FORM_URL, bytes); } URLCodec(); URLCodec(final String charset); static final byte[] encodeUrl(BitSet urlsafe, final byte[] bytes); static final byte[] decodeUrl(final byte[] bytes); @Override byte[] encode(final byte[] bytes); @Override byte[] decode(final byte[] bytes); String encode(final String str, final String charset); @Override String encode(final String str); String decode(final String str, final String charset); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); String getDefaultCharset(); @Deprecated String getEncoding(); }### Answer: @Test public void testEncodeNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final byte[] plain = null; final byte[] encoded = urlCodec.encode(plain); assertEquals("Encoding a null string should return null", null, encoded); this.validateState(urlCodec); } @Test public void testEncodeStringWithNull() throws Exception { final URLCodec urlCodec = new URLCodec(); final String test = null; final String result = urlCodec.encode( test, "charset" ); assertEquals("Result should be null", null, result); } @Test public void testEncodeObjects() throws Exception { final URLCodec urlCodec = new URLCodec(); final String plain = "Hello there!"; String encoded = (String) urlCodec.encode((Object) plain); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); final byte[] plainBA = plain.getBytes(Charsets.UTF_8); final byte[] encodedBA = (byte[]) urlCodec.encode((Object) plainBA); encoded = new String(encodedBA); assertEquals("Basic URL encoding test", "Hello+there%21", encoded); final Object result = urlCodec.encode((Object) null); assertEquals( "Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); urlCodec.encode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final EncoderException ee) { } this.validateState(urlCodec); } @Test public void testDefaultEncoding() throws Exception { final String plain = "Hello there!"; final URLCodec urlCodec = new URLCodec("UnicodeBig"); urlCodec.encode(plain); final String encoded1 = urlCodec.encode(plain, "UnicodeBig"); final String encoded2 = urlCodec.encode(plain); assertEquals(encoded1, encoded2); this.validateState(urlCodec); }
### Question: URLCodec implements BinaryEncoder, BinaryDecoder, StringEncoder, StringDecoder { public static final byte[] decodeUrl(final byte[] bytes) throws DecoderException { if (bytes == null) { return null; } final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); for (int i = 0; i < bytes.length; i++) { final int b = bytes[i]; if (b == '+') { buffer.write(' '); } else if (b == ESCAPE_CHAR) { try { final int u = Utils.digit16(bytes[++i]); final int l = Utils.digit16(bytes[++i]); buffer.write((char) ((u << 4) + l)); } catch (final ArrayIndexOutOfBoundsException e) { throw new DecoderException("Invalid URL encoding: ", e); } } else { buffer.write(b); } } return buffer.toByteArray(); } URLCodec(); URLCodec(final String charset); static final byte[] encodeUrl(BitSet urlsafe, final byte[] bytes); static final byte[] decodeUrl(final byte[] bytes); @Override byte[] encode(final byte[] bytes); @Override byte[] decode(final byte[] bytes); String encode(final String str, final String charset); @Override String encode(final String str); String decode(final String str, final String charset); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); String getDefaultCharset(); @Deprecated String getEncoding(); }### Answer: @Test public void testDecodeWithNullArray() throws Exception { final byte[] plain = null; final byte[] result = URLCodec.decodeUrl( plain ); assertEquals("Result should be null", null, result); }
### Question: QCodec extends RFC1522Codec implements StringEncoder, StringDecoder { public String encode(final String str, final Charset charset) throws EncoderException { if (str == null) { return null; } return encodeText(str, charset); } QCodec(); QCodec(final Charset charset); QCodec(final String charsetName); String encode(final String str, final Charset charset); String encode(final String str, final String charset); @Override String encode(final String str); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); Charset getCharset(); String getDefaultCharset(); boolean isEncodeBlanks(); void setEncodeBlanks(final boolean b); }### Answer: @Test public void testEncodeStringWithNull() throws Exception { final QCodec qcodec = new QCodec(); final String test = null; final String result = qcodec.encode( test, "charset" ); assertEquals("Result should be null", null, result); } @Test public void testEncodeObjects() throws Exception { final QCodec qcodec = new QCodec(); final String plain = "1+1 = 2"; final String encoded = (String) qcodec.encode((Object) plain); assertEquals("Basic Q encoding test", "=?UTF-8?Q?1+1 =3D 2?=", encoded); final Object result = qcodec.encode((Object) null); assertEquals( "Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qcodec.encode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final EncoderException ee) { } }
### Question: CommonAddBuilder extends AbstractCommonBuilder { @Override public String buildSql() { String s1 = Joiner.on(", ").join(columns); List<String> cps = new ArrayList<String>(); for (String prop : properties) { cps.add(":" + prop); } String s2 = Joiner.on(", ").join(cps); return String.format(SQL_TEMPLATE, s1, s2); } CommonAddBuilder(String propId, List<String> props, List<String> cols, boolean isAutoGenerateId); @Override String buildSql(); }### Answer: @Test public void build() throws Exception { List<String> properties = Lists.newArrayList("id", "name", "age"); List<String> columns = Lists.newArrayList("id2", "name2", "age2"); CommonAddBuilder b = new CommonAddBuilder("id", properties, columns, true); assertThat(b.buildSql(), equalTo("insert into #table(name2, age2) values(:name, :age)")); properties = Lists.newArrayList("id", "name", "age"); columns = Lists.newArrayList("id2", "name2", "age2"); b = new CommonAddBuilder("id", properties, columns, false); assertThat(b.buildSql(), equalTo("insert into #table(id2, name2, age2) values(:id, :name, :age)")); }
### Question: Reflection { public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException { if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "specified class is an interface"); } try { return clazz.newInstance(); } catch (InstantiationException e) { throw new BeanInstantiationException(clazz, "Is it an abstract class?", e); } catch (IllegalAccessException e) { throw new BeanInstantiationException(clazz, "Is the constructor accessible?", e); } } static T instantiate(Class<T> clazz); static T instantiateClass(Class<T> clazz); static T instantiateClass(Constructor<T> ctor, Object... args); static T newProxy( Class<T> interfaceType, InvocationHandler handler); static void makeAccessible(Constructor<?> ctor); static Set<Annotation> getAnnotations(Class<?> clazz); }### Answer: @Test public void testInstantiate() throws Exception { Reflection.instantiateClass(A.class); }
### Question: QCodec extends RFC1522Codec implements StringEncoder, StringDecoder { @Override public String decode(final String str) throws DecoderException { if (str == null) { return null; } try { return decodeText(str); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } } QCodec(); QCodec(final Charset charset); QCodec(final String charsetName); String encode(final String str, final Charset charset); String encode(final String str, final String charset); @Override String encode(final String str); @Override String decode(final String str); @Override Object encode(final Object obj); @Override Object decode(final Object obj); Charset getCharset(); String getDefaultCharset(); boolean isEncodeBlanks(); void setEncodeBlanks(final boolean b); }### Answer: @Test public void testDecodeStringWithNull() throws Exception { final QCodec qcodec = new QCodec(); final String test = null; final String result = qcodec.decode( test ); assertEquals("Result should be null", null, result); } @Test public void testDecodeObjects() throws Exception { final QCodec qcodec = new QCodec(); final String decoded = "=?UTF-8?Q?1+1 =3D 2?="; final String plain = (String) qcodec.decode((Object) decoded); assertEquals("Basic Q decoding test", "1+1 = 2", plain); final Object result = qcodec.decode((Object) null); assertEquals( "Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); qcodec.decode( dObj ); fail( "Trying to url encode a Double object should cause an exception."); } catch (final DecoderException ee) { } }
### Question: BCodec extends RFC1522Codec implements StringEncoder, StringDecoder { public String encode(final String value, final Charset charset) throws EncoderException { if (value == null) { return null; } return encodeText(value, charset); } BCodec(); BCodec(final Charset charset); BCodec(final String charsetName); String encode(final String value, final Charset charset); String encode(final String value, final String charset); @Override String encode(final String value); @Override String decode(final String value); @Override Object encode(final Object value); @Override Object decode(final Object value); Charset getCharset(); String getDefaultCharset(); }### Answer: @Test public void testEncodeStringWithNull() throws Exception { final BCodec bcodec = new BCodec(); final String test = null; final String result = bcodec.encode(test, "charset"); assertEquals("Result should be null", null, result); } @Test public void testEncodeObjects() throws Exception { final BCodec bcodec = new BCodec(); final String plain = "what not"; final String encoded = (String) bcodec.encode((Object) plain); assertEquals("Basic B encoding test", "=?UTF-8?B?d2hhdCBub3Q=?=", encoded); final Object result = bcodec.encode((Object) null); assertEquals("Encoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); bcodec.encode(dObj); fail("Trying to url encode a Double object should cause an exception."); } catch (final EncoderException ee) { } }
### Question: BCodec extends RFC1522Codec implements StringEncoder, StringDecoder { @Override public String decode(final String value) throws DecoderException { if (value == null) { return null; } try { return this.decodeText(value); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } } BCodec(); BCodec(final Charset charset); BCodec(final String charsetName); String encode(final String value, final Charset charset); String encode(final String value, final String charset); @Override String encode(final String value); @Override String decode(final String value); @Override Object encode(final Object value); @Override Object decode(final Object value); Charset getCharset(); String getDefaultCharset(); }### Answer: @Test public void testDecodeStringWithNull() throws Exception { final BCodec bcodec = new BCodec(); final String test = null; final String result = bcodec.decode(test); assertEquals("Result should be null", null, result); } @Test public void testDecodeObjects() throws Exception { final BCodec bcodec = new BCodec(); final String decoded = "=?UTF-8?B?d2hhdCBub3Q=?="; final String plain = (String) bcodec.decode((Object) decoded); assertEquals("Basic B decoding test", "what not", plain); final Object result = bcodec.decode((Object) null); assertEquals("Decoding a null Object should return null", null, result); try { final Object dObj = new Double(3.0); bcodec.decode(dObj); fail("Trying to url encode a Double object should cause an exception."); } catch (final DecoderException ee) { } }
### Question: DaitchMokotoffSoundex implements StringEncoder { @Override public Object encode(final Object obj) throws EncoderException { if (!(obj instanceof String)) { throw new EncoderException( "Parameter supplied to DaitchMokotoffSoundex encode is not of type java.lang.String"); } return encode((String) obj); } DaitchMokotoffSoundex(); DaitchMokotoffSoundex(final boolean folding); @Override Object encode(final Object obj); @Override String encode(final String source); String soundex(final String source); }### Answer: @Test public void testEncodeIgnoreTrimmable() { Assert.assertEquals("746536", encode(" \t\n\r Washington \t\n\r ")); Assert.assertEquals("746536", encode("Washington")); }
### Question: CommonGetAllBuilder extends AbstractCommonBuilder { @Override public String buildSql() { String s1 = Joiner.on(", ").join(columns); return String.format(SQL, s1, s1); } CommonGetAllBuilder(List<String> cols); @Override String buildSql(); }### Answer: @Test public void build() throws Exception { List<String> columns = Lists.newArrayList("id2", "user_name", "user_age"); CommonGetAllBuilder b = new CommonGetAllBuilder(columns); assertThat(b.buildSql(), equalTo("select id2, user_name, user_age from #table")); }
### Question: ColognePhonetic implements StringEncoder { public boolean isEncodeEqual(final String text1, final String text2) { return colognePhonetic(text1).equals(colognePhonetic(text2)); } String colognePhonetic(String text); @Override Object encode(final Object object); @Override String encode(final String text); boolean isEncodeEqual(final String text1, final String text2); }### Answer: @Test public void testIsEncodeEquals() { final String[][] data = { {"Meyer", "M\u00fcller"}, {"Meyer", "Mayr"}, {"house", "house"}, {"House", "house"}, {"Haus", "house"}, {"ganz", "Gans"}, {"ganz", "G\u00e4nse"}, {"Miyagi", "Miyako"}}; for (final String[] element : data) { this.getStringEncoder().isEncodeEqual(element[1], element[0]); } }
### Question: RefinedSoundex implements StringEncoder { public int difference(final String s1, final String s2) throws EncoderException { return SoundexUtils.difference(this, s1, s2); } RefinedSoundex(); RefinedSoundex(final char[] mapping); RefinedSoundex(final String mapping); int difference(final String s1, final String s2); @Override Object encode(final Object obj); @Override String encode(final String str); String soundex(String str); static final String US_ENGLISH_MAPPING_STRING; static final RefinedSoundex US_ENGLISH; }### Answer: @Test public void testDifference() throws EncoderException { assertEquals(0, this.getStringEncoder().difference(null, null)); assertEquals(0, this.getStringEncoder().difference("", "")); assertEquals(0, this.getStringEncoder().difference(" ", " ")); assertEquals(6, this.getStringEncoder().difference("Smith", "Smythe")); assertEquals(3, this.getStringEncoder().difference("Ann", "Andrew")); assertEquals(1, this.getStringEncoder().difference("Margaret", "Andrew")); assertEquals(1, this.getStringEncoder().difference("Janet", "Margaret")); assertEquals(5, this.getStringEncoder().difference("Green", "Greene")); assertEquals(1, this.getStringEncoder().difference("Blotchet-Halls", "Greene")); assertEquals(6, this.getStringEncoder().difference("Smith", "Smythe")); assertEquals(8, this.getStringEncoder().difference("Smithers", "Smythers")); assertEquals(5, this.getStringEncoder().difference("Anothers", "Brothers")); }
### Question: RefinedSoundex implements StringEncoder { @Override public Object encode(final Object obj) throws EncoderException { if (!(obj instanceof String)) { throw new EncoderException("Parameter supplied to RefinedSoundex encode is not of type java.lang.String"); } return soundex((String) obj); } RefinedSoundex(); RefinedSoundex(final char[] mapping); RefinedSoundex(final String mapping); int difference(final String s1, final String s2); @Override Object encode(final Object obj); @Override String encode(final String str); String soundex(String str); static final String US_ENGLISH_MAPPING_STRING; static final RefinedSoundex US_ENGLISH; }### Answer: @Test public void testEncode() { assertEquals("T6036084", this.getStringEncoder().encode("testing")); assertEquals("T6036084", this.getStringEncoder().encode("TESTING")); assertEquals("T60", this.getStringEncoder().encode("The")); assertEquals("Q503", this.getStringEncoder().encode("quick")); assertEquals("B1908", this.getStringEncoder().encode("brown")); assertEquals("F205", this.getStringEncoder().encode("fox")); assertEquals("J408106", this.getStringEncoder().encode("jumped")); assertEquals("O0209", this.getStringEncoder().encode("over")); assertEquals("T60", this.getStringEncoder().encode("the")); assertEquals("L7050", this.getStringEncoder().encode("lazy")); assertEquals("D6043", this.getStringEncoder().encode("dogs")); assertEquals("D6043", RefinedSoundex.US_ENGLISH.encode("dogs")); }
### Question: RefinedSoundex implements StringEncoder { char getMappingCode(final char c) { if (!Character.isLetter(c)) { return 0; } return this.soundexMapping[Character.toUpperCase(c) - 'A']; } RefinedSoundex(); RefinedSoundex(final char[] mapping); RefinedSoundex(final String mapping); int difference(final String s1, final String s2); @Override Object encode(final Object obj); @Override String encode(final String str); String soundex(String str); static final String US_ENGLISH_MAPPING_STRING; static final RefinedSoundex US_ENGLISH; }### Answer: @Test public void testGetMappingCodeNonLetter() { final char code = this.getStringEncoder().getMappingCode('#'); assertEquals("Code does not equals zero", 0, code); }
### Question: RefinedSoundex implements StringEncoder { public String soundex(String str) { if (str == null) { return null; } str = SoundexUtils.clean(str); if (str.length() == 0) { return str; } final StringBuilder sBuf = new StringBuilder(); sBuf.append(str.charAt(0)); char last, current; last = '*'; for (int i = 0; i < str.length(); i++) { current = getMappingCode(str.charAt(i)); if (current == last) { continue; } else if (current != 0) { sBuf.append(current); } last = current; } return sBuf.toString(); } RefinedSoundex(); RefinedSoundex(final char[] mapping); RefinedSoundex(final String mapping); int difference(final String s1, final String s2); @Override Object encode(final Object obj); @Override String encode(final String str); String soundex(String str); static final String US_ENGLISH_MAPPING_STRING; static final RefinedSoundex US_ENGLISH; }### Answer: @Test public void testNewInstance() { assertEquals("D6043", new RefinedSoundex().soundex("dogs")); } @Test public void testNewInstance2() { assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING.toCharArray()).soundex("dogs")); } @Test public void testNewInstance3() { assertEquals("D6043", new RefinedSoundex(RefinedSoundex.US_ENGLISH_MAPPING_STRING).soundex("dogs")); }
### Question: PhoneticEngine { public String encode(final String input) { final Languages.LanguageSet languageSet = this.lang.guessLanguages(input); return encode(input, languageSet); } PhoneticEngine(final NameType nameType, final RuleType ruleType, final boolean concat); PhoneticEngine(final NameType nameType, final RuleType ruleType, final boolean concat, final int maxPhonemes); String encode(final String input); String encode(String input, final Languages.LanguageSet languageSet); Lang getLang(); NameType getNameType(); RuleType getRuleType(); boolean isConcat(); int getMaxPhonemes(); }### Answer: @Test(timeout = 10000L) public void testEncode() { final PhoneticEngine engine = new PhoneticEngine(this.nameType, this.ruleType, this.concat, this.maxPhonemes); final String phoneticActual = engine.encode(this.name); assertEquals("phoneme incorrect", this.phoneticExpected, phoneticActual); if (this.concat) { final String[] split = phoneticActual.split("\\|"); assertTrue(split.length <= this.maxPhonemes); } else { final String[] words = phoneticActual.split("-"); for (final String word : words) { final String[] split = word.split("\\|"); assertTrue(split.length <= this.maxPhonemes); } } }
### Question: BeiderMorseEncoder implements StringEncoder { @Override public Object encode(final Object source) throws EncoderException { if (!(source instanceof String)) { throw new EncoderException("BeiderMorseEncoder encode parameter is not of type String"); } return encode((String) source); } @Override Object encode(final Object source); @Override String encode(final String source); NameType getNameType(); RuleType getRuleType(); boolean isConcat(); void setConcat(final boolean concat); void setNameType(final NameType nameType); void setRuleType(final RuleType ruleType); void setMaxPhonemes(final int maxPhonemes); }### Answer: @Test public void testAllChars() throws EncoderException { final BeiderMorseEncoder bmpm = createGenericApproxEncoder(); for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) { bmpm.encode(Character.toString(c)); } } @Test public void testEncodeGna() throws EncoderException { final BeiderMorseEncoder bmpm = createGenericApproxEncoder(); bmpm.encode("gna"); } @Test(timeout = 10000L) public void testLongestEnglishSurname() throws EncoderException { final BeiderMorseEncoder bmpm = createGenericApproxEncoder(); bmpm.encode("MacGhilleseatheanaich"); } @Test() public void testSpeedCheck() throws EncoderException { final BeiderMorseEncoder bmpm = this.createGenericApproxEncoder(); final StringBuilder stringBuffer = new StringBuilder(); stringBuffer.append(TEST_CHARS[0]); for (int i = 0, j = 1; i < 40; i++, j++) { if (j == TEST_CHARS.length) { j = 0; } bmpm.encode(stringBuffer.toString()); stringBuffer.append(TEST_CHARS[j]); } } @Test public void testSpeedCheck2() throws EncoderException { final BeiderMorseEncoder bmpm = this.createGenericApproxEncoder(); final String phrase = "ItstheendoftheworldasweknowitandIfeelfine"; for (int i = 1; i <= phrase.length(); i++) { bmpm.encode(phrase.subSequence(0, i)); } } @Test public void testSpeedCheck3() throws EncoderException { final BeiderMorseEncoder bmpm = this.createGenericApproxEncoder(); final String phrase = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"; for (int i = 1; i <= phrase.length(); i++) { bmpm.encode(phrase.subSequence(0, i)); } }
### Question: BeiderMorseEncoder implements StringEncoder { public void setConcat(final boolean concat) { this.engine = new PhoneticEngine(this.engine.getNameType(), this.engine.getRuleType(), concat, this.engine.getMaxPhonemes()); } @Override Object encode(final Object source); @Override String encode(final String source); NameType getNameType(); RuleType getRuleType(); boolean isConcat(); void setConcat(final boolean concat); void setNameType(final NameType nameType); void setRuleType(final RuleType ruleType); void setMaxPhonemes(final int maxPhonemes); }### Answer: @Test public void testSetConcat() { final BeiderMorseEncoder bmpm = new BeiderMorseEncoder(); bmpm.setConcat(false); assertFalse("Should be able to set concat to false", bmpm.isConcat()); }
### Question: BeiderMorseEncoder implements StringEncoder { public void setRuleType(final RuleType ruleType) { this.engine = new PhoneticEngine(this.engine.getNameType(), ruleType, this.engine.isConcat(), this.engine.getMaxPhonemes()); } @Override Object encode(final Object source); @Override String encode(final String source); NameType getNameType(); RuleType getRuleType(); boolean isConcat(); void setConcat(final boolean concat); void setNameType(final NameType nameType); void setRuleType(final RuleType ruleType); void setMaxPhonemes(final int maxPhonemes); }### Answer: @Test(expected = IllegalArgumentException.class) public void testSetRuleTypeToRulesIllegalArgumentException() { final BeiderMorseEncoder bmpm = new BeiderMorseEncoder(); bmpm.setRuleType(RuleType.RULES); }