001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing.testers;
018
019import static com.google.common.collect.testing.features.CollectionSize.ZERO;
020
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.collect.testing.features.CollectionSize;
023import java.util.Arrays;
024
025/**
026 * A generic JUnit test which tests {@code toArray()} operations on a list. Can't be invoked
027 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
028 *
029 * @author Chris Povirk
030 */
031@GwtCompatible
032public class ListToArrayTester<E> extends AbstractListTester<E> {
033  // CollectionToArrayTester tests everything except ordering.
034
035  public void testToArray_noArg() {
036    Object[] actual = getList().toArray();
037    assertArrayEquals("toArray() order should match list", createOrderedArray(), actual);
038  }
039
040  @CollectionSize.Require(absent = ZERO)
041  public void testToArray_tooSmall() {
042    Object[] actual = getList().toArray(new Object[0]);
043    assertArrayEquals("toArray(tooSmall) order should match list", createOrderedArray(), actual);
044  }
045
046  public void testToArray_largeEnough() {
047    Object[] actual = getList().toArray(new Object[getNumElements()]);
048    assertArrayEquals("toArray(largeEnough) order should match list", createOrderedArray(), actual);
049  }
050
051  private static void assertArrayEquals(String message, Object[] expected, Object[] actual) {
052    assertEquals(message, Arrays.asList(expected), Arrays.asList(actual));
053  }
054}