Replaced all tabs with spaces

This commit is contained in:
Hanz727 2025-11-14 19:36:29 +01:00
commit ee0d2ce427
9 changed files with 247 additions and 247 deletions

View file

@ -31,27 +31,27 @@ import javafx.stage.Stage;
public class Main extends Application { public class Main extends Application {
private static final Injector INJECTOR = createInjector(new MyModule()); private static final Injector INJECTOR = createInjector(new MyModule());
private static final MyFXML FXML = new MyFXML(INJECTOR); private static final MyFXML FXML = new MyFXML(INJECTOR);
public static void main(String[] args) throws URISyntaxException, IOException { public static void main(String[] args) throws URISyntaxException, IOException {
launch(); launch();
} }
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
var serverUtils = INJECTOR.getInstance(ServerUtils.class); var serverUtils = INJECTOR.getInstance(ServerUtils.class);
if (!serverUtils.isServerAvailable()) { if (!serverUtils.isServerAvailable()) {
var msg = "Server needs to be started before the client, but it does not seem to be available. Shutting down."; var msg = "Server needs to be started before the client, but it does not seem to be available. Shutting down.";
System.err.println(msg); System.err.println(msg);
return; return;
} }
var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml"); var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml");
var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml"); var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml");
var mainCtrl = INJECTOR.getInstance(MainCtrl.class); var mainCtrl = INJECTOR.getInstance(MainCtrl.class);
mainCtrl.initialize(primaryStage, overview, add); mainCtrl.initialize(primaryStage, overview, add);
} }
} }

View file

@ -29,68 +29,68 @@ import javafx.stage.Modality;
public class AddQuoteCtrl { public class AddQuoteCtrl {
private final ServerUtils server; private final ServerUtils server;
private final MainCtrl mainCtrl; private final MainCtrl mainCtrl;
@FXML @FXML
private TextField firstName; private TextField firstName;
@FXML @FXML
private TextField lastName; private TextField lastName;
@FXML @FXML
private TextField quote; private TextField quote;
@Inject @Inject
public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) { public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) {
this.mainCtrl = mainCtrl; this.mainCtrl = mainCtrl;
this.server = server; this.server = server;
} }
public void cancel() { public void cancel() {
clearFields(); clearFields();
mainCtrl.showOverview(); mainCtrl.showOverview();
} }
public void ok() { public void ok() {
try { try {
server.addQuote(getQuote()); server.addQuote(getQuote());
} catch (WebApplicationException e) { } catch (WebApplicationException e) {
var alert = new Alert(Alert.AlertType.ERROR); var alert = new Alert(Alert.AlertType.ERROR);
alert.initModality(Modality.APPLICATION_MODAL); alert.initModality(Modality.APPLICATION_MODAL);
alert.setContentText(e.getMessage()); alert.setContentText(e.getMessage());
alert.showAndWait(); alert.showAndWait();
return; return;
} }
clearFields(); clearFields();
mainCtrl.showOverview(); mainCtrl.showOverview();
} }
private Quote getQuote() { private Quote getQuote() {
var p = new Person(firstName.getText(), lastName.getText()); var p = new Person(firstName.getText(), lastName.getText());
var q = quote.getText(); var q = quote.getText();
return new Quote(p, q); return new Quote(p, q);
} }
private void clearFields() { private void clearFields() {
firstName.clear(); firstName.clear();
lastName.clear(); lastName.clear();
quote.clear(); quote.clear();
} }
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
switch (e.getCode()) { switch (e.getCode()) {
case ENTER: case ENTER:
ok(); ok();
break; break;
case ESCAPE: case ESCAPE:
cancel(); cancel();
break; break;
default: default:
break; break;
} }
} }
} }

View file

