package com.ibm.emb.test;

import javax.emb.MediaException;
import javax.emb.ProtocolConstraints;

import com.ibm.emb.junit.EMBStaticHelper;
import com.ibm.emb.junit.EMBTestCaseBase;
import com.ibm.emb.junit.EMBTestRunner;

/**
 * <pre>
 *
 *
 *
 *
 *     Line Item: MFB API
 *     Subcategory 1: javax.emb
 *     Subcategory 2: ProtocolConstraint
 *
 *
 *
 *
 * </pre>
 */
public class ProtocolConstraintsTest extends EMBTestCaseBase {

    public ProtocolConstraintsTest(String name) throws MediaException {
        super(name);
    }

    public static void main(String args[]) {
        EMBTestRunner.run(ProtocolConstraintsTest.class);
    }

    /**
     * <pre>
     *
     *
     *
     *
     *     Testcase Name: getConstraint(String)
     *     Testcase Number: EMB145
     *
     *     setup: create testInstance of ProtocolConstraints
     *
     *     test procedure:
     *     1.call getConstraint(null)
     *       expected result: NullPointerException
     *
     *     2.call getConstraint(random String)
     *       expected result: null
     *
     *     3.call setConstraint(&quot;test&quot;, random String)
     *       call getConstraint(&quot;test&quot;)
     *       expected result: random String that was passed in
     *
     *     4.call setConstraint(&quot;SERVER_TYPE, random array of Strings)
     *     	 call getConstraint(&quot;SERVER_TYPE&quot;)
     *       expected result: array of Strings
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB145() {
        //
        // test 1
        //
        ProtocolConstraints testInstance = new ProtocolConstraints();
        try {
            testInstance.getConstraint(null);
            fail("test 1: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw a NullPointerException but threw " + e.toString());
        }
        //
        // test 2
        //
        testInstance = new ProtocolConstraints();
        assertNull("test 2: random constraint not null", testInstance.getConstraint(EMBStaticHelper
                .createRandomString(EMBStaticHelper.randomInt(0, 10))));
        testTrace("test 2 passed");
        //
        // test 3
        //
        testInstance = new ProtocolConstraints();
        String constraint = "test";
        String value = "testValue";
        try {
            testInstance.setConstraint(constraint, value);
            fail("test 3: Should throw a IllegalArgumentException.");
        } catch (IllegalArgumentException e) {
            testTrace("test 3 passed");
        }

        //
        // test 4
        //
        testInstance = new ProtocolConstraints();
        String[] constraintValues = new String[EMBStaticHelper.randomInt(0, 5)];
        for (int i = 0; i < constraintValues.length; i++) {
            constraintValues[i] = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10));
        }
        testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE, constraintValues);

        String[] serverTypeValues = (String[]) testInstance.getConstraint(ProtocolConstraints.SERVER_TYPE);
        assertTrue("test 4: constraint values do not match", java.util.Arrays
                .equals(serverTypeValues, constraintValues));
        testTrace("test 4 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *     Testcase Name: getConstraintTypes()
     *     Testcase Number: EMB146
     *
     *     setup: create testInstance of ProtocolConstraints
     *
     *     test procedure:
     *     1.
     *       call setConstraint(&quot;SERVER_TYPE&quot;, random array of Strings)
     *       call setConstraint(&quot;CLIENT_TYPE&quot;, random array of Strings)
     *       expected result: 2 element String array &quot;SERVER_TYPE&quot;, and &quot;CLIENT_TYPE&quot;
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB146() {
        ProtocolConstraints testInstance = new ProtocolConstraints();

        //
        // test 1
        //

        String[] serverTypes = new String[EMBStaticHelper.randomInt(0, 5)];
        for (int i = 0; i < serverTypes.length; i++) {
            serverTypes[i] = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10));
        }
        String[] clientTypes = new String[EMBStaticHelper.randomInt(0, 5)];
        for (int j = 0; j < clientTypes.length; j++) {
            clientTypes[j] = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10));
        }
        testInstance.setConstraint("SERVER_TYPE", serverTypes);
        testInstance.setConstraint("CLIENT_TYPE", clientTypes);

        String[] constraints = testInstance.getConstraintTypes();
        boolean serverTypeFound = false;
        boolean clientTypeFound = false;

        for (int k = 0; k < constraints.length; k++) {
            if (constraints[k].equals(ProtocolConstraints.SERVER_TYPE)) {
                serverTypeFound = true;
            }else if (constraints[k].equals(ProtocolConstraints.CLIENT_TYPE)) {
                clientTypeFound = true;
            }
        }

        assertTrue("test 1: did not find constraint (SERVER_TYPE)", serverTypeFound);
        assertTrue("test 1: did not find constraint (CLIENT_TYPE)", clientTypeFound);
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *
     *     Testcase Name: setConstraint(String, Object)
     *     Testcase Number: EMB147
     *
     *     setup: create testInstance of ProtocolConstraints
     *
     *     test procedure:
     *
     *     1.call setConstraint(null, random String)
     *       expected result: NullPointerException
     *
     *     2.call setConstraint(&quot;test&quot;, null)
     *       expected result: NullPointerException
     *
     *     3.call setConstraint(&quot;test&quot;, random String)
     *       call getConstraint(&quot;test&quot;)
     *       expected result: random String that was passed in
     *
     *     4.a.call setConstraint(&quot;SERVER_TYPE&quot;, array of Strings)
     *         call getConstraint(&quot;SERVER_TYPE&quot;)
     *         expected result: array of Strings passed in
     *     	 b.call setConstraint(&quot;SERVER_TYPE&quot;, random Object)
     *         expected result: IllegalArgumentException
     *
     *     5.a.call setConstraint(&quot;CLIENT_TYPE&quot;, array of Strings)
     *         call getConstraint(&quot;CLIENT_TYPE&quot;)
     *         expected result: array of Strings passed in
     *     	 b.call setConstraint(&quot;CLIENT_TYPE&quot;, random Object)
     *         expected result: IllegalArgumentException
     *
     *
     *
     *
     * </pre>
     */
    public void testEMB147() {
        //
        // test 1
        //
        ProtocolConstraints testInstance = new ProtocolConstraints();
        try {
            testInstance.setConstraint(null, new Object());
            fail("test 1: Should throw a NullPointerException");
        } catch (NullPointerException e) {
            testTrace("test 1 passed");
        } catch (Exception e) {
            fail("test 1: Should throw a NullPointerException but threw " + e.toString());
        }

        //
        // test 2
        //
        testInstance = new ProtocolConstraints();
        try {
            testInstance.setConstraint("test", null);
            fail("test 2: Should throw a IllegalArgumentException");
        } catch (NullPointerException e) {
            fail("test 2: Should throw a IllegalArgumentException but threw " + e.toString());
        } catch (IllegalArgumentException e) {
            testTrace("test 2 passed");
        } catch (Exception e) {
            fail("test 2: Should throw a IllegalArgumentException but threw " + e.toString());
        }

        //
        // test 3
        //
        testInstance = new ProtocolConstraints();
        String[] testString = {EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 50))};
        testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE, testString);
        assertSame("test 3: ", testString, testInstance.getConstraint(ProtocolConstraints.SERVER_TYPE));
        testTrace("test 3 passed");

        //
        // test 4a
        //
        testInstance = new ProtocolConstraints();
        String[] serverTypes = new String[EMBStaticHelper.randomInt(0, 5)];
        for (int i = 0; i < serverTypes.length; i++) {
            serverTypes[i] = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10));
        }
        testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE, serverTypes);
        assertTrue("test 4a: server types do not match", java.util.Arrays.equals(serverTypes, (String[]) testInstance
                .getConstraint(ProtocolConstraints.SERVER_TYPE)));
        testTrace("test 4a passed");
        //
        // test 4b
        //
        testInstance = new ProtocolConstraints();
        Object randomObj = new Object();
        try {
            testInstance.setConstraint(ProtocolConstraints.SERVER_TYPE, randomObj);
        } catch (IllegalArgumentException e) {
            testTrace("test 4b passed");
        } catch (Exception e) {
            fail("test 4b: Should throw a MediaException");
        }

        //
        // test 5a
        //
        testInstance = new ProtocolConstraints();
        String[] clientTypes = new String[EMBStaticHelper.randomInt(0, 5)];
        for (int j = 0; j < clientTypes.length; j++) {
            clientTypes[j] = EMBStaticHelper.createRandomString(EMBStaticHelper.randomInt(0, 10));
        }
        testInstance.setConstraint(ProtocolConstraints.CLIENT_TYPE, clientTypes);
        assertTrue("test 5a: server types do not match", java.util.Arrays.equals(clientTypes, (String[]) testInstance
                .getConstraint(ProtocolConstraints.CLIENT_TYPE)));
        testTrace("test 5a passed");
        //
        // test 5b
        //
        testInstance = new ProtocolConstraints();
        randomObj = new Object();
        try {
            testInstance.setConstraint(ProtocolConstraints.CLIENT_TYPE, randomObj);
        } catch (IllegalArgumentException e) {
            testTrace("test 5b passed");
        } catch (Exception e) {
            fail("test 5b: Should throw a MediaException");
        }

        succeed();
    }

}