KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > util > TestStringTools


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package se.anatom.ejbca.util;
15
16 import junit.framework.TestCase;
17
18 import org.apache.log4j.Logger;
19 import org.ejbca.util.CertTools;
20 import org.ejbca.util.StringTools;
21
22
23 /**
24  * Tests the StringTools class .
25  *
26  * @version $Id: TestStringTools.java,v 1.9 2006/08/02 11:23:21 anatom Exp $
27  */

28 public class TestStringTools extends TestCase {
29     private static Logger log = Logger.getLogger(TestStringTools.class);
30
31     /**
32      * Creates a new TestStringTools object.
33      *
34      * @param name name
35      */

36     public TestStringTools(String JavaDoc name) {
37         super(name);
38     }
39
40     protected void setUp() throws Exception JavaDoc {
41         log.debug(">setUp()");
42         log.debug("<setUp()");
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46         log.debug(">tearDown()");
47         log.debug("<tearDown()");
48     }
49
50     /**
51      * tests stipping whitespace
52      *
53      * @throws Exception error
54      */

55     public void test01StripWhitespace() throws Exception JavaDoc {
56         log.debug(">test01StripWhitespace()");
57         String JavaDoc test = " foo \t bar \r\n\r\n \f\f\f quu x ";
58         assertEquals("foobarquux", StringTools.stripWhitespace(test));
59         log.debug(">test01StripWhitespace()");
60     }
61
62     public void test02IpStringToOctets() throws Exception JavaDoc {
63         log.debug(">test02IpStringToOctets()");
64         String JavaDoc ip = "23.34.45.167";
65         byte[] octs = StringTools.ipStringToOctets(ip);
66         for (int i=0;i<octs.length;i++) {
67             log.debug("octs["+i+"]="+(int)octs[i]);
68         }
69         log.debug(">test02IpStringToOctets()");
70     }
71     public void test03Strip() throws Exception JavaDoc {
72         log.debug(">test03Strip()");
73         String JavaDoc strip1 = "foo$bar:far%";
74         String JavaDoc stripped = StringTools.strip(strip1);
75         assertTrue("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1));
76         assertEquals("String not stripped correctly!", stripped, "foo/bar:far/");
77         log.debug("<test03Strip()");
78     }
79     public void test04Strip() throws Exception JavaDoc {
80         log.debug(">test04Strip()");
81         String JavaDoc strip1 = "CN=foo, O=Acme\\, Inc, OU=;\\/<>bar";
82         String JavaDoc stripped = StringTools.strip(strip1);
83         assertTrue("String has chars that should be stripped!", StringTools.hasSqlStripChars(strip1));
84         assertEquals("String not stripped correctly!", stripped, "CN=foo, O=Acme\\, Inc, OU=/////bar");
85         log.debug("<test04Strip()");
86     }
87     public void testBase64() throws Exception JavaDoc {
88         String JavaDoc s1 = "C=SE, O=abc, CN=def";
89         String JavaDoc b1 = StringTools.putBase64String(s1);
90         String JavaDoc s2 = StringTools.getBase64String(b1);
91         assertEquals(s2,s1);
92
93         s1 = "C=SE, O=ÅÄÖ, CN=åäö";
94         b1 = StringTools.putBase64String(s1);
95         s2 = StringTools.getBase64String(b1);
96         assertEquals(s2,s1);
97     }
98     public void testObfuscate() throws Exception JavaDoc {
99         String JavaDoc obf = StringTools.obfuscate("foo123");
100         String JavaDoc deobf = StringTools.deobfuscate(obf);
101         assertEquals("foo123", deobf);
102     }
103     public void testPbe() throws Exception JavaDoc {
104         CertTools.installBCProvider();
105         String JavaDoc enc = StringTools.pbeEncryptStringWithSha256Aes192("foo123");
106         String JavaDoc dec = StringTools.pbeDecryptStringWithSha256Aes192(enc);
107         assertEquals("foo123", dec);
108     }
109 }
110
Popular Tags