@ -35,43 +35,43 @@ import jakarta.ws.rs.core.GenericType;
public class ServerUtils { public class ServerUtils {
private static final String SERVER = "http://localhost:8080/"; private static final String SERVER = "http://localhost:8080/";
public void getQuotesTheHardWay() throws IOException, URISyntaxException { public void getQuotesTheHardWay() throws IOException, URISyntaxException {
var url = new URI("http://localhost:8080/api/quotes").toURL(); var url = new URI("http://localhost:8080/api/quotes").toURL();
var is = url.openConnection().getInputStream(); var is = url.openConnection().getInputStream();
var br = new BufferedReader(new InputStreamReader(is)); var br = new BufferedReader(new InputStreamReader(is));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
System.out.println(line); System.out.println(line);
} }
} }
public List<Quote> getQuotes() { public List<Quote> getQuotes() {
return ClientBuilder.newClient(new ClientConfig()) // return ClientBuilder.newClient(new ClientConfig()) //
.target(SERVER).path("api/quotes") // .target(SERVER).path("api/quotes") //
.request(APPLICATION_JSON) // .request(APPLICATION_JSON) //
.get(new GenericType<List<Quote>>() {}); .get(new GenericType<List<Quote>>() {});
} }
public Quote addQuote(Quote quote) { public Quote addQuote(Quote quote) {
return ClientBuilder.newClient(new ClientConfig()) // return ClientBuilder.newClient(new ClientConfig()) //
.target(SERVER).path("api/quotes") // .target(SERVER).path("api/quotes") //
.request(APPLICATION_JSON) // .request(APPLICATION_JSON) //
.post(Entity.entity(quote, APPLICATION_JSON), Quote.class); .post(Entity.entity(quote, APPLICATION_JSON), Quote.class);
} }
public boolean isServerAvailable() { public boolean isServerAvailable() {
try { try {
ClientBuilder.newClient(new ClientConfig()) // ClientBuilder.newClient(new ClientConfig()) //
.target(SERVER) // .target(SERVER) //
.request(APPLICATION_JSON) // .request(APPLICATION_JSON) //
.get(); .get();
} catch (ProcessingException e) { } catch (ProcessingException e) {
if (e.getCause() instanceof ConnectException) { if (e.getCause() instanceof ConnectException) {
return false; return false;
} }
} }
return true; return true;
} }
} }

View file

@ -29,35 +29,35 @@ import jakarta.persistence.Id;
@Entity @Entity
public class Person { public class Person {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
public long id; public long id;
public String firstName; public String firstName;
public String lastName; public String lastName;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private Person() { private Person() {
// for object mapper // for object mapper
} }
public Person(String firstName, String lastName) { public Person(String firstName, String lastName) {
this.firstName = firstName; this.firstName = firstName;
this.lastName = lastName; this.lastName = lastName;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj); return EqualsBuilder.reflectionEquals(this, obj);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this); return HashCodeBuilder.reflectionHashCode(this);
} }
@Override @Override
public String toString() { public String toString() {
return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE); return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE);
} }
} }

View file

@ -31,36 +31,36 @@ import jakarta.persistence.OneToOne;
@Entity @Entity
public class Quote { public class Quote {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
public long id; public long id;
@OneToOne(cascade = CascadeType.PERSIST) @OneToOne(cascade = CascadeType.PERSIST)
public Person person; public Person person;
public String quote; public String quote;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private Quote() { private Quote() {
// for object mappers // for object mappers
} }
public Quote(Person person, String quote) { public Quote(Person person, String quote) {
this.person = person; this.person = person;
this.quote = quote; this.quote = quote;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj); return EqualsBuilder.reflectionEquals(this, obj);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this); return HashCodeBuilder.reflectionHashCode(this);
} }
@Override @Override
public String toString() { public String toString() {
return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE); return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE);
} }
} }

View file

@ -23,34 +23,34 @@ import org.junit.jupiter.api.Test;
public class PersonTest { public class PersonTest {
@Test @Test
public void checkConstructor() { public void checkConstructor() {
var p = new Person("f", "l"); var p = new Person("f", "l");
assertEquals("f", p.firstName); assertEquals("f", p.firstName);
assertEquals("l", p.lastName); assertEquals("l", p.lastName);
} }
@Test @Test
public void equalsHashCode() { public void equalsHashCode() {
var a = new Person("a", "b"); var a = new Person("a", "b");
var b = new Person("a", "b"); var b = new Person("a", "b");
assertEquals(a, b); assertEquals(a, b);
assertEquals(a.hashCode(), b.hashCode()); assertEquals(a.hashCode(), b.hashCode());
} }
@Test @Test
public void notEqualsHashCode() { public void notEqualsHashCode() {
var a = new Person("a", "b"); var a = new Person("a", "b");
var b = new Person("a", "c"); var b = new Person("a", "c");
assertNotEquals(a, b); assertNotEquals(a, b);
assertNotEquals(a.hashCode(), b.hashCode()); assertNotEquals(a.hashCode(), b.hashCode());
} }
@Test @Test
public void hasToString() { public void hasToString() {
var actual = new Person("a", "b").toString(); var actual = new Person("a", "b").toString();
assertTrue(actual.contains(Person.class.getSimpleName())); assertTrue(actual.contains(Person.class.getSimpleName()));
assertTrue(actual.contains("\n")); assertTrue(actual.contains("\n"));
assertTrue(actual.contains("firstName")); assertTrue(actual.contains("firstName"));
} }
} }

