This project has retired. For details please refer to its Attic page.
KeyGeneratorTest xref
View Javadoc
1   /*
2    * Copyright 2001-2009 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *      http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.apache.juddi.keygen;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.apache.commons.configuration.ConfigurationException;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.juddi.config.Property;
24  import org.apache.juddi.model.UddiEntityPublisher;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
30   */
31  public class KeyGeneratorTest 
32  {
33  	private Log logger = LogFactory.getLog(this.getClass());
34  	
35  
36  	@Test() 
37  	public void testNonExisitingKeyGeneratorClass()
38  	{
39  		System.setProperty(Property.JUDDI_KEYGENERATOR, "org.apache.juddi.keygen.FooGenerator");
40  		try {
41  			KeyGenerator keyGenerator = KeyGeneratorFactory.forceNewKeyGenerator();
42  			System.out.println("Generator = " + keyGenerator.getClass());
43                          if (!keyGenerator.getClass().getCanonicalName().equals(KeyGeneratorFactory.DEFAULT_IMPL))
44                              Assert.fail("This should have thrown an exception because this class does not exist.");
45  		} catch (Exception e) {
46  			String message = e.getMessage();
47  			Assert.assertEquals("The specified Key Generator class 'org.apache.juddi.keygen.FooGenerator' was not found on classpath.", message);
48  		}
49                  System.clearProperty(Property.JUDDI_KEYGENERATOR);
50  	}
51  	
52  	@Test() 
53  	public void testGeneratorInterface()
54  	{
55  		System.setProperty(Property.JUDDI_KEYGENERATOR, "org.apache.juddi.keygen.KeyGenerator");
56  		try {
57  			KeyGenerator keyGenerator = KeyGeneratorFactory.forceNewKeyGenerator();
58  			System.out.println("Generator = " + keyGenerator.getClass());
59                          Assert.fail("This should have thrown an exception because you cannot instantiate an interface.");
60  		} catch (Exception e) {
61  			String message = e.getMessage();
62  			Assert.assertEquals("The specified Key Generator class 'org.apache.juddi.keygen.KeyGenerator' cannot be instantiated.", message);
63  		}
64                  System.clearProperty(Property.JUDDI_KEYGENERATOR);
65  	}
66  	/**
67  	 * The DefaultKeyGenerator
68  	 * @throws ConfigurationException
69  	 */
70  	@Test
71  	public void testDefaultKeyGenerator()
72  	{
73  		System.setProperty(Property.JUDDI_KEYGENERATOR, "org.apache.juddi.keygen.DefaultKeyGenerator");
74  		try {
75  			KeyGenerator keyGenerator = KeyGeneratorFactory.forceNewKeyGenerator();
76  			Assert.assertEquals(org.apache.juddi.keygen.DefaultKeyGenerator.class, keyGenerator.getClass());
77  			String key = keyGenerator.generate(null);
78  			Assert.assertNotNull(key);
79  			Assert.assertTrue(key.startsWith("uddi:juddi.apache.org"));
80  			UddiEntityPublisher publisher = new UddiEntityPublisher();
81  			
82  			List<String> keyGeneratorKeys = new ArrayList<String>();
83  			keyGeneratorKeys.add("uddi:mydomain.org:keyGenerator");
84  			publisher.setKeyGeneratorKeys(keyGeneratorKeys);
85  			String key2 = keyGenerator.generate(publisher);
86  			Assert.assertNotNull(key2);
87  			Assert.assertTrue(key2.startsWith("uddi:mydomain.org"));
88  			
89  		} catch (Exception e) {
90  			logger.error(e.getMessage(),e);
91  			Assert.fail("unexpected");
92  		}
93                  System.clearProperty(Property.JUDDI_KEYGENERATOR);
94  	}
95  	
96  }