001: /* CredentialStoreTest
002: *
003: * Created on Apr 1, 2004
004: *
005: * Copyright (C) 2004 Internet Archive.
006: *
007: * This file is part of the Heritrix web crawler (crawler.archive.org).
008: *
009: * Heritrix is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU Lesser Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * any later version.
013: *
014: * Heritrix is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser Public License
020: * along with Heritrix; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: package org.archive.crawler.datamodel;
024:
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.logging.Logger;
030:
031: import javax.management.AttributeNotFoundException;
032: import javax.management.InvalidAttributeValueException;
033: import javax.management.MBeanException;
034: import javax.management.ReflectionException;
035:
036: import org.archive.crawler.datamodel.credential.Credential;
037: import org.archive.crawler.settings.CrawlerSettings;
038: import org.archive.crawler.settings.SettingsFrameworkTestCase;
039:
040: /**
041: * Test add, edit, delete from credential store.
042: *
043: * @author stack
044: * @version $Revision: 4668 $, $Date: 2006-09-26 21:49:01 +0000 (Tue, 26 Sep 2006) $
045: */
046: public class CredentialStoreTest extends SettingsFrameworkTestCase {
047:
048: protected static Logger logger = Logger
049: .getLogger("org.archive.crawler.datamodel.CredentialTest");
050:
051: final public void testCredentials()
052: throws InvalidAttributeValueException,
053: IllegalArgumentException, InvocationTargetException,
054: AttributeNotFoundException, MBeanException,
055: ReflectionException {
056:
057: CredentialStore store = (CredentialStore) this .settingsHandler
058: .getOrder().getAttribute(CredentialStore.ATTR_NAME);
059: writeCrendentials(store, this .getGlobalSettings(), "global");
060: writeCrendentials(store, this .getPerDomainSettings(), "domain");
061: writeCrendentials(store, this .getPerHostSettings(), "host");
062: List types = CredentialStore.getCredentialTypes();
063: List globalNames = checkContextNames(store.iterator(this
064: .getGlobalSettings()), types.size());
065: checkContextNames(store.iterator(this .getPerDomainSettings()),
066: types.size() * 2 /*This should be global + domain*/);
067: checkContextNames(store.iterator(this .getPerHostSettings()),
068: types.size() * 3 /*This should be global + domain + host*/);
069: for (Iterator i = globalNames.iterator(); i.hasNext();) {
070: store.remove(this .getGlobalSettings(), (String) i.next());
071: }
072: // Should be only host and domain objects at deepest scope.
073: checkContextNames(store.iterator(this .getPerHostSettings()),
074: types.size() * 2);
075: }
076:
077: private List checkContextNames(Iterator i, int size) {
078: List<String> names = new ArrayList<String>(size);
079: for (; i.hasNext();) {
080: String name = ((Credential) i.next()).getName();
081: names.add(name);
082: }
083: logger.info("Added: " + names.toString());
084: assertTrue("Not enough names, size " + size, size == names
085: .size());
086: return names;
087: }
088:
089: private void writeCrendentials(CredentialStore store,
090: CrawlerSettings context, String prefix)
091: throws InvalidAttributeValueException,
092: AttributeNotFoundException, IllegalArgumentException,
093: InvocationTargetException {
094:
095: List types = CredentialStore.getCredentialTypes();
096: for (Iterator i = types.iterator(); i.hasNext();) {
097: Class cl = (Class) i.next();
098: Credential c = store.create(context, prefix + "."
099: + cl.getName(), cl);
100: assertNotNull("Failed create of " + cl, c);
101: logger.info("Created " + c.getName());
102: }
103: List<String> names = new ArrayList<String>(types.size());
104: for (Iterator i = store.iterator(null); i.hasNext();) {
105: names.add(((Credential) i.next()).getName());
106: }
107: getSettingsHandler().writeSettingsObject(context);
108: }
109: }
|