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.lang.reflect.*; 018 019import org.apache.juneau.internal.*; 020import org.apache.juneau.marshall.*; 021 022/** 023 * Used for fluent assertion calls against array objects. 024 * 025 * @param <R> The return type. 026 */ 027@FluentSetters(returns="FluentArrayAssertion<R>") 028public class FluentArrayAssertion<R> extends FluentObjectAssertion<R> { 029 030 private Object value; 031 032 /** 033 * Constructor. 034 * 035 * @param contents The byte array being tested. 036 * @param returns The object to return after the test. 037 */ 038 public FluentArrayAssertion(Object contents, R returns) { 039 this(null, contents, returns); 040 } 041 042 /** 043 * Constructor. 044 * 045 * @param creator The assertion that created this assertion. 046 * @param contents The byte array being tested. 047 * @param returns The object to return after the test. 048 */ 049 public FluentArrayAssertion(Assertion creator, Object contents, R returns) { 050 super(creator, contents, returns); 051 this.value = contents; 052 } 053 054 /** 055 * Asserts that the collection exists and is empty. 056 * 057 * @return The object to return after the test. 058 * @throws AssertionError If assertion failed. 059 */ 060 public R isEmpty() throws AssertionError { 061 exists(); 062 if (Array.getLength(value) != 0) 063 throw error("Array was not empty."); 064 return returns(); 065 } 066 067 /** 068 * Asserts that the collection exists and is not empty. 069 * 070 * @return The object to return after the test. 071 * @throws AssertionError If assertion failed. 072 */ 073 public R isNotEmpty() throws AssertionError { 074 exists(); 075 if (Array.getLength(value) == 0) 076 throw error("Array was empty."); 077 return returns(); 078 } 079 080 /** 081 * Asserts that the collection exists and is the specified size. 082 * 083 * @param size The expected size. 084 * @return The object to return after the test. 085 * @throws AssertionError If assertion failed. 086 */ 087 public R isSize(int size) throws AssertionError { 088 exists(); 089 if (Array.getLength(value) != size) 090 throw error("Array did not have the expected size. Expected={0}, Actual={1}.", size, Array.getLength(value)); 091 return returns(); 092 } 093 094 /** 095 * Asserts that the array contains the expected value. 096 * 097 * @param value The value to check for. 098 * @return The object to return after the test. 099 * @throws AssertionError If assertion failed. 100 */ 101 public R contains(Object value) throws AssertionError { 102 exists(); 103 for (int i = 0; i < Array.getLength(this.value); i++) 104 if (eq(Array.get(this.value, i), value)) 105 return returns(); 106 throw error("Array did not contain expected value.\nContents: {0}\nExpected:{1}", SimpleJson.DEFAULT.toString(this.value), value); 107 } 108 109 /** 110 * Asserts that the array does not contain the expected value. 111 * 112 * @param value The value to check for. 113 * @return The object to return after the test. 114 * @throws AssertionError If assertion failed. 115 */ 116 public R doesNotContain(Object value) throws AssertionError { 117 exists(); 118 for (int i = 0; i < Array.getLength(this.value); i++) 119 if (eq(Array.get(this.value, i), value)) 120 throw error("Array contain unexpected value.\nContents: {0}\nUnexpected:{1}", SimpleJson.DEFAULT.toString(this.value), value); 121 return returns(); 122 } 123 124 /** 125 * Returns an object assertion on the item specified at the specified index. 126 * 127 * <p> 128 * If the array is <jk>null</jk> or the index is out-of-bounds, the returned assertion is a null assertion 129 * (meaning {@link FluentObjectAssertion#exists()} returns <jk>false</jk>). 130 * 131 * @param index The index of the item to retrieve from the array. 132 * @return A new assertion. 133 */ 134 public FluentObjectAssertion<R> item(int index) { 135 return new FluentObjectAssertion<>(this, getItem(index), returns()); 136 } 137 138 private Object getItem(int index) { 139 if (value != null && Array.getLength(value) > index) 140 return Array.get(value, index); 141 return null; 142 } 143 144 // <FluentSetters> 145 146 @Override /* GENERATED - Assertion */ 147 public FluentArrayAssertion<R> msg(String msg, Object...args) { 148 super.msg(msg, args); 149 return this; 150 } 151 152 @Override /* GENERATED - Assertion */ 153 public FluentArrayAssertion<R> stderr() { 154 super.stderr(); 155 return this; 156 } 157 158 @Override /* GENERATED - Assertion */ 159 public FluentArrayAssertion<R> stdout() { 160 super.stdout(); 161 return this; 162 } 163 164 // </FluentSetters> 165}