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.ws.security.components.crypto;
21  
22  /**
23   *
24   */
25  public class X509NameTokenizerTest extends org.junit.Assert {
26  
27      @org.junit.Test
28      public void 
29      testEmptyX509NameTokenizer() {
30          checkEmpty(new X509NameTokenizer(""));
31          checkEmpty(new X509NameTokenizer("  "));
32          checkEmpty(new X509NameTokenizer(" \t \n  \r\n"));
33      }
34      
35      @org.junit.Test
36      public void 
37      testWellFormedX509NameTokenizer() {
38          checkTokenizer(
39              new X509NameTokenizer("foo"), 
40              new String[] { "foo" }
41          );
42          checkTokenizer(
43              new X509NameTokenizer(" foo   "), 
44              new String[] { "foo" }
45          );
46          checkTokenizer(
47              new X509NameTokenizer(" foo,   "), 
48              new String[] { "foo", "" }
49          );
50          checkTokenizer(
51              new X509NameTokenizer(" foo\\,   "), 
52              new String[] { "foo\\,"}
53          );
54          checkTokenizer(
55              new X509NameTokenizer(" foo\\,   bar  "), 
56              new String[] { "foo\\,   bar"}
57          );
58          checkTokenizer(
59              new X509NameTokenizer(" \"foo,\"   "), 
60              new String[] { "\"foo,\""}
61          );
62          checkTokenizer(
63              new X509NameTokenizer("foo, bar"), 
64              new String[] { "foo", "bar"}
65          );
66          checkTokenizer(
67              new X509NameTokenizer("\"foo bar\", gnu gnat"), 
68              new String[] { "\"foo bar\"", "gnu gnat"}
69          );
70          checkTokenizer(
71              new X509NameTokenizer("foo\\ "), 
72              new String[] { "foo\\ "}
73          );
74          checkTokenizer(
75              new X509NameTokenizer("foo\\\\ "), 
76              new String[] { "foo\\\\"}
77          );
78      }
79      
80      private void
81      checkEmpty(
82          final X509NameTokenizer tokenizer
83      ) {
84          checkTokenizer(
85              tokenizer, new String[0]
86          );
87      }
88      
89      private void
90      checkTokenizer(
91          final X509NameTokenizer tokenizer,
92          final String[] expected
93      ) {
94          for (int i = 0;  i < expected.length;  ++i) {
95              assertTrue(tokenizer.hasMoreTokens());
96              assertEquals(tokenizer.nextToken(), expected[i]);
97          }
98          assertTrue(!tokenizer.hasMoreTokens());
99          assertEquals(tokenizer.nextToken(), "");
100     }
101 }