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;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import javax.naming.NamingException;
024: import javax.naming.directory.BasicAttribute;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1TestUtils;
029: import org.apache.harmony.jndi.provider.ldap.asn1.LdapASN1Constant;
030: import org.apache.harmony.jndi.provider.ldap.asn1.Utils;
031:
032: public class LdapAttributeTest extends TestCase {
033: public void test_encode_decode() throws NamingException {
034: String id = "test";
035: byte[] v1 = Utils.getBytes("value1");
036: byte[] v2 = Utils.getBytes("value2");
037: byte[] v3 = Utils.getBytes("value3");
038: LdapAttribute attr = new LdapAttribute(id, null);
039: attr.add(v1);
040: attr.add(v2);
041: attr.add(v3);
042: ASN1TestUtils.checkEncode(attr, LdapASN1Constant.Attribute);
043:
044: Object[] encoded = new Object[2];
045: attr.encodeValues(encoded);
046: LdapAttribute decoded = new LdapAttribute("", null);
047: decoded.decodeValues(encoded);
048:
049: assertEquals(attr.getID(), decoded.getID());
050:
051: }
052:
053: public void test_constructor_LAttribute() throws Exception {
054: BasicAttribute attr = new BasicAttribute("cn");
055: attr.add("test");
056: attr.add("harmony");
057: LdapAttribute la = new LdapAttribute(attr, null);
058:
059: ASN1TestUtils.checkEncode(la, LdapASN1Constant.Attribute);
060:
061: }
062:
063: public void test_binary_attribute() throws Exception {
064: String id = "photo";
065: List<byte[]> valueList = new ArrayList<byte[]>();
066: byte[] bs = new byte[] { 0, 1, 2, 3, 4, 5 };
067: valueList.add(bs);
068: bs = Utils.getBytes("value");
069: valueList.add(bs);
070:
071: Object[] values = new Object[] { Utils.getBytes(id), valueList };
072:
073: LdapAttribute la = new LdapAttribute();
074: // 'photo' is binary attribute
075: la.decodeValues(values);
076:
077: for (int i = 0; i < la.size(); ++i) {
078: assertTrue(la.get(i) instanceof byte[]);
079: }
080:
081: id = "cn";
082: values = new Object[] { Utils.getBytes(id), valueList };
083: la = new LdapAttribute();
084: // 'cn' is not binary attribute, should return string value
085: la.decodeValues(values);
086:
087: for (int i = 0; i < la.size(); ++i) {
088: assertTrue(la.get() instanceof String);
089: }
090:
091: id = "cn;binary";
092: values = new Object[] { Utils.getBytes(id), valueList };
093: la = new LdapAttribute();
094: // 'cn;binary' is binary attribute
095: la.decodeValues(values);
096:
097: for (int i = 0; i < la.size(); ++i) {
098: assertTrue(la.get() instanceof byte[]);
099: }
100: }
101: }
|