Java 11 migraiton: model-view-controller
This commit is contained in:
@ -47,9 +47,9 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// create model, view and controller
|
||||
GiantModel giant = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
GiantView view = new GiantView();
|
||||
GiantController controller = new GiantController(giant, view);
|
||||
var giant = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
var view = new GiantView();
|
||||
var controller = new GiantController(giant, view);
|
||||
// initial display
|
||||
controller.updateView();
|
||||
// controller receives some interactions that affect the giant
|
||||
|
@ -30,7 +30,7 @@ public enum Fatigue {
|
||||
|
||||
ALERT("alert"), TIRED("tired"), SLEEPING("sleeping");
|
||||
|
||||
private String title;
|
||||
private final String title;
|
||||
|
||||
Fatigue(String title) {
|
||||
this.title = title;
|
||||
|
@ -28,14 +28,15 @@ package com.iluwatar.model.view.controller;
|
||||
*/
|
||||
public class GiantController {
|
||||
|
||||
private GiantModel giant;
|
||||
private GiantView view;
|
||||
private final GiantModel giant;
|
||||
private final GiantView view;
|
||||
|
||||
public GiantController(GiantModel giant, GiantView view) {
|
||||
this.giant = giant;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public Health getHealth() {
|
||||
return giant.getHealth();
|
||||
}
|
||||
@ -44,6 +45,7 @@ public class GiantController {
|
||||
this.giant.setHealth(health);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public Fatigue getFatigue() {
|
||||
return giant.getFatigue();
|
||||
}
|
||||
@ -52,6 +54,7 @@ public class GiantController {
|
||||
this.giant.setFatigue(fatigue);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public Nourishment getNourishment() {
|
||||
return giant.getNourishment();
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public enum Health {
|
||||
|
||||
HEALTHY("healthy"), WOUNDED("wounded"), DEAD("dead");
|
||||
|
||||
private String title;
|
||||
private final String title;
|
||||
|
||||
Health(String title) {
|
||||
this.title = title;
|
||||
|
@ -30,7 +30,7 @@ public enum Nourishment {
|
||||
|
||||
SATURATED("saturated"), HUNGRY("hungry"), STARVING("starving");
|
||||
|
||||
private String title;
|
||||
private final String title;
|
||||
|
||||
Nourishment(String title) {
|
||||
this.title = title;
|
||||
|
@ -26,15 +26,12 @@ package com.iluwatar.model.view.controller;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -23,13 +23,13 @@
|
||||
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/20/15 - 2:19 PM
|
||||
*
|
||||
@ -42,19 +42,20 @@ public class GiantControllerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSetHealth() {
|
||||
final GiantModel model = mock(GiantModel.class);
|
||||
final GiantView view = mock(GiantView.class);
|
||||
final GiantController controller = new GiantController(model, view);
|
||||
final var model = mock(GiantModel.class);
|
||||
final var view = mock(GiantView.class);
|
||||
final var controller = new GiantController(model, view);
|
||||
|
||||
verifyZeroInteractions(model, view);
|
||||
|
||||
for (final Health health : Health.values()) {
|
||||
for (final var health : Health.values()) {
|
||||
controller.setHealth(health);
|
||||
verify(model).setHealth(health);
|
||||
verifyZeroInteractions(view);
|
||||
}
|
||||
|
||||
controller.getHealth();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
verify(model).getHealth();
|
||||
|
||||
verifyNoMoreInteractions(model, view);
|
||||
@ -65,19 +66,20 @@ public class GiantControllerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSetFatigue() {
|
||||
final GiantModel model = mock(GiantModel.class);
|
||||
final GiantView view = mock(GiantView.class);
|
||||
final GiantController controller = new GiantController(model, view);
|
||||
final var model = mock(GiantModel.class);
|
||||
final var view = mock(GiantView.class);
|
||||
final var controller = new GiantController(model, view);
|
||||
|
||||
verifyZeroInteractions(model, view);
|
||||
|
||||
for (final Fatigue fatigue : Fatigue.values()) {
|
||||
for (final var fatigue : Fatigue.values()) {
|
||||
controller.setFatigue(fatigue);
|
||||
verify(model).setFatigue(fatigue);
|
||||
verifyZeroInteractions(view);
|
||||
}
|
||||
|
||||
controller.getFatigue();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
verify(model).getFatigue();
|
||||
|
||||
verifyNoMoreInteractions(model, view);
|
||||
@ -88,19 +90,20 @@ public class GiantControllerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSetNourishment() {
|
||||
final GiantModel model = mock(GiantModel.class);
|
||||
final GiantView view = mock(GiantView.class);
|
||||
final GiantController controller = new GiantController(model, view);
|
||||
final var model = mock(GiantModel.class);
|
||||
final var view = mock(GiantView.class);
|
||||
final var controller = new GiantController(model, view);
|
||||
|
||||
verifyZeroInteractions(model, view);
|
||||
|
||||
for (final Nourishment nourishment : Nourishment.values()) {
|
||||
for (final var nourishment : Nourishment.values()) {
|
||||
controller.setNourishment(nourishment);
|
||||
verify(model).setNourishment(nourishment);
|
||||
verifyZeroInteractions(view);
|
||||
}
|
||||
|
||||
controller.getNourishment();
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
verify(model).getNourishment();
|
||||
|
||||
verifyNoMoreInteractions(model, view);
|
||||
@ -108,9 +111,9 @@ public class GiantControllerTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateView() {
|
||||
final GiantModel model = mock(GiantModel.class);
|
||||
final GiantView view = mock(GiantView.class);
|
||||
final GiantController controller = new GiantController(model, view);
|
||||
final var model = mock(GiantModel.class);
|
||||
final var view = mock(GiantView.class);
|
||||
final var controller = new GiantController(model, view);
|
||||
|
||||
verifyZeroInteractions(model, view);
|
||||
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.model.view.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Date: 12/20/15 - 2:10 PM
|
||||
*
|
||||
@ -39,12 +39,13 @@ public class GiantModelTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSetHealth() {
|
||||
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
final var model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.HUNGRY);
|
||||
assertEquals(Health.HEALTHY, model.getHealth());
|
||||
for (final Health health : Health.values()) {
|
||||
for (final var health : Health.values()) {
|
||||
model.setHealth(health);
|
||||
assertEquals(health, model.getHealth());
|
||||
assertEquals("The giant looks " + health.toString() + ", alert and saturated.", model.toString());
|
||||
assertEquals("The giant looks " + health.toString() + ", alert and saturated.", model
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,12 +54,13 @@ public class GiantModelTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSetFatigue() {
|
||||
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
final var model = new GiantModel(Health.WOUNDED, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
assertEquals(Fatigue.ALERT, model.getFatigue());
|
||||
for (final Fatigue fatigue : Fatigue.values()) {
|
||||
for (final var fatigue : Fatigue.values()) {
|
||||
model.setFatigue(fatigue);
|
||||
assertEquals(fatigue, model.getFatigue());
|
||||
assertEquals("The giant looks healthy, " + fatigue.toString() + " and saturated.", model.toString());
|
||||
assertEquals("The giant looks healthy, " + fatigue.toString() + " and saturated.", model
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,12 +69,13 @@ public class GiantModelTest {
|
||||
*/
|
||||
@Test
|
||||
public void testSetNourishment() {
|
||||
final GiantModel model = new GiantModel(Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
|
||||
final var model = new GiantModel(Health.HEALTHY, Fatigue.TIRED, Nourishment.SATURATED);
|
||||
assertEquals(Nourishment.SATURATED, model.getNourishment());
|
||||
for (final Nourishment nourishment : Nourishment.values()) {
|
||||
for (final var nourishment : Nourishment.values()) {
|
||||
model.setNourishment(nourishment);
|
||||
assertEquals(nourishment, model.getNourishment());
|
||||
assertEquals("The giant looks healthy, alert and " + nourishment.toString() + ".", model.toString());
|
||||
assertEquals("The giant looks healthy, alert and " + nourishment.toString() + ".", model
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,6 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -62,9 +61,9 @@ public class GiantViewTest {
|
||||
*/
|
||||
@Test
|
||||
public void testDisplayGiant() {
|
||||
final GiantView view = new GiantView();
|
||||
final var view = new GiantView();
|
||||
|
||||
final GiantModel model = mock(GiantModel.class);
|
||||
final var model = mock(GiantModel.class);
|
||||
view.displayGiant(model);
|
||||
|
||||
assertEquals(model.toString(), appender.getLastMessage());
|
||||
@ -74,10 +73,10 @@ public class GiantViewTest {
|
||||
/**
|
||||
* Logging Appender Implementation
|
||||
*/
|
||||
public class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
private List<ILoggingEvent> log = new LinkedList<>();
|
||||
public static class InMemoryAppender extends AppenderBase<ILoggingEvent> {
|
||||
private final List<ILoggingEvent> log = new LinkedList<>();
|
||||
|
||||
public InMemoryAppender(Class clazz) {
|
||||
public InMemoryAppender(Class<?> clazz) {
|
||||
((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
Reference in New Issue
Block a user