1 /*
2 * SmartSprites Project
3 *
4 * Copyright (C) 2007-2009, Stanisław Osiński.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without modification,
8 * are permitted provided that the following conditions are met:
9 *
10 * - Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * - Redistributions in binary form must reproduce the above copyright notice, this
14 * list of conditions and the following disclaimer in the documentation and/or
15 * other materials provided with the distribution.
16 *
17 * - Neither the name of the SmartSprites Project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * - We kindly request that you include in the end-user documentation provided with
22 * the redistribution and/or in the software itself an acknowledgement equivalent
23 * to the following: "This product includes software developed by the SmartSprites
24 * Project."
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 package org.carrot2.labs.test;
38
39 import com.google.common.collect.Lists;
40 import com.google.common.collect.Sets;
41
42 import java.util.ArrayList;
43 import java.util.Iterator;
44 import java.util.List;
45 import java.util.Set;
46
47 import org.assertj.core.api.Assertions;
48 import org.assertj.core.api.Fail;
49 import org.carrot2.labs.smartsprites.message.Message;
50 import org.carrot2.labs.smartsprites.message.Message.MessageLevel;
51
52 /**
53 * Assertions on lists of {@link Message}s.
54 */
55 public class MessageListAssertion {
56
57 /** The actual message list. */
58 private List<Message> actual;
59
60 /**
61 * Creates a {@link Message} list assertion object.
62 *
63 * @param actual
64 * the actual
65 */
66 public MessageListAssertion(List<Message> actual) {
67 this.actual = actual;
68 }
69
70 /**
71 * Asserts that the current message list contains (at least) the specified messages.
72 *
73 * @param messages
74 * the messages
75 *
76 * @return the message list assertion
77 */
78 public MessageListAssertion contains(Message... messages) {
79 final Set<Message> toCheck = Sets.newHashSet(messages);
80 for (int i = 0; i < actual.size(); i++) {
81 for (Iterator<Message> it = toCheck.iterator(); it.hasNext();) {
82 final Message message = it.next();
83 try {
84 org.carrot2.labs.test.Assertions.assertThat(actual.get(i)).as("message[" + i + "]")
85 .isEquivalentTo(message);
86 it.remove();
87
88 } catch (AssertionError e) {
89 // This means the message wasn't equivalent, ignore
90 }
91 }
92 }
93
94 if (!toCheck.isEmpty()) {
95 Fail.fail("Message list did not contain " + toCheck.size() + " of the required messages");
96 }
97
98 return this;
99 }
100
101 /**
102 * Asserts that the current message list is equivalent to the provided expected message list.
103 *
104 * @param expected
105 * the expected
106 *
107 * @return the message list assertion
108 */
109 public MessageListAssertion isEquivalentTo(List<Message> expected) {
110 Assertions.assertThat(actual).hasSize(expected.size());
111 for (int i = 0; i < actual.size(); i++) {
112 org.carrot2.labs.test.Assertions.assertThat(actual.get(i)).as("message[" + i + "]")
113 .isEquivalentTo(expected.get(i));
114 }
115 return this;
116 }
117
118 /**
119 * Asserts that the current message list is equivalent to the provided expected message list.
120 *
121 * @param onlyLevel
122 * the only level
123 * @param expected
124 * the expected
125 *
126 * @return the message list assertion
127 */
128 public MessageListAssertion isEquivalentTo(MessageLevel onlyLevel, List<Message> expected) {
129 final List<Message> filtered = new ArrayList<>();
130 for (Message message : actual) {
131 if (message.level == onlyLevel) {
132 filtered.add(message);
133 }
134 }
135
136 final List<Message> actualBackup = actual;
137 actual = filtered;
138 isEquivalentTo(expected);
139 actual = actualBackup;
140
141 return this;
142 }
143
144 /**
145 * Asserts that the current message list is equivalent to the provided expected message list.
146 *
147 * @param messages
148 * the messages
149 *
150 * @return the message list assertion
151 */
152 public MessageListAssertion isEquivalentTo(Message... messages) {
153 return isEquivalentTo(Lists.newArrayList(messages));
154 }
155
156 /**
157 * Asserts that the current message list is equivalent to the provided expected message list.
158 *
159 * @param onlyLevel
160 * the only level
161 * @param messages
162 * the messages
163 *
164 * @return the message list assertion
165 */
166 public MessageListAssertion isEquivalentTo(MessageLevel onlyLevel, Message... messages) {
167 return isEquivalentTo(onlyLevel, Lists.newArrayList(messages));
168 }
169
170 /**
171 * Does not have messages of level.
172 *
173 * @param level
174 * the level
175 *
176 * @return the message list assertion
177 */
178 public MessageListAssertion doesNotHaveMessagesOfLevel(MessageLevel level) {
179 int levelCount = 0;
180 final StringBuilder messages = new StringBuilder();
181 for (Message message : actual) {
182 // Ignore status messages
183 if (message.level.equals(MessageLevel.STATUS)) {
184 continue;
185 }
186
187 if (MessageLevel.COMPARATOR.compare(message.level, level) >= 0) {
188 levelCount++;
189 messages.append(message.toString());
190 messages.append(", ");
191 }
192 }
193
194 if (levelCount > 0) {
195 Fail.fail("Found " + levelCount + " " + level.name() + "+ messages: "
196 + messages.substring(0, messages.length() - 2));
197 }
198
199 return this;
200 }
201
202 /**
203 * Checks if is empty.
204 *
205 * @return the message list assertion
206 */
207 public MessageListAssertion isEmpty() {
208 Assertions.assertThat(actual).isEmpty();
209 return this;
210 }
211 }