Skip to content
Snippets Groups Projects
Commit 3fe2e190 authored by jez04's avatar jez04
Browse files

feat: :tada: solution

parent 3a7fc2aa
No related merge requests found
Showing
with 32 additions and 279 deletions
package jez04.structure.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import org.junit.jupiter.api.Test;
class ClassStructureTest {
StructureHelper helper = StructureHelper.getInstance();
}
run.sh 0 → 100755
#!/bin/bash
cd java2-lab02-v1
java --module-path target/java2-lab02-v1-0.0.1-SNAPHOST.jar:target/libs -m cz.vsb.fei.java2.lab02_module/lab.gui.App
cd ..
package lab;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class DataImporter {
private static final Pattern personTextPattern = Pattern.compile("\\{[^\\{\\}]*address");
private static final Pattern firstNamePattern = Pattern.compile("\"firstname\":\"([\\p{IsAlphabetic}']+)\"");
private static final Pattern lastNamePattern = Pattern.compile("\"lastname\":\"([\\p{IsAlphabetic}']+)\"");
private static final Pattern datePattern = Pattern.compile("\"birthday\":\"(\\d{4}-\\d{2}-\\d{2})\"");
private static final DateTimeFormatter czechDate = DateTimeFormatter.ofPattern("dd. MM. YYYY");
public static void main(String[] args) {
importPlayers();
}
public static void importPlayers() {
List<Person> persons = parseData(downloadText()).stream().map(DataImporter::parsePerson).toList();
for (Person person : persons) {
System.out.printf("%s %s (%s): age=%d, 50th: %s , next in: %d%n"
, person.getFirstName(), person.getLastName()
, person.getDayOfBirth().format(czechDate)
, person.getAge()
, person.get50thBirthDay().format(czechDate)
, person.getDaysToBirthday());
}
}
public static String downloadText() {
try {
URL url = new URI("https://fakerapi.it/api/v2/persons?_quantity=20").toURL();
try (InputStream is = url.openStream()) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
return bufferedReader.lines().collect(Collectors.joining("\n"));
}
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
return "";
}
}
public static List<String> parseData(String data) {
Matcher matcher = personTextPattern.matcher(data);
return matcher.results().map(MatchResult::group).toList();
}
public static Person parsePerson(String personText) {
String firstName = firstNamePattern.matcher(personText).results().map(result -> result.group(1)).findAny()
.orElse("");
String lastName = lastNamePattern.matcher(personText).results().map(result -> result.group(1)).findAny()
.orElse("");
LocalDate date = datePattern.matcher(personText).results().map(result -> result.group(1)).map(text -> LocalDate.parse(text, DateTimeFormatter.ISO_DATE)).findAny()
.orElse(LocalDate.EPOCH);
return new Person(firstName, lastName, date);
}
}
package lab;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class Person {
private String firstName;
private String lastName;
private LocalDate dayOfBirth;
public Person(String firstName, String lastName, LocalDate dayOfBirth) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.dayOfBirth = dayOfBirth;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public LocalDate getDayOfBirth() {
return dayOfBirth;
}
public int getAge() {
return Period.between(dayOfBirth, LocalDate.now()).getYears();
}
public static void main(String[] args) {
Person p = new Person("", "", LocalDate.now().minusDays(3700));
System.out.println(p.getAge());
System.out.println(p.getDaysToBirthday());
}
public LocalDate get50thBirthDay() {
return dayOfBirth.plusYears(50);
}
public long getDaysToBirthday() {
LocalDate nextBirthday = dayOfBirth.withYear(LocalDate.now().getYear());
if(nextBirthday.isBefore(LocalDate.now())){
nextBirthday = nextBirthday.plusYears(1);
}
return ChronoUnit.DAYS.between(LocalDate.now(), nextBirthday);
}
}
package lab;
public class ScoreStorageFactory {
private static DbConnector instance;
public static DbConnector getInstance() {
if(instance == null) {
instance = new DbConnector();
}
return instance;
}
}
module lab01 {
requires transitive javafx.controls;
requires javafx.fxml;
requires javafx.base;
requires java.sql;
opens lab to javafx.fxml;
exports lab;
}
\ No newline at end of file
package jez04.structure.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import org.junit.jupiter.api.Test;
class ClassStructureTest {
StructureHelper helper = StructureHelper.getInstance();
@Test
void dataImporterTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> c = helper.getClassRegexp(".*Data.*");
}
@Test
void dataImporterDownloadTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> d = helper.getClassRegexp(".*Data.*");
assertThat(d, new HasMethod(".*down.*", String.class).useRegExp(true).caseSensitive(false));
Method download = helper.getMethod(d, ".*down.*", false, String.class);
String downloadedText = (String) download.invoke(null);
assertThat(downloadedText, allOf(
endsWith("}]}"),
startsWith("{\"status\":\"OK\""),
stringContainsInOrder("firstname"),
stringContainsInOrder("lastname"),
stringContainsInOrder("birthday")
));
}
@Test
void dataImporterParseAllTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> d = helper.getClassRegexp(".*Data.*");
helper.hasMethod(d, ".*parse.*", false, List.class, String.class);
Method parse = helper.getMethod(d, ".*parse.*", false, List.class, String.class);
List<String> texts = (List<String>) parse.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\"1946-11-06\",\"gender\":\"male\",\"address\":{");
assertThat(texts, not(empty()));
}
@Test
void personTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist("Person", false, false));
Class<?> p = helper.getClass("Person", false);
}
@Test
void dataImporterParsePersonTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> d = helper.getClassRegexp(".*Data.*");
assertThat("", new ClassExist("Person", false, false));
Class<?> p = helper.getClass("Person", false);
helper.hasMethod(d, ".*parse.*", false, p, String.class);
Method parsePerson = helper.getMethod(d, ".*parse.*", false, p, String.class);
Object person = parsePerson.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\"1946-11-06\",\"gender\":\"male\",\"address\":{");
assertThat(person, notNullValue());
}
@Test
void personAgeTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> d = helper.getClassRegexp(".*Data.*");
assertThat("", new ClassExist("Person", false, false));
Class<?> p = helper.getClass("Person", false);
helper.hasMethod(d, ".*parse.*", false, p, String.class);
Method parsePerson = helper.getMethod(d, ".*parse.*", false, p, String.class);
Object person = parsePerson.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\"1946-11-06\",\"gender\":\"male\",\"address\":{");
assertThat(person, notNullValue());
Method age = helper.getMethod(p, ".*age.*", false, int.class);
int result = (int)age.invoke(person);
assertEquals(78, result , "Calculate wrong age.");
}
@Test
void person50birthdayTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> d = helper.getClassRegexp(".*Data.*");
assertThat("", new ClassExist("Person", false, false));
Class<?> p = helper.getClass("Person", false);
helper.hasMethod(d, ".*parse.*", false, p, String.class);
Method parsePerson = helper.getMethod(d, ".*parse.*", false, p, String.class);
Object person = parsePerson.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\"1946-11-06\",\"gender\":\"male\",\"address\":{");
assertThat(person, notNullValue());
Method birth50 = helper.getMethod(p, ".*50.*", false, LocalDate.class);
LocalDate ldBirth50 = (LocalDate)birth50.invoke(person);
assertEquals(LocalDate.of(1996, 11, 06), ldBirth50 , "Calculate wrong 50th birthday.");
}
@Test
void personNextBirthdayTest() throws URISyntaxException, IOException, IllegalAccessException, InvocationTargetException {
assertThat("", new ClassExist(".*Data.*", false, true));
Class<?> d = helper.getClassRegexp(".*Data.*");
assertThat("", new ClassExist("Person", false, false));
Class<?> p = helper.getClass("Person", false);
helper.hasMethod(d, ".*parse.*", false, p, String.class);
Method parsePerson = helper.getMethod(d, ".*parse.*", false, p, String.class);
LocalDate bod =LocalDate.now().plusDays(338).minusYears(10);
Object person = parsePerson.invoke(null, "{\"id\":1,\"firstname\":\"Tatum\",\"lastname\":\"Block\",\"email\":\"lonnie.bergstrom@stoltenberg.com\",\"phone\":\"+12397191764\",\"birthday\":\""+ bod.format(DateTimeFormatter.ISO_DATE) +"\",\"gender\":\"male\",\"address\":{");
assertThat(person, notNullValue());
Method daysM = helper.getMethod(p, ".*days.*", false, long.class);
long days= (long)daysM.invoke(person);
assertEquals(338, days , "Calculate wrong days to next birthday.");
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment