PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/alexruiz-fest-assert-1.x-7a64c52/src/main/java/org/fest/assertions/ListAssert.java

#
Java | 230 lines | 103 code | 19 blank | 108 comment | 35 complexity | 990934f5b21bc14db77644f275ceb272 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Created on Mar 29, 2009
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. *
  13. * Copyright @2009-2011 the original author or authors.
  14. */
  15. package org.fest.assertions;
  16. import static java.util.Collections.emptyList;
  17. import static org.fest.assertions.Formatting.format;
  18. import static org.fest.util.Collections.list;
  19. import static org.fest.util.Objects.areEqual;
  20. import java.util.*;
  21. import org.fest.util.IntrospectionError;
  22. /**
  23. * Assertions for <code>{@link List}</code>s.
  24. * <p>
  25. * To create a new instance of this class invoke <code>{@link Assertions#assertThat(List)}</code>.
  26. * </p>
  27. *
  28. * @author Alex Ruiz
  29. * @author Yvonne Wang
  30. *
  31. * @since 1.1
  32. */
  33. public class ListAssert extends ObjectGroupAssert<ListAssert, List<?>> {
  34. /**
  35. * Creates a new </code>{@link ListAssert}</code>.
  36. * @param actual the target to verify.
  37. */
  38. protected ListAssert(List<?> actual) {
  39. super(ListAssert.class, actual);
  40. }
  41. /**
  42. * Verifies that the actual <code>{@link List}</code> contains the given object at the given index.
  43. * @param o the object to look for.
  44. * @param index the index where the object should be stored in the actual {@code List}.
  45. * @return this assertion object.
  46. * @throws NullPointerException if the given <code>Index</code> is {@code null}.
  47. * @throws IndexOutOfBoundsException if the value of the given <code>Index</code> is negative, or equal to or greater
  48. * than the size of the actual {@code List}.
  49. * @throws AssertionError if the given {@code List} does not contain the given object at the given index.
  50. */
  51. public ListAssert contains(Object o, Index index) {
  52. if (index == null) throw new NullPointerException(formattedErrorMessage("The given index should not be null"));
  53. isNotNull().isNotEmpty();
  54. int indexValue = index.value();
  55. int listSize = actualGroupSize();
  56. if (indexValue < 0 || indexValue >= listSize) failIndexOutOfBounds(indexValue);
  57. Object actualElement = actual.get(indexValue);
  58. if (!areEqual(actualElement, o)) failElementNotFound(o, actualElement, indexValue);
  59. return this;
  60. }
  61. private void failElementNotFound(Object e, Object a, int index) {
  62. failIfCustomMessageIsSet();
  63. fail(format("expecting <%s> at index <%s> but found <%s>", e, index, a));
  64. }
  65. private void failIndexOutOfBounds(int index) {
  66. throw new IndexOutOfBoundsException(formattedErrorMessage(
  67. format("The index <%s> should be greater than or equal to zero and less than %s", index, actualGroupSize())));
  68. }
  69. /**
  70. * Verifies that the actual <code>{@link List}</code> contains the given sequence of objects, without any other
  71. * objects between them.
  72. * @param sequence the sequence of objects to look for.
  73. * @return this assertion object.
  74. * @throws AssertionError if the actual {@code List} is {@code null}.
  75. * @throws AssertionError if the given array is {@code null}.
  76. * @throws AssertionError if the actual {@code List} does not contain the given sequence of objects.
  77. */
  78. public ListAssert containsSequence(Object... sequence) {
  79. isNotNull();
  80. validateIsNotNull(sequence);
  81. int sequenceSize = sequence.length;
  82. if (sequenceSize == 0) return this;
  83. int indexOfFirst = actual.indexOf(sequence[0]);
  84. if (indexOfFirst == -1) failIfSequenceNotFound(sequence);
  85. int listSize = actualGroupSize();
  86. for (int i = 0; i < sequenceSize; i++) {
  87. int actualIndex = indexOfFirst + i;
  88. if (actualIndex > listSize - 1) failIfSequenceNotFound(sequence);
  89. if (!areEqual(sequence[i], actual.get(actualIndex))) failIfSequenceNotFound(sequence);
  90. }
  91. return this;
  92. }
  93. private void failIfSequenceNotFound(Object[] notFound) {
  94. failIfCustomMessageIsSet();
  95. fail(format("list:<%s> does not contain the sequence:<%s>", actual, notFound));
  96. }
  97. /**
  98. * Verifies that the actual <code>{@link List}</code> starts with the given sequence of objects, without any other
  99. * objects between them. Same as <code>{@link #containsSequence}</code>, but verifies also that first given object is
  100. * also first element of {@code List}.
  101. * @param sequence the sequence of objects to look for.
  102. * @return this assertion object.
  103. * @throws AssertionError if the actual {@code List} is {@code null}.
  104. * @throws AssertionError if the given array is {@code null}.
  105. * @throws AssertionError if the actual {@code List} is not empty and with the given sequence of objects is
  106. * empty.
  107. * @throws AssertionError if the actual {@code List} does not start with the given sequence of objects.
  108. */
  109. public ListAssert startsWith(Object... sequence) {
  110. isNotNull();
  111. validateIsNotNull(sequence);
  112. int sequenceSize = sequence.length;
  113. int listSize = actualGroupSize();
  114. if (sequenceSize == 0 && listSize == 0) return this;
  115. if (sequenceSize == 0 && listSize != 0) failIfNotStartingWithSequence(sequence);
  116. if (listSize < sequenceSize) failIfNotStartingWithSequence(sequence);
  117. for (int i = 0; i < sequenceSize; i++)
  118. if (!areEqual(sequence[i], actual.get(i))) failIfNotStartingWithSequence(sequence);
  119. return this;
  120. }
  121. private void failIfNotStartingWithSequence(Object[] notFound) {
  122. failIfCustomMessageIsSet();
  123. fail(format("list:<%s> does not start with the sequence:<%s>", actual, notFound));
  124. }
  125. /**
  126. * Verifies that the actual <code>{@link List}</code> ends with the given sequence of objects, without any other
  127. * objects between them. Same as <code>{@link #containsSequence}</code>, but verifies also that last given object is
  128. * also last element of {@code List}.
  129. * @param sequence the sequence of objects to look for.
  130. * @return this assertion object.
  131. * @throws AssertionError if the actual {@code List} is {@code null}.
  132. * @throws AssertionError if the given array is {@code null}.
  133. * @throws AssertionError if the actual {@code List} is not empty and with the given sequence of objects is
  134. * empty.
  135. * @throws AssertionError if the actual {@code List} does not end with the given sequence of objects.
  136. */
  137. public ListAssert endsWith(Object... sequence) {
  138. isNotNull();
  139. validateIsNotNull(sequence);
  140. int sequenceSize = sequence.length;
  141. int listSize = actualGroupSize();
  142. if (sequenceSize == 0 && listSize == 0) return this;
  143. if (sequenceSize == 0 && listSize != 0) failIfNotEndingWithSequence(sequence);
  144. if (listSize < sequenceSize) failIfNotEndingWithSequence(sequence);
  145. for (int i = 0; i < sequenceSize; i++) {
  146. int sequenceIndex = sequenceSize - 1 - i;
  147. int listIndex = listSize - 1 - i;
  148. if (!areEqual(sequence[sequenceIndex], actual.get(listIndex))) failIfNotEndingWithSequence(sequence);
  149. }
  150. return this;
  151. }
  152. private void failIfNotEndingWithSequence(Object[] notFound) {
  153. failIfCustomMessageIsSet();
  154. fail(format("list:<%s> does not end with the sequence:<%s>", actual, notFound));
  155. }
  156. /**
  157. * Returns the number of elements in the actual <code>{@link List}</code>.
  158. * @return the number of elements in the actual {@code List}.
  159. * @throws AssertionError if the actual {@code List} is {@code null}.
  160. */
  161. @Override protected int actualGroupSize() {
  162. isNotNull();
  163. return actual.size();
  164. }
  165. /**
  166. * Verifies that the actual <code>{@link List}</code> contains the given objects, in the same order. This method works
  167. * just like <code>{@link #isEqualTo(List)}</code>, with the difference that internally the given array is converted
  168. * to a {@code List}.
  169. * @param objects the objects to look for.
  170. * @return this assertion object.
  171. * @throws AssertionError if the actual {@code List} is {@code null}.
  172. * @throws NullPointerException if the given array is {@code null}.
  173. * @throws AssertionError if the actual {@code List} does not contain the given objects.
  174. */
  175. public ListAssert containsExactly(Object... objects) {
  176. validateIsNotNull(objects);
  177. return isNotNull().isEqualTo(list(objects));
  178. }
  179. /**
  180. * Creates a new instance of <code>{@link ListAssert}</code> whose target list contains the values of the given
  181. * property name from the elements of this {@code ListAssert}'s list. Property access works with both simple
  182. * properties like {@code Person.age} and nested properties {@code Person.father.age}.
  183. * </p>
  184. * <p>
  185. * For example, let's say we have a list of {@code Person} objects and you want to verify their age:
  186. * <pre>
  187. * assertThat(persons).onProperty("age").containsOnly(25, 16, 44, 37); // simple property
  188. * assertThat(persons).onProperty("father.age").containsOnly(55, 46, 74, 62); // nested property
  189. * </p>
  190. * @param propertyName the name of the property to extract values from the actual list to build a new
  191. * {@code ListAssert}.
  192. * @return a new {@code ListAssert} containing the values of the given property name from the elements of this
  193. * {@code ListAssert}'s list.
  194. * @throws AssertionError if the actual list is {@code null}.
  195. * @throws IntrospectionError if an element in the given list does not have a matching property.
  196. * @since 1.3
  197. */
  198. @Override public ListAssert onProperty(String propertyName) {
  199. isNotNull();
  200. if (actual.isEmpty()) return new ListAssert(emptyList());
  201. return new ListAssert(PropertySupport.instance().propertyValues(propertyName, actual));
  202. }
  203. /** {@inheritDoc} */
  204. @Override protected Set<Object> actualAsSet() {
  205. return new LinkedHashSet<Object>(actual);
  206. }
  207. /** {@inheritDoc} */
  208. @Override protected List<Object> actualAsList() {
  209. return new ArrayList<Object>(actual);
  210. }
  211. }