View Javadoc

    1   /* CredentialStoreTest
    2    *
    3    * Created on Apr 1, 2004
    4    *
    5    * Copyright (C) 2004 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22   */
    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  /***
    42   * Test add, edit, delete from credential store.
    43   *
    44   * @author stack
    45   * @version $Revision: 4668 $, $Date: 2006-09-26 21:49:01 +0000 (Tue, 26 Sep 2006) $
    46   */
    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 /*This should be global + domain*/);
    67          checkContextNames(store.iterator(this.getPerHostSettings()),
    68              types.size() * 3 /*This should be global + domain + host*/);
    69          for (Iterator i = globalNames.iterator();
    70                  i.hasNext();) {
    71              store.remove(this.getGlobalSettings(),(String)i.next());
    72          }
    73          // Should be only host and domain objects at deepest scope.
    74          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 }