1 /*
   2  * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.collections;
  27 
  28 import java.util.Arrays;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import static org.junit.Assert.*;
  32 
  33 public class MockMapObserver<K, V> implements MapChangeListener<K, V> {
  34 
  35     private List<Call> calls = new ArrayList<Call>();
  36 
  37     @Override
  38     public void onChanged(Change<? extends K,? extends V> c) {
  39         calls.add(new Call<K, V>(c.getKey(), c.getValueRemoved(), c.getValueAdded()));
  40     }
  41 
  42     public int getCallsNumber() {
  43         return calls.size();
  44     }
  45 
  46     public void clear() {
  47         calls.clear();
  48     }
  49 
  50     public void check0() {
  51         assertEquals(0, calls.size());
  52     }
  53 
  54     public void assertAdded(Tuple<K, V> tuple) {
  55         assertAdded(0, tuple);
  56     }
  57 
  58     public void assertAdded(int call, Tuple<K, V> tuple) {
  59         assertTrue("Missing call to the observer # " + call, call < calls.size());
  60         assertEquals(calls.get(call).key, tuple.key);
  61         assertEquals(calls.get(call).added, tuple.val);
  62     }
  63 
  64     public void assertMultipleCalls(Call<K, V>... calls) {
  65         assertEquals(this.calls.size(), calls.length);
  66         for (Call<K,V> c : calls) {
  67             assertTrue(Arrays.toString(calls) + " doesn't contain "  + c, this.calls.contains(c));
  68         }
  69     }
  70 
  71     public void assertMultipleRemove(Tuple<K, V>... tuples) {
  72         assertEquals(this.calls.size(), tuples.length);
  73         for (Tuple<K,V> t : tuples) {
  74             assertTrue(calls + " doesn't contain "  + t, this.calls.contains(new Call<K,V>(t.key, t.val, null)));
  75         }
  76     }
  77 
  78     public void assertRemoved(Tuple<K, V> tuple) {
  79         assertRemoved(0, tuple);
  80     }
  81 
  82     public void assertRemoved(int call, Tuple<K, V> tuple) {
  83         assertTrue("Missing call to the observer # " + call, call < calls.size());
  84         assertEquals(calls.get(call).key, tuple.key);
  85         assertEquals(calls.get(call).removed, tuple.val);
  86     }
  87 
  88     public void assertMultipleRemoved(Tuple<K, V>... tuples) {
  89         for (Tuple<K,V> t : tuples) {
  90             boolean found = false;
  91             for (Call c : calls ) {
  92                 if (c.key.equals(t.key)) {
  93                     assertEquals(c.removed, t.val);
  94                     found = true;
  95                     break;
  96                 }
  97             }
  98             assertTrue(found);
  99         }
 100     }
 101 
 102     public static class Call<K, V> {
 103         private K key;
 104         private V removed;
 105         private V added;
 106 
 107         public Call(K key, V removed, V added) {
 108             this.key = key;
 109             this.removed = removed;
 110             this.added = added;
 111         }
 112 
 113         public static<K, V> Call<K, V> call(K k, V o, V n) {
 114             return new Call<K, V>(k, o, n);
 115         }
 116 
 117         @Override
 118         @SuppressWarnings("unchecked")
 119         public boolean equals(Object obj) {
 120             if (obj == null) {
 121                 return false;
 122             }
 123             if (getClass() != obj.getClass()) {
 124                 return false;
 125             }
 126             final Call<K, V> other = (Call<K, V>) obj;
 127             if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) {
 128                 return false;
 129             }
 130             if (this.removed != other.removed && (this.removed == null || !this.removed.equals(other.removed))) {
 131                 return false;
 132             }
 133             if (this.added != other.added && (this.added == null || !this.added.equals(other.added))) {
 134                 return false;
 135             }
 136             return true;
 137         }
 138 
 139         @Override
 140         public int hashCode() {
 141             int hash = 7;
 142             hash = 47 * hash + (this.key != null ? this.key.hashCode() : 0);
 143             hash = 47 * hash + (this.removed != null ? this.removed.hashCode() : 0);
 144             hash = 47 * hash + (this.added != null ? this.added.hashCode() : 0);
 145             return hash;
 146         }
 147 
 148         @Override
 149         public String toString() {
 150             return "[ " + key + " -> " + added + " (" + removed + ") ]";
 151         }
 152 
 153     }
 154 
 155     public static class Tuple<K, V> {
 156         public K key;
 157         public V val;
 158 
 159         private Tuple(K key, V val) {
 160             this.key = key;
 161             this.val = val;
 162         }
 163 
 164         public static<K, V> Tuple<K, V> tup(K k, V v) {
 165             return new Tuple<K, V>(k, v);
 166         }
 167     }
 168 
 169 }