From d9c3799fbd368ea672d41f5b98df168b8191148e Mon Sep 17 00:00:00 2001 From: maks Date: Wed, 9 Nov 2022 08:10:30 +0100 Subject: Add tests --- .../jopek/applicationdevrification/CodeOption.java | 13 +++ .../applicationdevrification/MainActivity.java | 105 ++++++--------------- .../applicationdevrification/RegexTester.java | 78 +++++++++++++++ .../MainActivityTestAddress.java | 32 +++++++ .../MainActivityTestCensorship.java | 32 +++++++ .../MainActivityTestEmail.java | 32 +++++++ .../MainActivityTestIdcard.java | 32 +++++++ .../MainActivityTestNameNSurname.java | 32 +++++++ .../MainActivityTestPassword.java | 32 +++++++ .../MainActivityTestPesel.java | 32 +++++++ .../MainActivityTestPhone.java | 32 +++++++ 11 files changed, 376 insertions(+), 76 deletions(-) create mode 100644 app/src/main/java/com/jopek/applicationdevrification/CodeOption.java create mode 100644 app/src/main/java/com/jopek/applicationdevrification/RegexTester.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestAddress.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestCensorship.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestEmail.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestIdcard.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestNameNSurname.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPassword.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPesel.java create mode 100644 app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPhone.java (limited to 'app/src') diff --git a/app/src/main/java/com/jopek/applicationdevrification/CodeOption.java b/app/src/main/java/com/jopek/applicationdevrification/CodeOption.java new file mode 100644 index 0000000..d4b96f2 --- /dev/null +++ b/app/src/main/java/com/jopek/applicationdevrification/CodeOption.java @@ -0,0 +1,13 @@ +package com.jopek.applicationdevrification; + +public enum CodeOption { + zipCode, + address, + phone, + email, + pesel, + idcard, + nameNSurname, + password, + censorship, +} diff --git a/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java b/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java index e9c9603..1b46be2 100644 --- a/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java +++ b/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java @@ -7,7 +7,6 @@ import android.graphics.Color; import android.os.Bundle; import android.text.InputFilter; import android.text.InputType; -import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; @@ -21,19 +20,8 @@ import androidx.appcompat.app.AppCompatActivity; import java.util.regex.Pattern; -enum CodeOption { - zipCode, - address, - phone, - email, - pesel, - idcard, - nameNSurname, - password, - censorship, -} - public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { + public boolean testing = false; ArrayAdapter adapter; EditText input; TextView output; @@ -68,72 +56,37 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte } else if (selOption == CodeOption.zipCode) { Requests requests = new Requests(); requests.get("https://jopek.eu/maks/get-city?code=" + in, txt -> { - validateResult(txt.length() != 0, ""); + validateResult(txt.length() != 0, txt); }); return; - } else if (selOption == CodeOption.address) { - Pattern p = Pattern.compile("^([a-ząśćżźńłęó]+ )+\\d+[a-z]?$", Pattern.CASE_INSENSITIVE); - result = p.matcher(in).matches(); - } else if (selOption == CodeOption.phone) { - Pattern p; - if (in.charAt(0) == '+') { - p = Pattern.compile("\\+(\\d\\d?\\d?)\\d{9}", Pattern.CASE_INSENSITIVE); - } else { - p = Pattern.compile("\\d{7}(\\d{2})?", Pattern.CASE_INSENSITIVE); - } - result = p.matcher(in).matches(); - } else if (selOption == CodeOption.email) { - Pattern p = Pattern.compile("^[a-zA-Z][\\w.]+[\\w]@[a-z]{2,}\\.[a-z]{2,}$"); - result = p.matcher(in).matches(); } else if (selOption == CodeOption.pesel) { - int lastDigit = Integer.parseInt(in.substring(10, 11)); - int a = Integer.parseInt(in.substring(0, 1)); - int b = Integer.parseInt(in.substring(1, 2)); - int c = Integer.parseInt(in.substring(2, 3)); - int d = Integer.parseInt(in.substring(3, 4)); - int e = Integer.parseInt(in.substring(4, 5)); - int f = Integer.parseInt(in.substring(5, 6)); - int g = Integer.parseInt(in.substring(6, 7)); - int h = Integer.parseInt(in.substring(7, 8)); - int i = Integer.parseInt(in.substring(8, 9)); - int j = Integer.parseInt(in.substring(9, 10)); - int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j; - result = sum % 10 == lastDigit; - if (result) { - int plec = Integer.parseInt(in.substring(9, 10)); - if (plec % 2 == 0 || plec == 0) { - gender = "K"; - } else { - gender = "M"; + if (in.length() != 11) { + result = false; + } else { + int lastDigit = Integer.parseInt(in.substring(10, 11)); + int a = Integer.parseInt(in.substring(0, 1)); + int b = Integer.parseInt(in.substring(1, 2)); + int c = Integer.parseInt(in.substring(2, 3)); + int d = Integer.parseInt(in.substring(3, 4)); + int e = Integer.parseInt(in.substring(4, 5)); + int f = Integer.parseInt(in.substring(5, 6)); + int g = Integer.parseInt(in.substring(6, 7)); + int h = Integer.parseInt(in.substring(7, 8)); + int i = Integer.parseInt(in.substring(8, 9)); + int j = Integer.parseInt(in.substring(9, 10)); + int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j; + result = sum % 10 == lastDigit; + if (result) { + int plec = Integer.parseInt(in.substring(9, 10)); + if (plec % 2 == 0 || plec == 0) { + gender = "K"; + } else { + gender = "M"; + } } } - } else if (selOption == CodeOption.idcard) { - Pattern p = Pattern.compile(".*\\d.*"); - result = p.matcher(in).matches(); - if (result) { - p = Pattern.compile("^[a-z][a-z\\d]+$", Pattern.CASE_INSENSITIVE); - result = p.matcher(in).matches(); - } - } else if (selOption == CodeOption.nameNSurname) { - Pattern p = Pattern.compile("^[a-z]+ [a-z]+(-[a-z]+)?$", Pattern.CASE_INSENSITIVE); - result = p.matcher(in).matches(); - } else if (selOption == CodeOption.password) { - result = in.matches(".*[A-Z].*"); - Log.d("maks", "validate: " + result); - result = result && in.matches(".*[a-z].*"); - Log.d("maks", "validate: " + result); - result = result && in.matches(".*([^\\w\\s]|_).*"); - Log.d("maks", "validate: " + result); - result = result && in.matches(".*\\d.*"); - Log.d("maks", "validate: " + result); - result = result && in.matches(".{7,}"); - Log.d("maks", "validate: " + result); - } else if (selOption == CodeOption.censorship) { - String[] illegal = new String[]{"wtf", "rabarbar", "kobieta", "bezalkoholowe", "kamienskiego"}; - result = true; - for (String ill : illegal) { - result = result && !in.matches(".*" + ill + ".*"); - } + } else { + result = RegexTester.validate(in, selOption); } validateResult(result, gender); @@ -167,7 +120,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte selOption = CodeOption.address// input.setMinLines(1); // input.setLines(2); // input.setMaxLines(10); -; + ; } else if ("phone".contentEquals(selectedOption)) { selOption = CodeOption.phone; } else if ("email".contentEquals(selectedOption)) { @@ -195,7 +148,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte } else if (selOption == CodeOption.pesel) { input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)}); input.setInputType(InputType.TYPE_CLASS_NUMBER); - } else if (selOption == CodeOption.password || selOption == CodeOption.censorship) { + } else if (/* selOption == CodeOption.password || */ selOption == CodeOption.censorship) { input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); input.setSingleLine(false); } diff --git a/app/src/main/java/com/jopek/applicationdevrification/RegexTester.java b/app/src/main/java/com/jopek/applicationdevrification/RegexTester.java new file mode 100644 index 0000000..da491fc --- /dev/null +++ b/app/src/main/java/com/jopek/applicationdevrification/RegexTester.java @@ -0,0 +1,78 @@ +package com.jopek.applicationdevrification; + +import java.util.regex.Pattern; + +public class RegexTester { + static boolean validate(String in, CodeOption selOption) { + String gender = ""; + boolean result = false; + if (in.length() == 0) { + result = false; + } else if (selOption == CodeOption.zipCode) { + return true; + } else if (selOption == CodeOption.address) { + Pattern p = Pattern.compile("^([a-ząśćżźńłęó]+ )+\\d+[a-z]?$", Pattern.CASE_INSENSITIVE); + result = p.matcher(in).matches(); + } else if (selOption == CodeOption.phone) { + Pattern p; + if (in.charAt(0) == '+') { + p = Pattern.compile("\\+(\\d\\d?\\d?)\\d{9}", Pattern.CASE_INSENSITIVE); + } else { + p = Pattern.compile("\\d{7}(\\d{2})?", Pattern.CASE_INSENSITIVE); + } + result = p.matcher(in).matches(); + } else if (selOption == CodeOption.email) { + Pattern p = Pattern.compile("^[a-zA-Z][\\w.]+[\\w]@[a-z]{2,}\\.[a-z]{2,}$"); + result = p.matcher(in).matches(); + } else if (selOption == CodeOption.pesel) { + if (in.length() != 11) { + result = false; + } else { + int lastDigit = Integer.parseInt(in.substring(10, 11)); + int a = Integer.parseInt(in.substring(0, 1)); + int b = Integer.parseInt(in.substring(1, 2)); + int c = Integer.parseInt(in.substring(2, 3)); + int d = Integer.parseInt(in.substring(3, 4)); + int e = Integer.parseInt(in.substring(4, 5)); + int f = Integer.parseInt(in.substring(5, 6)); + int g = Integer.parseInt(in.substring(6, 7)); + int h = Integer.parseInt(in.substring(7, 8)); + int i = Integer.parseInt(in.substring(8, 9)); + int j = Integer.parseInt(in.substring(9, 10)); + int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j; + result = sum % 10 == lastDigit; + if (result) { + int plec = Integer.parseInt(in.substring(9, 10)); + if (plec % 2 == 0 || plec == 0) { + gender = "K"; + } else { + gender = "M"; + } + } + } + } else if (selOption == CodeOption.idcard) { + Pattern p = Pattern.compile(".*\\d.*"); + result = p.matcher(in).matches(); + if (result) { + p = Pattern.compile("^[a-z][a-z\\d]+$", Pattern.CASE_INSENSITIVE); + result = p.matcher(in).matches(); + } + } else if (selOption == CodeOption.nameNSurname) { + Pattern p = Pattern.compile("^[a-z]+ [a-z]+(-[a-z]+)?$", Pattern.CASE_INSENSITIVE); + result = p.matcher(in).matches(); + } else if (selOption == CodeOption.password) { + result = in.matches(".*[A-Z].*"); + result = result && in.matches(".*[a-z].*"); + result = result && in.matches(".*([^\\w\\s]|_).*"); + result = result && in.matches(".*\\d.*"); + result = result && in.matches(".{7,}"); + } else if (selOption == CodeOption.censorship) { + String[] illegal = new String[]{"wtf", "rabarbar", "kobieta", "bezalkoholowe", "kamienskiego"}; + result = true; + for (String ill : illegal) { + result = result && !in.matches(".*" + ill + ".*"); + } + } + return result; + } +} diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestAddress.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestAddress.java new file mode 100644 index 0000000..0bbf284 --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestAddress.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestAddress { + @Test + public void test1() { + boolean res = RegexTester.validate("Tomaszkowice 552", CodeOption.address); + assertEquals(res, true); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("Lacznosc 359a", CodeOption.address); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing address"); + } + @AfterClass + public static void afterClass() { + System.out.println("Address passed tests"); + } +} \ No newline at end of file diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestCensorship.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestCensorship.java new file mode 100644 index 0000000..22d52bc --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestCensorship.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestCensorship { + @Test + public void test1() { + boolean res = RegexTester.validate("aakobietaaaa", CodeOption.censorship); + assertEquals(res, false); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("Good txt", CodeOption.censorship); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing censorship"); + } + @AfterClass + public static void afterClass() { + System.out.println("Censorship passed tests"); + } +} \ No newline at end of file diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestEmail.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestEmail.java new file mode 100644 index 0000000..e79dce7 --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestEmail.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestEmail { + @Test + public void test1() { + boolean res = RegexTester.validate("maks@jopek.eu", CodeOption.email); + assertNotEquals(res, false); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("nie emial", CodeOption.email); + assertNotEquals(res, true); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing email"); + } + @AfterClass + public static void afterClass() { + System.out.println("Email passed tests"); + } +} diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestIdcard.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestIdcard.java new file mode 100644 index 0000000..224c25e --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestIdcard.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestIdcard { + @Test + public void test1() { + boolean res = RegexTester.validate("qwe123ASD@!#(", CodeOption.idcard); + assertEquals(res, false); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("asd123", CodeOption.idcard); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing idcard"); + } + @AfterClass + public static void afterClass() { + System.out.println("Idcard passed tests"); + } +} diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestNameNSurname.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestNameNSurname.java new file mode 100644 index 0000000..87f1b5b --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestNameNSurname.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestNameNSurname { + @Test + public void test1() { + boolean res = RegexTester.validate("asd123)(ADS aASD", CodeOption.nameNSurname); + assertEquals(res, false); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("mkas jopek", CodeOption.nameNSurname); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing nameNSurname"); + } + @AfterClass + public static void afterClass() { + System.out.println("NameNSurname passed tests"); + } +} diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPassword.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPassword.java new file mode 100644 index 0000000..a1e2033 --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPassword.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestPassword { + @Test + public void test1() { + boolean res = RegexTester.validate("Tomaszkowice 552", CodeOption.password); + assertEquals(res, false); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("qwe123$%^naksaASD", CodeOption.password); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing password"); + } + @AfterClass + public static void afterClass() { + System.out.println("Password passed tests"); + } +} diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPesel.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPesel.java new file mode 100644 index 0000000..577a3f5 --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPesel.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestPesel { + @Test + public void test1() { + boolean res = RegexTester.validate("Tomaszkowice 552", CodeOption.pesel); + assertEquals(res, true); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("03282606339", CodeOption.pesel); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing pesel"); + } + @AfterClass + public static void afterClass() { + System.out.println("Pesel passed tests"); + } +} diff --git a/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPhone.java b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPhone.java new file mode 100644 index 0000000..5f17459 --- /dev/null +++ b/app/src/test/java/com/jopek/applicationdevrification/MainActivityTestPhone.java @@ -0,0 +1,32 @@ +package com.jopek.applicationdevrification; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MainActivityTestPhone { + @Test + public void test1() { + boolean res = RegexTester.validate("123(*&123", CodeOption.phone); + assertEquals(res,false); + } + + @Test + public void test2() { + boolean res = RegexTester.validate("123456789", CodeOption.phone); + assertTrue(res); + } + + @BeforeClass + public static void beforeClass() { + System.out.println("Testing phone"); + } + @AfterClass + public static void afterClass() { + System.out.println("Phone passed tests"); + } +} -- cgit v1.3.1