| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.assertion; |
| 12 | |
| 13 | import java.util.HashSet; |
| 14 | import java.util.Set; |
| 15 | |
| 16 | final class ObjectFactoryContractTesterFactory |
| 17 | { |
| 18 | private ObjectFactoryContractTesterFactory() |
| 19 | { |
| 20 | |
| 21 | } |
| 22 | |
| 23 | static ObjectFactoryContractTester newObjectFactoryContractTester(final ObjectFactory<?> factory) throws NullPointerException |
| 24 | { |
| 25 | if(factory == null) |
| 26 | { |
| 27 | throw new NullPointerException(); |
| 28 | } |
| 29 | |
| 30 | return new ObjectFactoryContractTesterImpl(factory); |
| 31 | } |
| 32 | |
| 33 | private static final class ObjectFactoryContractTesterImpl implements ObjectFactoryContractTester |
| 34 | { |
| 35 | private final ObjectFactory<?> factory; |
| 36 | |
| 37 | ObjectFactoryContractTesterImpl(final ObjectFactory<?> factory) |
| 38 | { |
| 39 | this.factory = factory; |
| 40 | } |
| 41 | |
| 42 | public boolean isXConsistentlyNewInstance() |
| 43 | { |
| 44 | final Object x1 = factory.newInstanceX(); |
| 45 | |
| 46 | if(x1 == null) |
| 47 | { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | for(int i = 0; i < 20; i++) |
| 52 | { |
| 53 | final Object x2 = factory.newInstanceX(); |
| 54 | |
| 55 | if(x1 == x2) |
| 56 | { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | public boolean isYConsistentlyNewInstance() |
| 65 | { |
| 66 | final Object y1 = factory.newInstanceY(); |
| 67 | |
| 68 | if(y1 == null) |
| 69 | { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | for(int i = 0; i < 20; i++) |
| 74 | { |
| 75 | final Object y2 = factory.newInstanceY(); |
| 76 | |
| 77 | if(y1 == y2) |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | public boolean isXConsistentlyEqual() |
| 87 | { |
| 88 | Object x1 = factory.newInstanceX(); |
| 89 | |
| 90 | if(x1 == null) |
| 91 | { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | for(int i = 0; i < 20; i++) |
| 96 | { |
| 97 | final Object x2 = factory.newInstanceX(); |
| 98 | |
| 99 | if(!x1.equals(x2)) |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | x1 = x2; |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | public boolean isYConsistentlyEqual() |
| 111 | { |
| 112 | Object y1 = factory.newInstanceY(); |
| 113 | |
| 114 | if(y1 == null) |
| 115 | { |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | for(int i = 0; i < 20; i++) |
| 120 | { |
| 121 | final Object y2 = factory.newInstanceY(); |
| 122 | |
| 123 | if(!y1.equals(y2)) |
| 124 | { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | y1 = y2; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | public boolean isXYConsistentlyUnequal() |
| 135 | { |
| 136 | final Set<Object> xs = new HashSet<Object>(); |
| 137 | final Set<Object> ys = new HashSet<Object>(); |
| 138 | |
| 139 | for(int i = 0; i < 20; i++) |
| 140 | { |
| 141 | final Object x = factory.newInstanceX(); |
| 142 | final Object y = factory.newInstanceY(); |
| 143 | |
| 144 | if(x == null || y == null) |
| 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | xs.add(x); |
| 150 | ys.add(y); |
| 151 | |
| 152 | if(xs.contains(y) || ys.contains(x)) |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | public boolean fillsContract() |
| 162 | { |
| 163 | return isXConsistentlyNewInstance() && |
| 164 | isYConsistentlyNewInstance() && |
| 165 | isXConsistentlyEqual() && |
| 166 | isYConsistentlyEqual() && |
| 167 | isXYConsistentlyUnequal(); |
| 168 | } |
| 169 | } |
| 170 | } |