Skip to content

Commit

Permalink
parametrized_tests and parametrized_tests.homework packages
Browse files Browse the repository at this point in the history
  • Loading branch information
kacper-cholewinski committed Dec 19, 2024
1 parent 1a14625 commit d03deba
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 0 deletions.
20 changes: 20 additions & 0 deletions kodilla-advanced-tests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'java'
}

group = 'com.kodilla'
version = '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kodilla.parametrized_tests;

public class NumberChecker {

public boolean isDivisibleByThree(int number) {
return number % 3 == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kodilla.parametrized_tests;

public class StringManipulator {

public String reverseWithLowerCase(String input) {
StringBuilder builder = new StringBuilder(input);
return builder.reverse().toString().toLowerCase();
}

public int getStringLengthWithoutSpaces(String input) {
String value = input.replaceAll(" ", "");
return value.length();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.kodilla.parametrized_tests;

public class StringValidator {

public boolean isBlank(String text) {
return text == null || text.trim().isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.kodilla.parametrized_tests.homework;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserValidator {
public static boolean validateUsername(String username) {
return username.matches("^[a-zA-Z0-9._-]{3,}$");
}

public static boolean validateEmail(String email) {
if (null != email) {
String regex = "^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{1,6}))?$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.kodilla.parametrized_tests;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class NumberCheckerTestSuite {

private NumberChecker numberChecker = new NumberChecker();

@ParameterizedTest
@ValueSource(ints = {0, 3, 6, -3, -12, 15})
public void shouldReturnTrueForNumberDivisibleByThree(int number) {
boolean result = numberChecker.isDivisibleByThree(number);
assertTrue(result);
}

@ParameterizedTest
@ValueSource(ints = {-2, -1, 1, 2, 4, 5})
public void shouldReturnFalseForNumberNotDivisibleByThree(int number) {
boolean result = numberChecker.isDivisibleByThree(number);
assertFalse(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.kodilla.parametrized_tests;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class StringManipulatorTestSuite {

private StringManipulator manipulator = new StringManipulator();

@ParameterizedTest
@CsvSource(value = {"test,tset", "OtHEr,rehto", "EVent,tneve", "null,llun", "A,a"})
public void shouldReverseStringWithLowerCase(String input, String expected) {
assertEquals(expected, manipulator.reverseWithLowerCase(input));
}

@ParameterizedTest
@CsvSource(value = {"test,4", " OtHEr ,5", "E V e n t,5", "null ,4", "A,1"})
public void shouldCalculateStringLengthWithoutSpaces(String input, int expected) {
assertEquals(expected, manipulator.getStringLengthWithoutSpaces(input));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kodilla.parametrized_tests;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class StringValidatorTestSuite {

private StringValidator validator = new StringValidator();

@Test
public void shouldReturnFalseIfStringIsNotEmpty() {
assertFalse(validator.isBlank("test"));
}

@Test
public void shouldReturnTrueIfStringIsEmpty() {
assertTrue(validator.isBlank(""));
}

@Test
public void shouldReturnTrueIfStringHasOnlySpaces() {
assertTrue(validator.isBlank(" "));
}

@Test
public void shouldReturnTrueIfStringIsNull() {
assertTrue(validator.isBlank(null));
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", " "})
public void shouldReturnTrueIfStringIsEmpty(String text) {
assertTrue(validator.isBlank(text));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.kodilla.parametrized_tests.homework;

import com.kodilla.parametrized_tests.StringValidator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class UserValidatorTestSuite {

@ParameterizedTest
@ValueSource(strings = {
/* wielkie litery */ "USERNAME",
/* małe litery */ "username",
/* mieszane litery */ "UsErNAme",
/* litery i cyfry */ "Username123",
/* same cyfry */ "123456789",
/* litery, cyfry i dozwolone znaki specjalne */ "23u_s-E.Rname1",
/* same dozwolone znaki specjalne */ "___...---",
/* minimalna długość */ "abc",
/* wysoka długość */ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
})
public void validateUsernameShouldReturnTrue(String username) {
assertTrue(UserValidator.validateUsername(username));
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {
/* poniżej minimalnej długości */ "Ab",
/* poniżej minimalnej długości */ "a",
/* same niedozwolone znaki specjalne */ "*&^%$",
/* litery i niedozwolone znaki specjalne */ "u%s$e#r!",
/* spacja w środku */ "username 123",
/* same spacje */ " ",
/* enter w środku */ "username\n123",
/* same entery */ "\n\n\n\n"
})
public void validateUsernameShouldReturnFalse(String username) {
assertFalse(UserValidator.validateUsername(username));
}

@ParameterizedTest
@ValueSource(strings = {
/* klasyk */ "[email protected]",
/* krótkie części */ "[email protected]",
/* dozwolone znaki specjalne */ "[email protected]",
/* wielkie litery */ "[email protected]",
/* długie części */ "[email protected]",
})
public void validateEmailShouldReturnTrue(String email) {
assertTrue(UserValidator.validateEmail(email));
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {
/* bez domeny */ "user",
/* bez domeny niższego poziomu */ "user.com",
/* bez domeny wyższego poziomu */ "user@mail",
/* sama domena */ "@mail.com",
/* niedozwolone znaki specjalne */ "u$er@ma!l.com",
/* kilka "małp" */ "user@[email protected]",
/* znaki specjalne w domenie wyższego poziomu */ "[email protected]_m",
/* same znaki specjalne */ "!#$%@^&*(.)^!",
/* spacje w środku części */ "us [email protected]", "user@ma il.com", "[email protected] m",
/* spacje między częściami */ "user@ mail.com", "user@mail .com"
})
public void validateEmailShouldReturnFalse(String email) {
assertFalse(UserValidator.validateEmail(email));
}
}

0 comments on commit d03deba

Please sign in to comment.