001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto.spec;
022:
023: import java.util.Arrays;
024: import javax.crypto.spec.PSource;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: /**
031: */
032:
033: public class PSourceTest extends TestCase {
034:
035: /**
036: * PSpecified(byte[] p) method testing. Tests that NullPointerException
037: * is thrown in the case of null p array. Also it checks the value of
038: * DEFAULT field, and that input p array is copied to protect against
039: * subsequent modification.
040: */
041: public void testPSpecified() {
042: try {
043: new PSource.PSpecified(null);
044: fail("NullPointerException should be thrown in the case of "
045: + "null p array.");
046: } catch (NullPointerException e) {
047: }
048:
049: assertEquals(
050: "The PSource.PSpecified DEFAULT value should be byte[0]",
051: 0, PSource.PSpecified.DEFAULT.getValue().length);
052:
053: byte[] p = new byte[] { 1, 2, 3, 4, 5 };
054: PSource.PSpecified ps = new PSource.PSpecified(p);
055: p[0]++;
056: assertFalse("The change of p specified in the constructor "
057: + "should not cause the change of internal array.",
058: p[0] == ps.getValue()[0]);
059: }
060:
061: /**
062: * getValue() method testing. Tests that returned array is equal to the
063: * array specified in the constructor. Checks that modification
064: * of returned array does not affect the internal array.
065: */
066: public void testGetValue() {
067: byte[] p = new byte[] { 1, 2, 3, 4, 5 };
068:
069: PSource.PSpecified ps = new PSource.PSpecified(p);
070: byte[] result = ps.getValue();
071: if (!Arrays.equals(p, result)) {
072: fail("The returned array does not equal to the specified "
073: + "in the constructor.");
074: }
075: result[0]++;
076: assertFalse("The change of returned by getValue() array "
077: + "should not cause the change of internal array.",
078: result[0] == ps.getValue()[0]);
079: }
080:
081: /**
082: * PSource(String pSrcName) method testing. Tests that returned value is
083: * equal to the value specified in the constructor.
084: */
085: public void testPSource() {
086: try {
087: new PSource(null);
088: fail("NullPointerException should be thrown in the case of "
089: + "null pSrcName.");
090: } catch (NullPointerException e) {
091: }
092: }
093:
094: /**
095: * getAlgorithm() method testing. Tests that returned value is
096: * equal to the value specified in the constructor.
097: */
098: public void testGetAlgorithm() {
099: String pSrcName = "pSrcName";
100: PSource ps = new PSource(pSrcName);
101: assertTrue(
102: "The returned value is not equal to the value specified "
103: + "in constructor", pSrcName.equals(ps
104: .getAlgorithm()));
105: }
106:
107: public static Test suite() {
108: return new TestSuite(PSourceTest.class);
109: }
110:
111: public static void main(String[] args) {
112: junit.textui.TestRunner.run(suite());
113: }
114: }
|