1 23 24 27 28 package com.sun.enterprise.admin.servermgmt; 29 30 import java.util.Random ; 31 32 import junit.framework.*; 34 import junit.textui.TestRunner; 35 36 public class RepositoryNameValidatorTest extends TestCase 37 { 38 static final char[] INVALID_CHARS = 39 {'/', '\\', ':', '*', '?', '"', '<', '>', '|', ',', '=', 'î', ' ', '&', ';', '[', ']', '{', '}', '(', ')', '%', '$', '^', '!'}; 40 41 static final char[] VALID_CHARS = {'a', '0', '-', '_', '.'}; 42 43 static final int ITERATIONS = 100; 44 45 Validator validator; 46 Random random; 47 48 public void testNull() 49 { 50 testInvalid(null); 51 } 52 53 public void testZeroLength() 54 { 55 testInvalid(""); 56 } 57 58 public void testInvalidChar() 59 { 60 for (int i = 0; i < INVALID_CHARS.length; i++) 61 { 62 testInvalid("" + INVALID_CHARS[i]); 63 } 64 } 65 66 public void testInvalidStr() 67 { 68 for (int i = 0; i < ITERATIONS; i++) 69 { 70 testInvalid("" + 71 INVALID_CHARS[random.nextInt(INVALID_CHARS.length)] + 72 INVALID_CHARS[random.nextInt(INVALID_CHARS.length)]); 73 } 74 } 75 76 public void testValidChar() 77 { 78 for (int i = 0; i < VALID_CHARS.length; i++) 79 { 80 testValid("" + VALID_CHARS[i]); 81 } 82 } 83 84 public void testValidStr() 85 { 86 for (int i = 0; i < ITERATIONS; i++) 87 { 88 testValid("" + 89 VALID_CHARS[random.nextInt(VALID_CHARS.length)] + 90 VALID_CHARS[random.nextInt(VALID_CHARS.length)]); 91 } 92 } 93 94 public void testCombination() 95 { 96 for (int i = 0; i < ITERATIONS; i++) 97 { 98 testInvalid("" + 99 VALID_CHARS[random.nextInt(VALID_CHARS.length)] + 100 INVALID_CHARS[random.nextInt(INVALID_CHARS.length)]); 101 } 102 } 103 104 public RepositoryNameValidatorTest(String name) throws Exception 105 { 106 super(name); 107 } 108 109 protected void setUp() 110 { 111 validator = new RepositoryNameValidator("repository name"); 112 random = new Random (); 113 } 114 115 protected void tearDown() 116 { 117 validator = null; 118 random = null; 119 } 120 121 public static junit.framework.Test suite() 122 { 123 TestSuite suite = new TestSuite(RepositoryNameValidatorTest.class); 124 return suite; 125 } 126 127 public static void main(String args[]) throws Exception 128 { 129 final TestRunner runner= new TestRunner(); 130 final TestResult result = runner.doRun( 131 RepositoryNameValidatorTest.suite(), false); 132 System.exit(result.errorCount() + result.failureCount()); 133 } 134 135 void testInvalid(String invalid) 136 { 137 try 138 { 139 validator.validate(invalid); 140 System.out.println(invalid); 141 Assert.assertTrue(false); 142 } 143 catch (Exception e) 144 { 145 } 147 } 148 149 void testValid(String valid) 150 { 151 try 152 { 153 validator.validate(valid); 154 } 155 catch (Exception e) 156 { 157 Assert.assertTrue(false); 158 } 159 } 160 } 161 | Popular Tags |