[NEw] New util CodeGenerator
This commit is contained in:
parent
a60c300d5c
commit
c42e99150d
|
@ -0,0 +1,36 @@
|
||||||
|
package li.strolch.utils;
|
||||||
|
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates code which should be easily readable as problematic letters e.g. 0, o, O, i, I, l are left out
|
||||||
|
*
|
||||||
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
|
*/
|
||||||
|
public class CodeGenerator {
|
||||||
|
|
||||||
|
private static final String ALPHA_NUMERIC_UPPER = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
||||||
|
private static final String ALPHA_NUMERIC_LOWER_UPPER = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz";
|
||||||
|
|
||||||
|
public static String alphaNumericUpper(int len) {
|
||||||
|
|
||||||
|
SecureRandom rnd = new SecureRandom();
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder(len);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
sb.append(ALPHA_NUMERIC_UPPER.charAt(rnd.nextInt(ALPHA_NUMERIC_UPPER.length())));
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String alphaNumericLowerUpper(int len) {
|
||||||
|
|
||||||
|
SecureRandom rnd = new SecureRandom();
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder(len);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
sb.append(ALPHA_NUMERIC_LOWER_UPPER.charAt(rnd.nextInt(ALPHA_NUMERIC_LOWER_UPPER.length())));
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package li.strolch.utils;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class CodeGeneratorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateGeneratorLowerUpper() {
|
||||||
|
String code = CodeGenerator.alphaNumericLowerUpper(12);
|
||||||
|
assertEquals(12, code.length());
|
||||||
|
assertFalse(code.contains("0"));
|
||||||
|
assertFalse(code.contains("i"));
|
||||||
|
assertFalse(code.contains("I"));
|
||||||
|
assertFalse(code.contains("l"));
|
||||||
|
assertFalse(code.contains("o"));
|
||||||
|
assertFalse(code.contains("O"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldCreateGeneratorUpper() {
|
||||||
|
String code = CodeGenerator.alphaNumericUpper(500);
|
||||||
|
assertEquals(500, code.length());
|
||||||
|
assertFalse(code.contains("0"));
|
||||||
|
assertFalse(code.contains("i"));
|
||||||
|
assertFalse(code.contains("I"));
|
||||||
|
assertFalse(code.contains("l"));
|
||||||
|
assertFalse(code.contains("o"));
|
||||||
|
assertFalse(code.contains("O"));
|
||||||
|
|
||||||
|
for (int i = 0; i < code.length(); i++) {
|
||||||
|
char c = code.charAt(i);
|
||||||
|
assertTrue(Character.isDigit(c) || Character.isUpperCase(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue