Replaced all tabs with spaces
This commit is contained in:
parent
bceb413241
commit
ee0d2ce427
9 changed files with 247 additions and 247 deletions
|
|
@ -31,27 +31,27 @@ import javafx.stage.Stage;
|
|||
|
||||
public class Main extends Application {
|
||||
|
||||
private static final Injector INJECTOR = createInjector(new MyModule());
|
||||
private static final MyFXML FXML = new MyFXML(INJECTOR);
|
||||
private static final Injector INJECTOR = createInjector(new MyModule());
|
||||
private static final MyFXML FXML = new MyFXML(INJECTOR);
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException, IOException {
|
||||
launch();
|
||||
}
|
||||
public static void main(String[] args) throws URISyntaxException, IOException {
|
||||
launch();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
|
||||
var serverUtils = INJECTOR.getInstance(ServerUtils.class);
|
||||
if (!serverUtils.isServerAvailable()) {
|
||||
var msg = "Server needs to be started before the client, but it does not seem to be available. Shutting down.";
|
||||
System.err.println(msg);
|
||||
return;
|
||||
}
|
||||
var serverUtils = INJECTOR.getInstance(ServerUtils.class);
|
||||
if (!serverUtils.isServerAvailable()) {
|
||||
var msg = "Server needs to be started before the client, but it does not seem to be available. Shutting down.";
|
||||
System.err.println(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml");
|
||||
var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml");
|
||||
var overview = FXML.load(QuoteOverviewCtrl.class, "client", "scenes", "QuoteOverview.fxml");
|
||||
var add = FXML.load(AddQuoteCtrl.class, "client", "scenes", "AddQuote.fxml");
|
||||
|
||||
var mainCtrl = INJECTOR.getInstance(MainCtrl.class);
|
||||
mainCtrl.initialize(primaryStage, overview, add);
|
||||
}
|
||||
var mainCtrl = INJECTOR.getInstance(MainCtrl.class);
|
||||
mainCtrl.initialize(primaryStage, overview, add);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,68 +29,68 @@ import javafx.stage.Modality;
|
|||
|
||||
public class AddQuoteCtrl {
|
||||
|
||||
private final ServerUtils server;
|
||||
private final MainCtrl mainCtrl;
|
||||
private final ServerUtils server;
|
||||
private final MainCtrl mainCtrl;
|
||||
|
||||
@FXML
|
||||
private TextField firstName;
|
||||
@FXML
|
||||
private TextField firstName;
|
||||
|
||||
@FXML
|
||||
private TextField lastName;
|
||||
@FXML
|
||||
private TextField lastName;
|
||||
|
||||
@FXML
|
||||
private TextField quote;
|
||||
@FXML
|
||||
private TextField quote;
|
||||
|
||||
@Inject
|
||||
public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) {
|
||||
this.mainCtrl = mainCtrl;
|
||||
this.server = server;
|
||||
@Inject
|
||||
public AddQuoteCtrl(ServerUtils server, MainCtrl mainCtrl) {
|
||||
this.mainCtrl = mainCtrl;
|
||||
this.server = server;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
clearFields();
|
||||
mainCtrl.showOverview();
|
||||
}
|
||||
public void cancel() {
|
||||
clearFields();
|
||||
mainCtrl.showOverview();
|
||||
}
|
||||
|
||||
public void ok() {
|
||||
try {
|
||||
server.addQuote(getQuote());
|
||||
} catch (WebApplicationException e) {
|
||||
public void ok() {
|
||||
try {
|
||||
server.addQuote(getQuote());
|
||||
} catch (WebApplicationException e) {
|
||||
|
||||
var alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.initModality(Modality.APPLICATION_MODAL);
|
||||
alert.setContentText(e.getMessage());
|
||||
alert.showAndWait();
|
||||
return;
|
||||
}
|
||||
var alert = new Alert(Alert.AlertType.ERROR);
|
||||
alert.initModality(Modality.APPLICATION_MODAL);
|
||||
alert.setContentText(e.getMessage());
|
||||
alert.showAndWait();
|
||||
return;
|
||||
}
|
||||
|
||||
clearFields();
|
||||
mainCtrl.showOverview();
|
||||
}
|
||||
clearFields();
|
||||
mainCtrl.showOverview();
|
||||
}
|
||||
|
||||
private Quote getQuote() {
|
||||
var p = new Person(firstName.getText(), lastName.getText());
|
||||
var q = quote.getText();
|
||||
return new Quote(p, q);
|
||||
}
|
||||
private Quote getQuote() {
|
||||
var p = new Person(firstName.getText(), lastName.getText());
|
||||
var q = quote.getText();
|
||||
return new Quote(p, q);
|
||||
}
|
||||
|
||||
private void clearFields() {
|
||||
firstName.clear();
|
||||
lastName.clear();
|
||||
quote.clear();
|
||||
}
|
||||
private void clearFields() {
|
||||
firstName.clear();
|
||||
lastName.clear();
|
||||
quote.clear();
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
case ENTER:
|
||||
ok();
|
||||
break;
|
||||
case ESCAPE:
|
||||
cancel();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
case ENTER:
|
||||
ok();
|
||||
break;
|
||||
case ESCAPE:
|
||||
cancel();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -35,43 +35,43 @@ import jakarta.ws.rs.core.GenericType;
|
|||
|
||||
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 {
|
||||
var url = new URI("http://localhost:8080/api/quotes").toURL();
|
||||
var is = url.openConnection().getInputStream();
|
||||
var br = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
public void getQuotesTheHardWay() throws IOException, URISyntaxException {
|
||||
var url = new URI("http://localhost:8080/api/quotes").toURL();
|
||||
var is = url.openConnection().getInputStream();
|
||||
var br = new BufferedReader(new InputStreamReader(is));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Quote> getQuotes() {
|
||||
return ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER).path("api/quotes") //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get(new GenericType<List<Quote>>() {});
|
||||
}
|
||||
public List<Quote> getQuotes() {
|
||||
return ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER).path("api/quotes") //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get(new GenericType<List<Quote>>() {});
|
||||
}
|
||||
|
||||
public Quote addQuote(Quote quote) {
|
||||
return ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER).path("api/quotes") //
|
||||
.request(APPLICATION_JSON) //
|
||||
.post(Entity.entity(quote, APPLICATION_JSON), Quote.class);
|
||||
}
|
||||
public Quote addQuote(Quote quote) {
|
||||
return ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER).path("api/quotes") //
|
||||
.request(APPLICATION_JSON) //
|
||||
.post(Entity.entity(quote, APPLICATION_JSON), Quote.class);
|
||||
}
|
||||
|
||||
public boolean isServerAvailable() {
|
||||
try {
|
||||
ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER) //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get();
|
||||
} catch (ProcessingException e) {
|
||||
if (e.getCause() instanceof ConnectException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public boolean isServerAvailable() {
|
||||
try {
|
||||
ClientBuilder.newClient(new ClientConfig()) //
|
||||
.target(SERVER) //
|
||||
.request(APPLICATION_JSON) //
|
||||
.get();
|
||||
} catch (ProcessingException e) {
|
||||
if (e.getCause() instanceof ConnectException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -29,35 +29,35 @@ import jakarta.persistence.Id;
|
|||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
public long id;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
public long id;
|
||||
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Person() {
|
||||
// for object mapper
|
||||
}
|
||||
@SuppressWarnings("unused")
|
||||
private Person() {
|
||||
// for object mapper
|
||||
}
|
||||
|
||||
public Person(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
public Person(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(this, obj);
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(this, obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE);
|
||||
}
|
||||
}
|
||||
|
|
@ -31,36 +31,36 @@ import jakarta.persistence.OneToOne;
|
|||
@Entity
|
||||
public class Quote {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
public long id;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
public long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.PERSIST)
|
||||
public Person person;
|
||||
public String quote;
|
||||
@OneToOne(cascade = CascadeType.PERSIST)
|
||||
public Person person;
|
||||
public String quote;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Quote() {
|
||||
// for object mappers
|
||||
}
|
||||
@SuppressWarnings("unused")
|
||||
private Quote() {
|
||||
// for object mappers
|
||||
}
|
||||
|
||||
public Quote(Person person, String quote) {
|
||||
this.person = person;
|
||||
this.quote = quote;
|
||||
}
|
||||
public Quote(Person person, String quote) {
|
||||
this.person = person;
|
||||
this.quote = quote;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(this, obj);
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(this, obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, MULTI_LINE_STYLE);
|
||||
}
|
||||
}
|
||||
|
|
@ -23,34 +23,34 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
public class PersonTest {
|
||||
|
||||
@Test
|
||||
public void checkConstructor() {
|
||||
var p = new Person("f", "l");
|
||||
assertEquals("f", p.firstName);
|
||||
assertEquals("l", p.lastName);
|
||||
}
|
||||
@Test
|
||||
public void checkConstructor() {
|
||||
var p = new Person("f", "l");
|
||||
assertEquals("f", p.firstName);
|
||||
assertEquals("l", p.lastName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsHashCode() {
|
||||
var a = new Person("a", "b");
|
||||
var b = new Person("a", "b");
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
@Test
|
||||
public void equalsHashCode() {
|
||||
var a = new Person("a", "b");
|
||||
var b = new Person("a", "b");
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEqualsHashCode() {
|
||||
var a = new Person("a", "b");
|
||||
var b = new Person("a", "c");
|
||||
assertNotEquals(a, b);
|
||||
assertNotEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
@Test
|
||||
public void notEqualsHashCode() {
|
||||
var a = new Person("a", "b");
|
||||
var b = new Person("a", "c");
|
||||
assertNotEquals(a, b);
|
||||
assertNotEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasToString() {
|
||||
var actual = new Person("a", "b").toString();
|
||||
assertTrue(actual.contains(Person.class.getSimpleName()));
|
||||
assertTrue(actual.contains("\n"));
|
||||
assertTrue(actual.contains("firstName"));
|
||||
}
|
||||
@Test
|
||||
public void hasToString() {
|
||||
var actual = new Person("a", "b").toString();
|
||||
assertTrue(actual.contains(Person.class.getSimpleName()));
|
||||
assertTrue(actual.contains("\n"));
|
||||
assertTrue(actual.contains("firstName"));
|
||||
}
|
||||
}
|
||||
|
|
@ -23,36 +23,36 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
public class QuoteTest {
|
||||
|
||||
private static final Person SOME_PERSON = new Person("a", "b");
|
||||
private static final Person SOME_PERSON = new Person("a", "b");
|
||||
|
||||
@Test
|
||||
public void checkConstructor() {
|
||||
var q = new Quote(SOME_PERSON, "q");
|
||||
assertEquals(SOME_PERSON, q.person);
|
||||
assertEquals("q", q.quote);
|
||||
}
|
||||
@Test
|
||||
public void checkConstructor() {
|
||||
var q = new Quote(SOME_PERSON, "q");
|
||||
assertEquals(SOME_PERSON, q.person);
|
||||
assertEquals("q", q.quote);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsHashCode() {
|
||||
var a = new Quote(new Person("a", "b"), "c");
|
||||
var b = new Quote(new Person("a", "b"), "c");
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
@Test
|
||||
public void equalsHashCode() {
|
||||
var a = new Quote(new Person("a", "b"), "c");
|
||||
var b = new Quote(new Person("a", "b"), "c");
|
||||
assertEquals(a, b);
|
||||
assertEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEqualsHashCode() {
|
||||
var a = new Quote(new Person("a", "b"), "c");
|
||||
var b = new Quote(new Person("a", "b"), "d");
|
||||
assertNotEquals(a, b);
|
||||
assertNotEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
@Test
|
||||
public void notEqualsHashCode() {
|
||||
var a = new Quote(new Person("a", "b"), "c");
|
||||
var b = new Quote(new Person("a", "b"), "d");
|
||||
assertNotEquals(a, b);
|
||||
assertNotEquals(a.hashCode(), b.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hasToString() {
|
||||
var actual = new Quote(new Person("a", "b"), "c").toString();
|
||||
assertTrue(actual.contains(Quote.class.getSimpleName()));
|
||||
assertTrue(actual.contains("\n"));
|
||||
assertTrue(actual.contains("person"));
|
||||
}
|
||||
@Test
|
||||
public void hasToString() {
|
||||
var actual = new Quote(new Person("a", "b"), "c").toString();
|
||||
assertTrue(actual.contains(Quote.class.getSimpleName()));
|
||||
assertTrue(actual.contains("\n"));
|
||||
assertTrue(actual.contains("person"));
|
||||
}
|
||||
}
|
||||
|
|
@ -30,23 +30,23 @@ import commons.Person;
|
|||
@RequestMapping("/api/people")
|
||||
public class PersonListingController {
|
||||
|
||||
private List<Person> people = new LinkedList<>();
|
||||
private List<Person> people = new LinkedList<>();
|
||||
|
||||
public PersonListingController() {
|
||||
people.add(new Person("Mickey", "Mouse"));
|
||||
people.add(new Person("Donald", "Duck"));
|
||||
}
|
||||
public PersonListingController() {
|
||||
people.add(new Person("Mickey", "Mouse"));
|
||||
people.add(new Person("Donald", "Duck"));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Person> list() {
|
||||
return people;
|
||||
}
|
||||
@GetMapping("/")
|
||||
public List<Person> list() {
|
||||
return people;
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public List<Person> add(@RequestBody Person p) {
|
||||
if (!people.contains(p)) {
|
||||
people.add(p);
|
||||
}
|
||||
return people;
|
||||
}
|
||||
@PostMapping("/")
|
||||
public List<Person> add(@RequestBody Person p) {
|
||||
if (!people.contains(p)) {
|
||||
people.add(p);
|
||||
}
|
||||
return people;
|
||||
}
|
||||
}
|
||||
|
|
@ -26,28 +26,28 @@ import commons.Person;
|
|||
|
||||
public class PersonListingControllerTest {
|
||||
|
||||
private static final Person MICKEY = new Person("Mickey", "Mouse");
|
||||
private static final Person DONALD = new Person("Donald", "Duck");
|
||||
private static final Person SCROOGE = new Person("Scrooge", "McDuck");
|
||||
private static final Person MICKEY = new Person("Mickey", "Mouse");
|
||||
private static final Person DONALD = new Person("Donald", "Duck");
|
||||
private static final Person SCROOGE = new Person("Scrooge", "McDuck");
|
||||
|
||||
private PersonListingController sut;
|
||||
private PersonListingController sut;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
sut = new PersonListingController();
|
||||
}
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
sut = new PersonListingController();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containsTwoDefaultNames() {
|
||||
var actual = sut.list();
|
||||
var expected = List.of(MICKEY, DONALD);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@Test
|
||||
public void containsTwoDefaultNames() {
|
||||
var actual = sut.list();
|
||||
var expected = List.of(MICKEY, DONALD);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canAddPeople() {
|
||||
var actual = sut.add(SCROOGE);
|
||||
var expected = List.of(MICKEY, DONALD, SCROOGE);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
@Test
|
||||
public void canAddPeople() {
|
||||
var actual = sut.add(SCROOGE);
|
||||
var expected = List.of(MICKEY, DONALD, SCROOGE);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue