001: package com.ibm.emb.test;
002:
003: import javax.emb.MediaException;
004: import javax.emb.ProtocolConstraints;
005:
006: import com.ibm.emb.junit.EMBStaticHelper;
007: import com.ibm.emb.junit.EMBTestCaseBase;
008: import com.ibm.emb.junit.EMBTestRunner;
009:
010: /**
011: * <pre>
012: *
013: *
014: *
015: *
016: * Line Item: MFB API
017: * Subcategory 1: javax.emb
018: * Subcategory 2: ProtocolConstraint
019: *
020: *
021: *
022: *
023: * </pre>
024: */
025: public class ProtocolConstraintsTest extends EMBTestCaseBase {
026:
027: public ProtocolConstraintsTest(String name) throws MediaException {
028: super (name);
029: }
030:
031: public static void main(String args[]) {
032: EMBTestRunner.run(ProtocolConstraintsTest.class);
033: }
034:
035: /**
036: * <pre>
037: *
038: *
039: *
040: *
041: * Testcase Name: getConstraint(String)
042: * Testcase Number: EMB145
043: *
044: * setup: create testInstance of ProtocolConstraints
045: *
046: * test procedure:
047: * 1.call getConstraint(null)
048: * expected result: NullPointerException
049: *
050: * 2.call getConstraint(random String)
051: * expected result: null
052: *
053: * 3.call setConstraint("test", random String)
054: * call getConstraint("test")
055: * expected result: random String that was passed in
056: *
057: * 4.call setConstraint("SERVER_TYPE, random array of Strings)
058: * call getConstraint("SERVER_TYPE")
059: * expected result: array of Strings
060: *
061: *
062: *
063: *
064: * </pre>
065: */
066: public void testEMB145() {
067: //
068: // test 1
069: //
070: ProtocolConstraints testInstance = new ProtocolConstraints();
071: try {
072: testInstance.getConstraint(null);
073: fail("test 1: Should throw a NullPointerException");
074: } catch (NullPointerException e) {
075: testTrace("test 1 passed");
076: } catch (Exception e) {
077: fail("test 1: Should throw a NullPointerException but threw "
078: + e.toString());
079: }
080: //
081: // test 2
082: //
083: testInstance = new ProtocolConstraints();
084: assertNull("test 2: random constraint not null", testInstance
085: .getConstraint(EMBStaticHelper
086: .createRandomString(EMBStaticHelper.randomInt(
087: 0, 10))));
088: testTrace("test 2 passed");
089: //
090: // test 3
091: //
092: testInstance = new ProtocolConstraints();
093: String constraint = "test";
094: String value = "testValue";
095: try {
096: testInstance.setConstraint(constraint, value);
097: fail("test 3: Should throw a IllegalArgumentException.");
098: } catch (IllegalArgumentException e) {
099: testTrace("test 3 passed");
100: }
101:
102: //
103: // test 4
104: //
105: testInstance = new ProtocolConstraints();
106: String[] constraintValues = new String[EMBStaticHelper
107: .randomInt(0, 5)];
108: for (int i = 0; i < constraintValues.length; i++) {
109: constraintValues[i] = EMBStaticHelper
110: .createRandomString(EMBStaticHelper
111: .randomInt(0, 10));
112: }
113: testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE,
114: constraintValues);
115:
116: String[] serverTypeValues = (String[]) testInstance
117: .getConstraint(ProtocolConstraints.SERVER_TYPE);
118: assertTrue("test 4: constraint values do not match",
119: java.util.Arrays.equals(serverTypeValues,
120: constraintValues));
121: testTrace("test 4 passed");
122:
123: succeed();
124: }
125:
126: /**
127: * <pre>
128: *
129: *
130: *
131: *
132: * Testcase Name: getConstraintTypes()
133: * Testcase Number: EMB146
134: *
135: * setup: create testInstance of ProtocolConstraints
136: *
137: * test procedure:
138: * 1.
139: * call setConstraint("SERVER_TYPE", random array of Strings)
140: * call setConstraint("CLIENT_TYPE", random array of Strings)
141: * expected result: 2 element String array "SERVER_TYPE", and "CLIENT_TYPE"
142: *
143: *
144: *
145: *
146: * </pre>
147: */
148: public void testEMB146() {
149: ProtocolConstraints testInstance = new ProtocolConstraints();
150:
151: //
152: // test 1
153: //
154:
155: String[] serverTypes = new String[EMBStaticHelper.randomInt(0,
156: 5)];
157: for (int i = 0; i < serverTypes.length; i++) {
158: serverTypes[i] = EMBStaticHelper
159: .createRandomString(EMBStaticHelper
160: .randomInt(0, 10));
161: }
162: String[] clientTypes = new String[EMBStaticHelper.randomInt(0,
163: 5)];
164: for (int j = 0; j < clientTypes.length; j++) {
165: clientTypes[j] = EMBStaticHelper
166: .createRandomString(EMBStaticHelper
167: .randomInt(0, 10));
168: }
169: testInstance.setConstraint("SERVER_TYPE", serverTypes);
170: testInstance.setConstraint("CLIENT_TYPE", clientTypes);
171:
172: String[] constraints = testInstance.getConstraintTypes();
173: boolean serverTypeFound = false;
174: boolean clientTypeFound = false;
175:
176: for (int k = 0; k < constraints.length; k++) {
177: if (constraints[k].equals(ProtocolConstraints.SERVER_TYPE)) {
178: serverTypeFound = true;
179: } else if (constraints[k]
180: .equals(ProtocolConstraints.CLIENT_TYPE)) {
181: clientTypeFound = true;
182: }
183: }
184:
185: assertTrue("test 1: did not find constraint (SERVER_TYPE)",
186: serverTypeFound);
187: assertTrue("test 1: did not find constraint (CLIENT_TYPE)",
188: clientTypeFound);
189: testTrace("test 1 passed");
190:
191: succeed();
192: }
193:
194: /**
195: * <pre>
196: *
197: *
198: *
199: *
200: * Testcase Name: setConstraint(String, Object)
201: * Testcase Number: EMB147
202: *
203: * setup: create testInstance of ProtocolConstraints
204: *
205: * test procedure:
206: *
207: * 1.call setConstraint(null, random String)
208: * expected result: NullPointerException
209: *
210: * 2.call setConstraint("test", null)
211: * expected result: NullPointerException
212: *
213: * 3.call setConstraint("test", random String)
214: * call getConstraint("test")
215: * expected result: random String that was passed in
216: *
217: * 4.a.call setConstraint("SERVER_TYPE", array of Strings)
218: * call getConstraint("SERVER_TYPE")
219: * expected result: array of Strings passed in
220: * b.call setConstraint("SERVER_TYPE", random Object)
221: * expected result: IllegalArgumentException
222: *
223: * 5.a.call setConstraint("CLIENT_TYPE", array of Strings)
224: * call getConstraint("CLIENT_TYPE")
225: * expected result: array of Strings passed in
226: * b.call setConstraint("CLIENT_TYPE", random Object)
227: * expected result: IllegalArgumentException
228: *
229: *
230: *
231: *
232: * </pre>
233: */
234: public void testEMB147() {
235: //
236: // test 1
237: //
238: ProtocolConstraints testInstance = new ProtocolConstraints();
239: try {
240: testInstance.setConstraint(null, new Object());
241: fail("test 1: Should throw a NullPointerException");
242: } catch (NullPointerException e) {
243: testTrace("test 1 passed");
244: } catch (Exception e) {
245: fail("test 1: Should throw a NullPointerException but threw "
246: + e.toString());
247: }
248:
249: //
250: // test 2
251: //
252: testInstance = new ProtocolConstraints();
253: try {
254: testInstance.setConstraint("test", null);
255: fail("test 2: Should throw a IllegalArgumentException");
256: } catch (NullPointerException e) {
257: fail("test 2: Should throw a IllegalArgumentException but threw "
258: + e.toString());
259: } catch (IllegalArgumentException e) {
260: testTrace("test 2 passed");
261: } catch (Exception e) {
262: fail("test 2: Should throw a IllegalArgumentException but threw "
263: + e.toString());
264: }
265:
266: //
267: // test 3
268: //
269: testInstance = new ProtocolConstraints();
270: String[] testString = { EMBStaticHelper
271: .createRandomString(EMBStaticHelper.randomInt(0, 50)) };
272: testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE,
273: testString);
274: assertSame("test 3: ", testString, testInstance
275: .getConstraint(ProtocolConstraints.SERVER_TYPE));
276: testTrace("test 3 passed");
277:
278: //
279: // test 4a
280: //
281: testInstance = new ProtocolConstraints();
282: String[] serverTypes = new String[EMBStaticHelper.randomInt(0,
283: 5)];
284: for (int i = 0; i < serverTypes.length; i++) {
285: serverTypes[i] = EMBStaticHelper
286: .createRandomString(EMBStaticHelper
287: .randomInt(0, 10));
288: }
289: testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE,
290: serverTypes);
291: assertTrue(
292: "test 4a: server types do not match",
293: java.util.Arrays
294: .equals(
295: serverTypes,
296: (String[]) testInstance
297: .getConstraint(ProtocolConstraints.SERVER_TYPE)));
298: testTrace("test 4a passed");
299: //
300: // test 4b
301: //
302: testInstance = new ProtocolConstraints();
303: Object randomObj = new Object();
304: try {
305: testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE,
306: randomObj);
307: } catch (IllegalArgumentException e) {
308: testTrace("test 4b passed");
309: } catch (Exception e) {
310: fail("test 4b: Should throw a MediaException");
311: }
312:
313: //
314: // test 5a
315: //
316: testInstance = new ProtocolConstraints();
317: String[] clientTypes = new String[EMBStaticHelper.randomInt(0,
318: 5)];
319: for (int j = 0; j < clientTypes.length; j++) {
320: clientTypes[j] = EMBStaticHelper
321: .createRandomString(EMBStaticHelper
322: .randomInt(0, 10));
323: }
324: testInstance.setConstraint(ProtocolConstraints.CLIENT_TYPE,
325: clientTypes);
326: assertTrue(
327: "test 5a: server types do not match",
328: java.util.Arrays
329: .equals(
330: clientTypes,
331: (String[]) testInstance
332: .getConstraint(ProtocolConstraints.CLIENT_TYPE)));
333: testTrace("test 5a passed");
334: //
335: // test 5b
336: //
337: testInstance = new ProtocolConstraints();
338: randomObj = new Object();
339: try {
340: testInstance.setConstraint(ProtocolConstraints.CLIENT_TYPE,
341: randomObj);
342: } catch (IllegalArgumentException e) {
343: testTrace("test 5b passed");
344: } catch (Exception e) {
345: fail("test 5b: Should throw a MediaException");
346: }
347:
348: succeed();
349: }
350:
351: }
|