001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.assertions; 014 015import static org.apache.juneau.internal.ObjectUtils.*; 016 017import java.util.*; 018 019import org.apache.juneau.internal.*; 020import org.apache.juneau.marshall.*; 021 022/** 023 * Used for fluent assertion calls against collections objects. 024 * 025 * @param <R> The return type. 026 */ 027@FluentSetters(returns="FluentCollectionAssertion<R>") 028@SuppressWarnings("rawtypes") 029public class FluentCollectionAssertion<R> extends FluentObjectAssertion<R> { 030 031 private Collection value; 032 033 /** 034 * Constructor. 035 * 036 * @param contents The byte array being tested. 037 * @param returns The object to return after the test. 038 */ 039 public FluentCollectionAssertion(Collection contents, R returns) { 040 this(null, contents, returns); 041 } 042 043 /** 044 * Constructor. 045 * 046 * @param creator The assertion that created this assertion. 047 * @param contents The byte array being tested. 048 * @param returns The object to return after the test. 049 */ 050 public FluentCollectionAssertion(Assertion creator, Collection contents, R returns) { 051 super(creator, contents, returns); 052 this.value = contents; 053 } 054 055 /** 056 * Asserts that the collection exists and is empty. 057 * 058 * @return The object to return after the test. 059 * @throws AssertionError If assertion failed. 060 */ 061 public R isEmpty() throws AssertionError { 062 exists(); 063 if (! value.isEmpty()) 064 throw error("Collection was not empty."); 065 return returns(); 066 } 067 068 /** 069 * Asserts that the collection contains the expected value. 070 * 071 * @param value The value to check for. 072 * @return The object to return after the test. 073 * @throws AssertionError If assertion failed. 074 */ 075 public R contains(Object value) throws AssertionError { 076 exists(); 077 for (Object o : this.value) 078 if (eq(o, value)) 079 return returns(); 080 throw error("Collection did not contain expected value.\nContents: {0}\nExpected:{1}", SimpleJson.DEFAULT.toString(this.value), value); 081 } 082 083 /** 084 * Asserts that the collection contains the expected value. 085 * 086 * @param value The value to check for. 087 * @return The object to return after the test. 088 * @throws AssertionError If assertion failed. 089 */ 090 public R doesNotContain(Object value) throws AssertionError { 091 exists(); 092 for (Object o : this.value) 093 if (eq(o, value)) 094 throw error("Collection contained unexpected value.\nContents: {0}\nUnexpected:{1}", SimpleJson.DEFAULT.toString(this.value), value); 095 return returns(); 096 } 097 098 /** 099 * Asserts that the collection exists and is not empty. 100 * 101 * @return The object to return after the test. 102 * @throws AssertionError If assertion failed. 103 */ 104 public R isNotEmpty() throws AssertionError { 105 exists(); 106 if (value.isEmpty()) 107 throw error("Collection was empty."); 108 return returns(); 109 } 110 111 /** 112 * Asserts that the collection exists and is the specified size. 113 * 114 * @param size The expected size. 115 * @return The object to return after the test. 116 * @throws AssertionError If assertion failed. 117 */ 118 public R isSize(int size) throws AssertionError { 119 exists(); 120 if (value.size() != size) 121 throw error("Collection did not have the expected size. Expected={0}, Actual={1}.", size, value.size()); 122 return returns(); 123 } 124 125 // <FluentSetters> 126 127 @Override /* GENERATED - Assertion */ 128 public FluentCollectionAssertion<R> msg(String msg, Object...args) { 129 super.msg(msg, args); 130 return this; 131 } 132 133 @Override /* GENERATED - Assertion */ 134 public FluentCollectionAssertion<R> stderr() { 135 super.stderr(); 136 return this; 137 } 138 139 @Override /* GENERATED - Assertion */ 140 public FluentCollectionAssertion<R> stdout() { 141 super.stdout(); 142 return this; 143 } 144 145 // </FluentSetters> 146}