View file

@ -23,36 +23,36 @@ import org.junit.jupiter.api.Test;
public class QuoteTest { public class QuoteTest {
private static final Person SOME_PERSON = new Person("a", "b"); private static final Person SOME_PERSON = new Person("a", "b");
@Test @Test
public void checkConstructor() { public void checkConstructor() {
var q = new Quote(SOME_PERSON, "q"); var q = new Quote(SOME_PERSON, "q");
assertEquals(SOME_PERSON, q.person); assertEquals(SOME_PERSON, q.person);
assertEquals("q", q.quote); assertEquals("q", q.quote);
} }
@Test @Test
public void equalsHashCode() { public void equalsHashCode() {
var a = new Quote(new Person("a", "b"), "c"); var a = new Quote(new Person("a", "b"), "c");
var b = new Quote(new Person("a", "b"), "c"); var b = new Quote(new Person("a", "b"), "c");
assertEquals(a, b); assertEquals(a, b);
assertEquals(a.hashCode(), b.hashCode()); assertEquals(a.hashCode(), b.hashCode());
} }
@Test @Test
public void notEqualsHashCode() { public void notEqualsHashCode() {
var a = new Quote(new Person("a", "b"), "c"); var a = new Quote(new Person("a", "b"), "c");
var b = new Quote(new Person("a", "b"), "d"); var b = new Quote(new Person("a", "b"), "d");
assertNotEquals(a, b); assertNotEquals(a, b);
assertNotEquals(a.hashCode(), b.hashCode()); assertNotEquals(a.hashCode(), b.hashCode());
} }
@Test @Test
public void hasToString() { public void hasToString() {
var actual = new Quote(new Person("a", "b"), "c").toString(); var actual = new Quote(new Person("a", "b"), "c").toString();
assertTrue(actual.contains(Quote.class.getSimpleName())); assertTrue(actual.contains(Quote.class.getSimpleName()));
assertTrue(actual.contains("\n")); assertTrue(actual.contains("\n"));
assertTrue(actual.contains("person")); assertTrue(actual.contains("person"));
} }
} }

View file

@ -30,23 +30,23 @@ import commons.Person;
@RequestMapping("/api/people") @RequestMapping("/api/people")
public class PersonListingController { public class PersonListingController {
private List<Person> people = new LinkedList<>(); private List<Person> people = new LinkedList<>();
public PersonListingController() { public PersonListingController() {
people.add(new Person("Mickey", "Mouse")); people.add(new Person("Mickey", "Mouse"));
people.add(new Person("Donald", "Duck")); people.add(new Person("Donald", "Duck"));
} }
@GetMapping("/") @GetMapping("/")
public List<Person> list() { public List<Person> list() {
return people; return people;
} }
@PostMapping("/") @PostMapping("/")
public List<Person> add(@RequestBody Person p) { public List<Person> add(@RequestBody Person p) {
if (!people.contains(p)) { if (!people.contains(p)) {
people.add(p); people.add(p);
} }
return people; return people;
} }
} }

View file

@ -26,28 +26,28 @@ import commons.Person;
public class PersonListingControllerTest { public class PersonListingControllerTest {
private static final Person MICKEY = new Person("Mickey", "Mouse"); private static final Person MICKEY = new Person("Mickey", "Mouse");
private static final Person DONALD = new Person("Donald", "Duck"); private static final Person DONALD = new Person("Donald", "Duck");
private static final Person SCROOGE = new Person("Scrooge", "McDuck"); private static final Person SCROOGE = new Person("Scrooge", "McDuck");
private PersonListingController sut; private PersonListingController sut;
@BeforeEach @BeforeEach
public void setup() { public void setup() {
sut = new PersonListingController(); sut = new PersonListingController();
} }
@Test @Test
public void containsTwoDefaultNames() { public void containsTwoDefaultNames() {
var actual = sut.list(); var actual = sut.list();
var expected = List.of(MICKEY, DONALD); var expected = List.of(MICKEY, DONALD);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test @Test
public void canAddPeople() { public void canAddPeople() {
var actual = sut.add(SCROOGE); var actual = sut.add(SCROOGE);
var expected = List.of(MICKEY, DONALD, SCROOGE); var expected = List.of(MICKEY, DONALD, SCROOGE);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
} }