1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.jdkim;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.james.jdkim.api.PublicKeyRecordRetriever;
27  import org.apache.james.jdkim.exceptions.FailException;
28  import org.apache.james.jdkim.exceptions.PermFailException;
29  import org.apache.james.jdkim.exceptions.TempFailException;
30  import org.apache.james.jdkim.impl.MultiplexingPublicKeyRecordRetriever;
31  
32  import junit.framework.TestCase;
33  
34  public class MultiplexingPublicKeyRecordRetrieverTest extends TestCase {
35  
36      private PublicKeyRecordRetriever myMethodRetriever = new PublicKeyRecordRetriever() {
37  
38          public List getRecords(CharSequence methodAndOption,
39                  CharSequence selector, CharSequence token)
40                  throws TempFailException, PermFailException {
41              List l = new ArrayList();
42              l.add(selector.toString());
43              l.add(token.toString());
44              return l;
45          }
46  
47      };
48  
49      public void testMultiplexingPublicKeyRecordRetriever() {
50          MultiplexingPublicKeyRecordRetriever pkrr = new MultiplexingPublicKeyRecordRetriever();
51          try {
52              pkrr.getRecords("method", "selector", "token");
53              fail("method is unknown");
54          } catch (FailException e) {
55          }
56      }
57  
58      public void testMultiplexingPublicKeyRecordRetrieverStringPublicKeyRecordRetriever()
59              throws TempFailException, PermFailException {
60          MultiplexingPublicKeyRecordRetriever pkrr = new MultiplexingPublicKeyRecordRetriever(
61                  "mymethod", myMethodRetriever);
62          check(pkrr, "mymethod");
63      }
64  
65      private void check(MultiplexingPublicKeyRecordRetriever pkrr, String method)
66              throws TempFailException, PermFailException {
67          List l = pkrr.getRecords(method, "selector", "token");
68          Iterator i = l.iterator();
69          assertEquals("selector", i.next());
70          assertEquals("token", i.next());
71          try {
72              l = pkrr.getRecords("anothermethod", "selector", "token");
73              fail("anothermethod is not declared");
74          } catch (FailException e) {
75          }
76      }
77  
78      public void testAddRetriever() throws TempFailException, PermFailException {
79          MultiplexingPublicKeyRecordRetriever pkrr = new MultiplexingPublicKeyRecordRetriever();
80          pkrr.addRetriever("mymethod", myMethodRetriever);
81          check(pkrr, "mymethod");
82      }
83  
84      public void testAddRetrieverWithOptions() throws TempFailException,
85              PermFailException {
86          MultiplexingPublicKeyRecordRetriever pkrr = new MultiplexingPublicKeyRecordRetriever();
87          pkrr.addRetriever("mymethod", myMethodRetriever);
88          check(pkrr, "mymethod/option");
89      }
90  
91  }