1 23 package org.archive.crawler.datamodel; 24 25 import java.lang.reflect.InvocationTargetException ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.logging.Logger ; 30 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.InvalidAttributeValueException ; 33 import javax.management.MBeanException ; 34 import javax.management.ReflectionException ; 35 36 import org.archive.crawler.datamodel.credential.Credential; 37 import org.archive.crawler.settings.CrawlerSettings; 38 import org.archive.crawler.settings.SettingsFrameworkTestCase; 39 40 41 47 public class CredentialStoreTest extends SettingsFrameworkTestCase { 48 49 protected static Logger logger = 50 Logger.getLogger("org.archive.crawler.datamodel.CredentialTest"); 51 52 final public void testCredentials() 53 throws InvalidAttributeValueException , IllegalArgumentException , 54 InvocationTargetException , AttributeNotFoundException , MBeanException , 55 ReflectionException { 56 57 CredentialStore store = (CredentialStore)this.settingsHandler. 58 getOrder().getAttribute(CredentialStore.ATTR_NAME); 59 writeCrendentials(store, this.getGlobalSettings(), "global"); 60 writeCrendentials(store, this.getPerDomainSettings(), "domain"); 61 writeCrendentials(store, this.getPerHostSettings(), "host"); 62 List types = CredentialStore.getCredentialTypes(); 63 List globalNames = checkContextNames(store.iterator( 64 this.getGlobalSettings()), types.size()); 65 checkContextNames(store.iterator(this.getPerDomainSettings()), 66 types.size() * 2 ); 67 checkContextNames(store.iterator(this.getPerHostSettings()), 68 types.size() * 3 ); 69 for (Iterator i = globalNames.iterator(); 70 i.hasNext();) { 71 store.remove(this.getGlobalSettings(),(String )i.next()); 72 } 73 checkContextNames(store.iterator(this.getPerHostSettings()), 75 types.size() * 2); 76 } 77 78 private List checkContextNames(Iterator i, int size) { 79 List <String > names = new ArrayList <String >(size); 80 for (; i.hasNext();) { 81 String name = ((Credential)i.next()).getName(); 82 names.add(name); 83 } 84 logger.info("Added: " + names.toString()); 85 assertTrue("Not enough names, size " + size, size == names.size()); 86 return names; 87 } 88 89 private void writeCrendentials(CredentialStore store, CrawlerSettings context, 90 String prefix) 91 throws InvalidAttributeValueException , AttributeNotFoundException , 92 IllegalArgumentException , InvocationTargetException { 93 94 List types = CredentialStore.getCredentialTypes(); 95 for (Iterator i = types.iterator(); i.hasNext();) { 96 Class cl = (Class )i.next(); 97 Credential c = store.create(context, prefix + "." + cl.getName(), 98 cl); 99 assertNotNull("Failed create of " + cl, c); 100 logger.info("Created " + c.getName()); 101 } 102 List <String > names = new ArrayList <String >(types.size()); 103 for (Iterator i = store.iterator(null); i.hasNext();) { 104 names.add(((Credential)i.next()).getName()); 105 } 106 getSettingsHandler().writeSettingsObject(context); 107 } 108 } 109 | Popular Tags |