/* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package test.javafx.collections; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import javafx.collections.MapChangeListener; import static org.junit.Assert.*; public class MockMapObserver implements MapChangeListener { private List calls = new ArrayList(); @Override public void onChanged(Change c) { calls.add(new Call(c.getKey(), c.getValueRemoved(), c.getValueAdded())); } public int getCallsNumber() { return calls.size(); } public void clear() { calls.clear(); } public void check0() { assertEquals(0, calls.size()); } public void assertAdded(Tuple tuple) { assertAdded(0, tuple); } public void assertAdded(int call, Tuple tuple) { assertTrue("Missing call to the observer # " + call, call < calls.size()); assertEquals(calls.get(call).key, tuple.key); assertEquals(calls.get(call).added, tuple.val); } public void assertMultipleCalls(Call... calls) { assertEquals(this.calls.size(), calls.length); for (Call c : calls) { assertTrue(Arrays.toString(calls) + " doesn't contain " + c, this.calls.contains(c)); } } public void assertMultipleRemove(Tuple... tuples) { assertEquals(this.calls.size(), tuples.length); for (Tuple t : tuples) { assertTrue(calls + " doesn't contain " + t, this.calls.contains(new Call(t.key, t.val, null))); } } public void assertRemoved(Tuple tuple) { assertRemoved(0, tuple); } public void assertRemoved(int call, Tuple tuple) { assertTrue("Missing call to the observer # " + call, call < calls.size()); assertEquals(calls.get(call).key, tuple.key); assertEquals(calls.get(call).removed, tuple.val); } public void assertMultipleRemoved(Tuple... tuples) { for (Tuple t : tuples) { boolean found = false; for (Call c : calls ) { if (c.key.equals(t.key)) { assertEquals(c.removed, t.val); found = true; break; } } assertTrue(found); } } public static class Call { private K key; private V removed; private V added; public Call(K key, V removed, V added) { this.key = key; this.removed = removed; this.added = added; } public static Call call(K k, V o, V n) { return new Call(k, o, n); } @Override @SuppressWarnings("unchecked") public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Call other = (Call) obj; if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) { return false; } if (this.removed != other.removed && (this.removed == null || !this.removed.equals(other.removed))) { return false; } if (this.added != other.added && (this.added == null || !this.added.equals(other.added))) { return false; } return true; } @Override public int hashCode() { int hash = 7; hash = 47 * hash + (this.key != null ? this.key.hashCode() : 0); hash = 47 * hash + (this.removed != null ? this.removed.hashCode() : 0); hash = 47 * hash + (this.added != null ? this.added.hashCode() : 0); return hash; } @Override public String toString() { return "[ " + key + " -> " + added + " (" + removed + ") ]"; } } public static class Tuple { public K key; public V val; private Tuple(K key, V val) { this.key = key; this.val = val; } public static Tuple tup(K k, V v) { return new Tuple(k, v); } } }