1 64 65 package com.jcorporate.expresso.core.security.filters; 66 67 import com.jcorporate.expresso.core.misc.ReusableChar; 68 import com.jcorporate.expresso.services.test.CommandLineParser; 69 import com.jcorporate.expresso.services.test.ExpressoTestCase; 70 import junit.framework.TestSuite; 71 72 73 79 public class FilterTreeTest 80 extends ExpressoTestCase { 81 private FilterTree f = null; 82 83 public FilterTreeTest(String testName) 84 throws Exception { 85 super(testName); 86 } 87 88 public static void main(String [] args) 89 throws Exception { 90 91 CommandLineParser.parseCommandLine(args); 93 junit.textui.TestRunner.run(suite()); 94 } 95 96 public static junit.framework.Test suite() 97 throws Exception { 98 return new TestSuite(FilterTreeTest.class); 99 } 100 101 public void setUp() 102 throws Exception { 103 f = new FilterTree(); 104 f.addFilterString("&", "&"); 105 f.addFilterString("&", "&"); 106 f.addFilterString("<", "<"); 107 f.addFilterString("<", "<"); 108 f.addFilterString(">", ">"); 109 f.addFilterString(">", ">"); 110 } 111 112 115 public void testTreeBuild() 116 throws Exception { 117 118 FilterTreeNode n = f.getRootNode(); 121 assertTrue("& in root Elements", 122 n.subnodeExists(new ReusableChar('&'))); 123 assertTrue("< in root Elements", 124 n.subnodeExists(new ReusableChar('&'))); 125 assertTrue("Replacement For '<' == <", 126 n.getSubnode(new ReusableChar('<')).getReplacementString().equals("<")); 127 128 FilterTreeNode child = n.getSubnode(new ReusableChar('&')); 129 assertTrue("Replacement For '&' == &", 130 child.getReplacementString().equals("&")); 131 assertTrue("a subnode exists", 132 child.subnodeExists(new ReusableChar('a'))); 133 child = child.getSubnode(new ReusableChar('a')); 134 assertTrue("a has no replacemant", 135 child.getReplacementString() == null); 136 child = child.getSubnode(new ReusableChar('m')); 137 assertTrue("m has no replacemant", 138 child.getReplacementString() == null); 139 child = child.getSubnode(new ReusableChar('p')); 140 assertTrue("p has no replacemant", 141 child.getReplacementString() == null); 142 child = child.getSubnode(new ReusableChar(';')); 143 144 assertTrue("; has replacemant &", 145 child.getReplacementString().equals("&")); 146 } 147 148 public void testTreeFilter() 149 throws Exception { 150 f.addFilterString("abc", "abcd"); 151 152 final String [] filterTests = { 153 "", "<", "&", "to bad for me!", "A < B", "&A < &amB", 154 "ab < abc", "ab < ABc", "a<", "B&G", ">1", "&" 155 }; 156 final String [] expectedResults = { 157 "", "<", "&", "to bad for me!", "A < B", 158 "&A < &amB", "ab < abcd", "ab < abcd", "a<", "B&G", ">1", 159 "&amp" 160 }; 161 162 for (int i = 0; i < filterTests.length; i++) { 163 try { 164 String result = f.replaceFilter(filterTests[i].toCharArray()); 165 assertTrue("Filter Test # " + Integer.toString(i) + " Got Result " + 166 result, result.equals(expectedResults[i])); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 fail("Error: " + e.getMessage() + ". i = " + i); 170 } 171 } 172 } 173 } | Popular Tags |