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: package org.apache.harmony.jndi.provider.ldap.asn1;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.util.Arrays;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.harmony.security.asn1.ASN1Boolean;
026: import org.apache.harmony.security.asn1.ASN1Enumerated;
027: import org.apache.harmony.security.asn1.ASN1Integer;
028: import org.apache.harmony.security.asn1.ASN1OctetString;
029: import org.apache.harmony.security.asn1.ASN1Sequence;
030: import org.apache.harmony.security.asn1.ASN1Type;
031:
032: public class UtilsTest extends TestCase {
033:
034: private final String testStr = "test123";
035:
036: private final char[] testCharArray = testStr.toCharArray();
037:
038: private byte[] testByteArray;
039:
040: public UtilsTest() {
041: super ();
042:
043: try {
044: testByteArray = testStr.getBytes(Utils.CODING_CHARSET);
045: } catch (UnsupportedEncodingException e) {
046: // never reached, because UTF-8 is supported by all java
047: // platform
048: }
049: }
050:
051: public void test_conjoinSequence() {
052: ASN1Sequence first = new ASN1Sequence(new ASN1Type[] {
053: ASN1OctetString.getInstance(),
054: ASN1Integer.getInstance(), ASN1Boolean.getInstance() }) {
055: {
056: setDefault(Boolean.FALSE, 2);
057: setOptional(1);
058: }
059: };
060: ASN1Sequence second = new ASN1Sequence(new ASN1Type[] {
061: ASN1Integer.getInstance(),
062: ASN1Enumerated.getInstance(),
063: ASN1OctetString.getInstance() }) {
064: {
065: setOptional(2);
066: }
067:
068: };
069: ASN1Sequence result = Utils.conjoinSequence(first, second);
070:
071: assertEquals(first.type.length + second.type.length,
072: result.type.length);
073:
074: for (int i = 0; i < first.type.length; i++) {
075: assertEquals(first.type[i], result.type[i]);
076: assertEquals(first.OPTIONAL[i], result.OPTIONAL[i]);
077: assertEquals(first.DEFAULT[i], result.DEFAULT[i]);
078: }
079:
080: int index = first.type.length;
081: for (int i = 0; i < second.type.length; i++) {
082: assertEquals(second.type[i], result.type[i + index]);
083: assertEquals(second.OPTIONAL[i], result.OPTIONAL[i + index]);
084: assertEquals(second.DEFAULT[i], result.DEFAULT[i + index]);
085: }
086:
087: result = Utils.conjoinSequence(first, null);
088:
089: assertEquals(first.type.length, result.type.length);
090: for (int i = 0; i < first.type.length; i++) {
091: assertEquals(first.type[i], result.type[i]);
092: assertEquals(first.OPTIONAL[i], result.OPTIONAL[i]);
093: assertEquals(first.DEFAULT[i], result.DEFAULT[i]);
094: }
095:
096: result = Utils.conjoinSequence(null, second);
097: assertEquals(second.type.length, result.type.length);
098: for (int i = 0; i < second.type.length; i++) {
099: assertEquals(second.type[i], result.type[i]);
100: assertEquals(second.OPTIONAL[i], result.OPTIONAL[i]);
101: assertEquals(second.DEFAULT[i], result.DEFAULT[i]);
102: }
103: }
104:
105: public void test_getString() {
106: assertEquals("", Utils.getString(null));
107:
108: assertEquals(testStr, Utils.getString(testStr));
109: assertEquals(testStr, Utils.getString(testByteArray));
110: assertEquals(testStr, Utils.getString(testCharArray));
111:
112: try {
113: Utils.getString(new Object());
114: fail("ClassCastException expected here.");
115: } catch (ClassCastException e) {
116: //expected
117: }
118: }
119:
120: public void test_getBytes() {
121: byte[] bytes = Utils.getBytes(null);
122: assertTrue((bytes instanceof byte[]) && (bytes.length == 0));
123:
124: assertTrue(Arrays
125: .equals(testByteArray, Utils.getBytes(testStr)));
126: assertEquals(testByteArray, Utils.getBytes(testByteArray));
127: assertTrue(Arrays.equals(testByteArray, Utils
128: .getBytes(testCharArray)));
129:
130: try {
131: Utils.getBytes(new Object());
132: fail("ClassCastException expected here.");
133: } catch (ClassCastException e) {
134: //expected
135: }
136: }
137:
138: public void test_getCharArray() {
139: char[] chars = Utils.getCharArray(null);
140: assertTrue((chars instanceof char[]) && (chars.length == 0));
141:
142: assertTrue(Arrays.equals(testCharArray, Utils
143: .getCharArray(testStr)));
144: assertTrue(Arrays.equals(testCharArray, Utils
145: .getCharArray(testByteArray)));
146: assertEquals(testCharArray, Utils.getCharArray(testCharArray));
147:
148: try {
149: Utils.getCharArray(new Object());
150: fail("ClassCastException expected here.");
151: } catch (ClassCastException e) {
152: //expected
153: }
154: }
155:
156: }
|