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 
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import java.util.List;
  32 
  33 import static org.junit.Assert.assertEquals;
  34 import static org.junit.Assert.assertTrue;
  35 
  36 public class MockSetObserver<E> implements SetChangeListener<E> {
  37 
  38     private List<Call> calls = new ArrayList<Call>();
  39 
  40     @Override
  41     public void onChanged(Change<? extends E> c) {
  42         calls.add(new Call<E>(c.getElementRemoved(), c.getElementAdded()));
  43     }
  44 
  45     public int getCallsNumber() {
  46         return calls.size();
  47     }
  48 
  49     public void clear() {
  50         calls.clear();
  51     }
  52 
  53     public void check0() {
  54         assertEquals(0, calls.size());
  55     }
  56 
  57     public void assertAdded(Tuple<E> tuple) {
  58         assertAdded(0, tuple);
  59     }
  60 
  61     public void assertAdded(int call, Tuple<E> tuple) {
  62         assertTrue("Missing call to the observer # " + call, call < calls.size());
  63         assertEquals(calls.get(call).added, tuple.val);
  64     }
  65 
  66     public void assertMultipleCalls(Call<E>... calls) {
  67         assertEquals(this.calls.size(), calls.length);
  68         for (Call<E> c : calls) {
  69             assertTrue(Arrays.toString(calls) + " doesn't contain "  + c, this.calls.contains(c));
  70         }
  71     }
  72 
  73     public void assertMultipleRemove(Tuple<E>... tuples) {
  74         assertEquals(this.calls.size(), tuples.length);
  75         for (Tuple<E> t : tuples) {
  76             assertTrue(calls + " doesn't contain "  + t, this.calls.contains(new Call<E>(t.val, null)));
  77         }
  78     }
  79 
  80     public void assertRemoved(Tuple<E> tuple) {
  81         assertRemoved(0, tuple);
  82     }
  83 
  84     public void assertRemoved(int call, Tuple<E> tuple) {
  85         assertTrue("Missing call to the observer # " + call, call < calls.size());
  86         assertEquals(calls.get(call).removed, tuple.val);
  87     }
  88 
  89     public void assertMultipleRemoved(Tuple<E>... tuples) {
  90         for (Tuple<E> t : tuples) {
  91             boolean found = false;
  92             for (Call c : calls) {
  93                 if (c.removed.equals(t.val)) {
  94                     found = true;
  95                     break;
  96                 }
  97             }
  98             assertTrue(found);
  99         }
 100     }
 101 
 102     public static class Call<E> {
 103         private E removed;
 104         private E added;
 105 
 106         public Call(E removed, E added) {
 107             this.removed = removed;
 108             this.added = added;
 109         }
 110 
 111         public static<E> Call<E> call(E o, E n) {
 112             return new Call<E>(o, n);
 113         }
 114 
 115         @Override
 116         @SuppressWarnings("unchecked")
 117         public boolean equals(Object obj) {
 118             if (obj == null) {
 119                 return false;
 120             }
 121             if (getClass() != obj.getClass()) {
 122                 return false;
 123             }
 124             final Call<E> other = (Call<E>) obj;
 125             if (this.removed != other.removed && (this.removed == null || !this.removed.equals(other.removed))) {
 126                 return false;
 127             }
 128             if (this.added != other.added && (this.added == null || !this.added.equals(other.added))) {
 129                 return false;
 130             }
 131             return true;
 132         }
 133 
 134         @Override
 135         public int hashCode() {
 136             int hash = 7;
 137             hash = 47 * hash + (this.removed != null ? this.removed.hashCode() : 0);
 138             hash = 47 * hash + (this.added != null ? this.added.hashCode() : 0);
 139             return hash;
 140         }
 141 
 142         @Override
 143         public String toString() {
 144             return "[ " + added + " (" + removed + ") ]";
 145         }
 146 
 147     }
 148 
 149     public static class Tuple<E> {
 150         public E val;
 151 
 152         private Tuple(E val) {
 153             this.val = val;
 154         }
 155 
 156         public static<E> Tuple<E> tup(E v) {
 157             return new Tuple<E>(v);
 158         }
 159     }
 160 
 161 }