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: * @author Hugo Beilis
019: * @author Leonardo Soler
020: * @author Gabriel Miretti
021: * @version 1.0
022: */package org.apache.harmony.jndi.tests.javax.naming.ldap.whitebox;
023:
024: import java.util.ArrayList;
025: import java.util.Enumeration;
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Properties;
029: import javax.naming.CompositeName;
030: import javax.naming.CompoundName;
031: import javax.naming.InvalidNameException;
032: import javax.naming.ldap.LdapName;
033: import javax.naming.ldap.Rdn;
034: import javax.naming.ldap.SortKey;
035: import junit.framework.TestCase;
036:
037: /**
038: * <p>This class test is made to test all cases of package where the coverage was not 100%.</p>
039: * <p>We are going to find here a lot cases from different classes, notice here that the conventional structure
040: * followed in the rest of the project is applied here.</p>
041: *
042: */
043: public class TestLdapNameWhiteBoxDevelopment extends TestCase {
044:
045: public static void main(String[] args) {
046: }
047:
048: public TestLdapNameWhiteBoxDevelopment(String name) {
049: super (name);
050: }
051:
052: protected void setUp() throws Exception {
053: super .setUp();
054: }
055:
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: /**
061: * <p>Test method for 'javax.naming.ldap.LdapName.add(int, String)'</p>
062: * <p>Here we are testing if this method adds a single component at a specified position within this LDAP name.</p>
063: * <p>Here we are testing if a naming exception is thrown if a name is trying of be added.</p>
064: */
065: public void testAddAll() {
066: try {
067: LdapName x = new LdapName("t=test");
068: x.addAll(0, new CompositeName());
069: assertNotNull(x);
070: } catch (InvalidNameException e) {
071: fail("Failed with:" + e);
072: }
073:
074: }
075:
076: /**
077: * <p>Test method for 'javax.naming.ldap.LdapName.add(int, String)'</p>
078: * <p>Here we are testing if this method adds a single component at a specified position within this LDAP name.</p>
079: * <p>Here we are testing if a naming exception is thrown if a name is trying of be added.</p>
080: */
081: public void testAddAll001() {
082: try {
083: LdapName x = new LdapName("t=test");
084: x.addAll(1, new CompositeName());
085: assertNotNull(x);
086: } catch (InvalidNameException e) {
087: fail("Failed with:" + e);
088: }
089:
090: }
091:
092: /**
093: * <p>Test method for 'javax.naming.ldap.LdapName.add(int, String)'</p>
094: * <p>Here we are testing if this method adds a single component at a specified position within this LDAP name.</p>
095: * <p>Here we are testing if a naming exception is thrown if a name is trying of be added.</p>
096: */
097: public void testAddAll002() {
098: try {
099: LdapName x = new LdapName("t=test");
100: x.addAll(1, new CompoundName("", new Properties()));
101: assertNotNull(x);
102: } catch (InvalidNameException e) {
103: fail("Failed with:" + e);
104: }
105:
106: }
107:
108: /**
109: * <p>Test method for 'javax.naming.ldap.LdapName.add(int, String)'</p>
110: * <p>Here we are testing if this method adds a single component at a specified position within this LDAP name.</p>
111: * <p>Here we are testing if a naming exception is thrown if a name is trying of be added.</p>
112: */
113: public void testAddAll003() {
114: try {
115: LdapName x = new LdapName("t=test");
116: x.addAll(1, new CompositeName("/"));
117: assertNotNull(x);
118: } catch (InvalidNameException e) {
119: fail("Failed with:" + e);
120: }
121:
122: }
123:
124: /**
125: * <p>Test method for 'javax.naming.ldap.LdapName.clone()'</p>
126: * <p>Here we are testing if a clone of an object of LdapName is equal to the original.</p>
127: * <p>The expected result in this case is true.</p>
128: */
129: public void testClone001() {
130:
131: LdapName ln;
132: try {
133: List<Rdn> lista = new ArrayList<Rdn>();
134: Rdn x = new Rdn("t", " ");
135: lista.add(x);
136: ln = new LdapName(lista);
137: LdapName cloned = (LdapName) ln.clone();
138: assertNotNull(cloned);
139: assertEquals(cloned, ln);
140: } catch (InvalidNameException e) {
141: fail("Failed with:" + e);
142: }
143:
144: }
145:
146: /**
147: * <p>Test method for 'javax.naming.ldap.LdapName.clone()'</p>
148: * <p>Here we are testing if a clone of an object of LdapName is equal to the original.</p>
149: * <p>The expected result in this case is true.</p>
150: */
151: public void testClone002() {
152:
153: LdapName ln;
154: try {
155: List<Rdn> lista = new ArrayList<Rdn>();
156: Rdn x = new Rdn("t", "asd");
157: lista.add(x);
158: ln = new LdapName(lista);
159: LdapName cloned = (LdapName) ln.clone();
160: assertNotNull(cloned);
161: assertTrue(cloned.equals(ln));
162: assertEquals(cloned.getRdns().hashCode(), ln.getRdns()
163: .hashCode());
164: } catch (InvalidNameException e) {
165: fail("Failed with:" + e);
166: } catch (ClassCastException e) {
167: fail("Failed with:" + e);
168: }
169:
170: }
171:
172: /**
173: * <p>Test method for 'javax.naming.ldap.LdapName.compareTo(Object)'</p>
174: * <p>Here we are testing if this method compares this LdapName with the specified Object for order.</p>
175: * <p>The expected result is a classcastException.</p>
176: */
177: public void testCompareTo001() {
178:
179: try {
180: LdapName ln = new LdapName("t=test,cn=common");
181: Rdn tocomp = null;
182: int i;
183: i = ln.compareTo(tocomp);
184: fail("The string is null.");
185: } catch (ClassCastException e) {
186:
187: } catch (Throwable e) {
188: fail("Failed with: " + e);
189:
190: }
191:
192: }
193:
194: /**
195: * <p>Test method for 'javax.naming.ldap.LdapName.compareTo(Object)'</p>
196: * <p>Here we are testing if this method compares this LdapName with the specified Object for order.</p>
197: * <p>The expected result is a classcastException.</p>
198: */
199: public void testCompareTo002() {
200:
201: try {
202: LdapName ln = new LdapName("t=test,cn=common");
203: int i;
204: i = ln.compareTo(new SortKey(""));
205: fail("The string is null.");
206: } catch (ClassCastException e) {
207:
208: } catch (Throwable e) {
209: fail("Failed with: " + e);
210:
211: }
212:
213: }
214:
215: /**
216: * <p>Test method for 'javax.naming.ldap.LdapName.endsWith(Name)'</p>
217: * <p>Here we are testing if this method determines whether this LDAP name ends with a specified LDAP name suffix.</p>
218: * <p>The expected result is a false.</p>
219: */
220: public void testEndsWithName001() {
221:
222: try {
223: LdapName ln = new LdapName("t=test,t=test");
224: LdapName t = new LdapName("t=test,t=etest");
225: assertFalse(ln.endsWith(t.getRdns()));
226: } catch (InvalidNameException e) {
227: fail("Failed with:" + e);
228: }
229:
230: }
231:
232: /**
233: * <p>Test method for 'javax.naming.ldap.LdapName.getAll()'</p>
234: * <p>Here we are testing if this method retrieves the components of this name as an enumeration of strings.</p>
235: * <p>The expected result is if an empty name returns a non-null enumeration.</p>
236: */
237: public void testGetAll001() {
238:
239: try {
240: LdapName ln = new LdapName("");
241: Enumeration<String> x = ln.getAll();
242: assertNotNull(x);
243: assertEquals(x.hasMoreElements(), ln.getRdns().iterator()
244: .hasNext());
245: } catch (InvalidNameException e) {
246:
247: }
248: }
249:
250: /**
251: * <p>Test method for 'javax.naming.ldap.LdapName.startsWith(List<Rdn>)'</p>
252: * <p>Here we are testing if this method determines whether the specified RDN sequence forms a prefix of this LDAP name.</p>
253: * <p>The expected result is false.</p>
254: */
255: public void testStartsWithListOfRdn004() {
256:
257: LinkedList<Rdn> test = new LinkedList<Rdn>();
258: try {
259: test.add(new Rdn("t=test"));
260: LdapName x = new LdapName("t=t");
261: assertFalse(x.startsWith(test));
262: } catch (InvalidNameException e) {
263: fail("Failed with:" + e);
264: }
265:
266: }
267:
268: /**
269: * <p>Test method for 'javax.naming.ldap.LdapName.equals(Object)'</p>
270: * <p>Here we are testing if this method determines whether the specified object is equal to the originaly one.</p>
271: * <p>The expected result is false.</p>
272: */
273: public void testequals() {
274: LinkedList<Rdn> test = new LinkedList<Rdn>();
275: LinkedList<Rdn> test1 = new LinkedList<Rdn>();
276: try {
277: test.add(new Rdn("t=test"));
278: test1.add(new Rdn("t=test"));
279: test1.add(new Rdn("t=test"));
280: LdapName x = new LdapName(test);
281: LdapName y = new LdapName(test1);
282: assertFalse(x.equals(y));
283: } catch (InvalidNameException e) {
284: fail("Failed with:" + e);
285: }
286: }
287:
288: }
|