Browse Source

added tests, overhauled implementation of Util.hex

drop_old_mail
Stephan Richter 3 years ago
parent
commit
25f435677e
  1. 10
      src/main/java/de/srsoftware/widerhall/Util.java
  2. 44
      src/test/java/de/srsoftware/widerhall/UtilTest.java

10
src/main/java/de/srsoftware/widerhall/Util.java

@ -1,16 +1,12 @@ @@ -1,16 +1,12 @@
package de.srsoftware.widerhall;
import de.srsoftware.examples.translations.App;
import de.srsoftware.tools.translations.Translation;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Util {
@ -51,8 +47,10 @@ public class Util { @@ -51,8 +47,10 @@ public class Util {
return buf.toString();
}
private static String hex(byte b){
return (b<16 ? "0" : "") + Integer.toHexString(b);
public static String hex(int b){
int lower = b & 0x0F;
int upper = (b & 0xF0) >> 4;
return (char)(upper < 10 ? '0'+upper : 'A'+upper-10)+""+(char)(lower < 10 ? '0'+lower : 'A'+lower-10);
}
public static String t(String tx, Object ... fills){

44
src/test/java/de/srsoftware/widerhall/UtilTest.java

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
package de.srsoftware.widerhall;
import junit.framework.TestCase;
import java.util.Map;
import java.util.TreeMap;
public class UtilTest extends TestCase {
public void testUrlEncode() {
TreeMap<String,Object> map = new TreeMap<>();
map.put("a",1);
map.put("b",2);
map.put("c","&");
map.put("d","=");
assertEquals("a=1&b=2&c=%26&d=%3D",Util.urlEncode(map));
}
public void testHex(){
assertEquals("00",Util.hex(0));
assertEquals("09",Util.hex(9));
assertEquals("0A",Util.hex(10));
assertEquals("0F",Util.hex(15));
assertEquals("10",Util.hex(16));
assertEquals("19",Util.hex(25));
assertEquals("FF",Util.hex(255));
}
public void testSha256() {
assertEquals("9F722959A023C02A3BA0FAFDBA81ADED642D6610EFF5DCA32DCE35132E16B6C5",Util.sha256("Dies ist ein Test"));
}
public void testTranslate() {
assertEquals("Dies ist ein Test",Util.t("{} {} {} {}","Dies","ist","ein","Test"));
}
public void testIsEmail() {
// TODO
}
public void testSimplePassword() {
// TODO
}
}
Loading…
Cancel
Save