1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.test;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.HashMap;
24
25 import net.sf.magicproject.tools.Log;
26 import net.sf.magicproject.tools.MToolKit;
27
28 /***
29 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
30 * @since 0.90
31 */
32 public final class TestFactory {
33
34 /***
35 * Prevent this class to be instanciated.
36 */
37 private TestFactory() {
38 super();
39 }
40
41 /***
42 * Return the next Test read from the current offset.
43 * <ul>
44 * Structure of InputStream : Data[size]
45 * <li>test code [1]</li>
46 * </ul>
47 *
48 * @param inputFile
49 * is the file containing this test
50 * @return the next Test read from the current offset
51 * @throws IOException
52 * if error occurred during the reading process from the specified
53 * input stream
54 */
55 public static Test readNextTest(InputStream inputFile) throws IOException {
56 IdTest idTest = IdTest.deserialize(inputFile);
57
58 switch (idTest) {
59 case TRUE:
60 return True.getInstance();
61 case FALSE:
62 return False.getInstance();
63 case PHASE_IS:
64 return new PhaseIs(inputFile);
65 case VALID_TARGET_ATTACHMENT:
66 return new ValidTargetAttachment(inputFile);
67 case VALID_ATTACHMENT:
68 return new ValidAttachment(inputFile);
69 case REFERENCED_TEST:
70 final String referenceName = MToolKit.readString(inputFile);
71 final Test test = referencedTests.get(referenceName);
72 if (test == null) {
73 throw new InternalError("No test named as '" + referenceName
74 + "' exists in the rules declaration");
75 }
76 return test;
77 case EQUAL:
78 return new Equal(inputFile);
79 case INF:
80 return new Inf(inputFile);
81 case SUP:
82 return new Sup(inputFile);
83 case DIFFERENT:
84 return new Different(inputFile);
85 case INF_EQUAL:
86 return new InfEqual(inputFile);
87 case AND:
88 return new And(inputFile);
89 case OR:
90 return new Or(inputFile);
91 case NOT:
92 return new Not(inputFile);
93 case XOR:
94 return new Xor(inputFile);
95 case HAS_ACTION:
96 return new HasAction(inputFile);
97 case HAS_COLOR:
98 return new HasColor(inputFile);
99 case CONTROLLER:
100 return new IsController(inputFile);
101 case DATABASE:
102 return new Database(inputFile);
103 case HAS_IDCARD:
104 return new HasIdCard(inputFile);
105 case IN_IDCARD:
106 return new InIdCard(inputFile);
107 case IN_ZONE:
108 return new InZone(inputFile);
109 case IDPOSITION:
110 return new Position(inputFile);
111 case OWNER:
112 return new IsOwner(inputFile);
113 case HAS_PROPERTY:
114 return new HasProperty(inputFile);
115 case HAS_PROPERTY_INTERSECTION_COLOR:
116 return new HasPropertyIntersectionColor(inputFile);
117 case HAS_PROPERTY_INTERSECTION_IDCARD:
118 return new HasPropertyIntersectionIdCard(inputFile);
119 case HAS_PROPERTY_INTERSECTION_PROPERTY:
120 return new HasPropertyIntersectionProperty(inputFile);
121 case HAS_PROPERTY_NOT_FROM:
122 return new HasPropertyNotFromCreator(inputFile);
123 case TARGET_LIST:
124 return new TargetListContains(inputFile);
125 case IS_TESTED:
126 return new IsTested(inputFile);
127 case IS_ABILITY:
128 return new IsAbility(inputFile);
129 case IS_PLAYER:
130 return new IsPlayer(inputFile);
131 case IS_SPELL:
132 return new IsSpell(inputFile);
133 case HAS_NAME:
134 return new HasName(inputFile);
135 case HAS_KEYWORD:
136 return new HasKeyword(inputFile);
137 case HAS_ABILITY:
138 return new HasAbility(inputFile);
139 case ISME_PLAYER:
140 return new IsMePlayer(inputFile);
141 case SOURCE_ACTION_IS:
142 return new ActionSource(inputFile);
143 case ABILITY_IS:
144 return new AbilityIs(inputFile);
145 case SOURCE_ABILITY_IS:
146 return new AbilitySource(inputFile);
147 case CONTEXT_TEST:
148 return ContextTest.getInstance();
149 case SUP_EQUAL:
150 return new SupEqual(inputFile);
151 case PLAYABLE_ABILITY:
152 return new PlayableAbility(inputFile);
153 case HAS:
154 return new Has(inputFile);
155 case IS_FACE_UP:
156 return new IsFaceUp(inputFile);
157 case IS_ABORTING:
158 return new IsAborting(inputFile);
159 case HAS_PRIVATE_OBJECT:
160 return new HasPrivateObject(inputFile);
161 case PREVIOUS_ZONE:
162 return new PreviousZone(inputFile);
163 case REPLACE_TESTED:
164 return new ReplaceTested(inputFile);
165 case IS_COPY:
166 return new IsCopy(inputFile);
167 case DECK_COUNTER:
168 return new DeckCounter(inputFile);
169 default:
170 throw new InternalError("Unknown int test code: " + idTest);
171 }
172 }
173
174 /***
175 * Read available action constraints and abilities references.
176 * <ul>
177 * Structure of InputStream : Data[size]
178 * <li>number of action constraints [1]</li>
179 * <li>id action constraint i [1]</li>
180 * <li>test as constraint i [...]</li>
181 * </ul>
182 *
183 * @param dbStream
184 * the mdb stream's header.
185 * @throws IOException
186 * error during the test reading.
187 */
188 public static void init(InputStream dbStream) throws IOException {
189 if (referencedTests != null) {
190 referencedTests.clear();
191 } else {
192 referencedTests = new HashMap<String, Test>();
193 }
194 for (int i = dbStream.read(); i-- > 0;) {
195 final String key = MToolKit.readString(dbStream);
196
197 try {
198 referencedTests.put(key, readNextTest(dbStream));
199 } catch (Throwable t) {
200 Log.error("Error reading reference test '" + key + "'", t);
201 }
202 }
203 }
204
205 /***
206 * Set of declared tests
207 */
208 private static HashMap<String, Test> referencedTests = null;
209
210 }