KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > datamodel > CredentialStoreTest


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 JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.logging.Logger JavaDoc;
30
31 import javax.management.AttributeNotFoundException JavaDoc;
32 import javax.management.InvalidAttributeValueException JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
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: 1.8.28.1 $, $Date: 2007/01/13 01:31:09 $
46  */

47 public class CredentialStoreTest extends SettingsFrameworkTestCase {
48
49     protected static Logger JavaDoc logger =
50         Logger.getLogger("org.archive.crawler.datamodel.CredentialTest");
51
52     final public void testCredentials()
53         throws InvalidAttributeValueException JavaDoc, IllegalArgumentException JavaDoc,
54         InvocationTargetException JavaDoc, AttributeNotFoundException JavaDoc, MBeanException JavaDoc,
55         ReflectionException JavaDoc {
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 JavaDoc types = CredentialStore.getCredentialTypes();
63         List JavaDoc 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 JavaDoc i = globalNames.iterator();
70                 i.hasNext();) {
71             store.remove(this.getGlobalSettings(),(String JavaDoc)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 JavaDoc checkContextNames(Iterator JavaDoc i, int size) {
79         List JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>(size);
80         for (; i.hasNext();) {
81             String JavaDoc 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 JavaDoc prefix)
91         throws InvalidAttributeValueException JavaDoc, AttributeNotFoundException JavaDoc,
92         IllegalArgumentException JavaDoc, InvocationTargetException JavaDoc {
93
94         List JavaDoc types = CredentialStore.getCredentialTypes();
95         for (Iterator JavaDoc i = types.iterator(); i.hasNext();) {
96             Class JavaDoc cl = (Class JavaDoc)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 JavaDoc<String JavaDoc> names = new ArrayList JavaDoc<String JavaDoc>(types.size());
103         for (Iterator JavaDoc i = store.iterator(null); i.hasNext();) {
104             names.add(((Credential)i.next()).getName());
105         }
106         getSettingsHandler().writeSettingsObject(context);
107     }
108 }
109
Popular Tags