验证码产生的源程序
import java.util.*;
public class ValidCode
{
private static String validCode="";
private static int n=0;
public ValidCode()
{
//构造函数
}
public static void toValidCode()
{
String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random ram = new Random();
for (int i = 0; i < n; i++)
{
while (true)
{
int pos = ram.nextInt(str.length());
char tmp = str.charAt(pos);
if (validCode.indexOf(tmp) == -1)
{
validCode = validCode + tmp;
break;
}
else
{
continue;
}
}
}
}
public static void setN(int i)
{
n=i;
}
public int getN()
{
return n;
}
public static String getValidCode()
{
return validCode;
}
public static void main(String args[])
{
setN(20);
toValidCode();
System.out.println(getValidCode());
}
}
Junit测试程序
import junit.framework.*;
/**
* @author 燕红云
*
*/
public class TestValidCode extends TestCase
{
/**
*
*/
public void setUp()
{
}
public TestValidCode(String name)
{
super(name);
}
public void testCase1()
{
ValidCode vc=new ValidCode();
vc.setN(4);
Assert.assertEquals(4, vc.getN());
}
/**
* @param args
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(TestValidCode.class);
}
}