Skip to content

Commit

Permalink
bla
Browse files Browse the repository at this point in the history
  • Loading branch information
Folifolo committed Jan 15, 2020
1 parent 3a634e5 commit 71ac1b5
Show file tree
Hide file tree
Showing 14 changed files with 405 additions and 17 deletions.
4 changes: 4 additions & 0 deletions code/gameoflife/Infrastructure/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
compile project(':GameOfLife-ViewModel')
testCompile project(':GameOfLife-ViewModel').sourceSets.test.output
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ru.unn.agile.gameoflife.infrastructure;

import ru.unn.agile.gameoflife.viewmodel.ILogger;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class TxtLogger implements ILogger {
private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
private final BufferedWriter writer;
private final String filename;

private static String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW, Locale.ENGLISH);
return sdf.format(cal.getTime());
}

public TxtLogger(final String filename) {
this.filename = filename;

BufferedWriter logWriter = null;
try {
logWriter = new BufferedWriter(new FileWriter(filename));
} catch (Exception e) {
e.printStackTrace();
}
writer = logWriter;
}

@Override
public void log(final String s) {
try {
writer.write(now() + " > " + s);
writer.newLine();
writer.flush();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

@Override
public List<String> getLog() {
BufferedReader reader;
ArrayList<String> log = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();

while (line != null) {
log.add(line);
line = reader.readLine();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}

return log;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ru.unn.agile.gameoflife.infrastructure;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.MatcherAssert.assertThat;

public class TxtLoggerTests {
private static final String FILENAME = "./TxtLogger_Tests-lab3.log";
private TxtLogger txtLogger;

@Before
public void setUp() {
txtLogger = new TxtLogger(FILENAME);
}

@Test
public void canCreateLoggerWithFileName() {
assertNotNull(txtLogger);
}

@Test
public void canCreateLogFileOnDisk() {
try {
new BufferedReader(new FileReader(FILENAME));
} catch (FileNotFoundException e) {
fail("File " + FILENAME + " wasn't found!");
}
}

@Test
public void canWriteLogMessage() {
String testMessage = "Test message";

txtLogger.log(testMessage);

String message = txtLogger.getLog().get(0);
assertTrue(message.matches(".*" + testMessage + "$"));
}

@Test
public void canWriteSeveralLogMessage() {
String[] messages = {"Test message 1", "Test message 2"};

txtLogger.log(messages[0]);
txtLogger.log(messages[1]);

List<String> actualMessages = txtLogger.getLog();
for (int i = 0; i < actualMessages.size(); i++) {
assertTrue(actualMessages.get(i).matches(".*" + messages[i] + "$"));
}
}

@Test
public void doesLogContainDateAndTime() {
String testMessage = "Test message";

txtLogger.log(testMessage);

String message = txtLogger.getLog().get(0);
assertTrue(message.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} > .*"));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package ru.unn.agile.gameoflife.infrastructure;

public class ViewModelWithTxtLoggerTests {
}
import ru.unn.agile.gameoflife.viewmodel.ViewModel;
import ru.unn.agile.gameoflife.viewmodel.ViewModelTest;

public class ViewModelWithTxtLoggerTests extends ViewModelTest {
@Override
public void setUp() {
TxtLogger logger =
new TxtLogger("./ViewModelWithTxtLoggerTests.log");
super.setViewModel(new ViewModel(logger));
}
}
Empty file.
Empty file.
1 change: 1 addition & 0 deletions code/gameoflife/View/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ javafx {

dependencies {
compile project(':GameOfLife-ViewModel')
compile project(':GameOfLife-Infrastructure')
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import ru.unn.agile.gameoflife.viewmodel.ViewModel;
import ru.unn.agile.gameoflife.infrastructure.TxtLogger;

public class GameOfLife {
private static final float GAME_FIELD_SIZE = 400;
private static final float GAME_FIELD_SIZE = 370;
private float cellWidth;
private float cellHeight;

Expand Down Expand Up @@ -71,6 +72,8 @@ private void gridGameUpdate() {

@FXML
void initialize() {
viewModel.setLogger(new TxtLogger("./TxtLogger-gameoflife.log"));

txtHeight.textProperty().bindBidirectional(viewModel.heightFieldProperty());
txtwidth.textProperty().bindBidirectional(viewModel.widthFieldProperty());
txtStatus.textProperty().bindBidirectional(viewModel.statusTextProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import ru.unn.agile.gameoflife.viewmodel.*?>

<AnchorPane prefHeight="510.0" prefWidth="410.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.unn.agile.gameoflife.view.GameOfLife">
<fx:define>
<ViewModel fx:id="viewModel" />
</fx:define>
<AnchorPane prefHeight="510.0" prefWidth="573.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ru.unn.agile.gameoflife.view.GameOfLife">
<fx:define>
<ViewModel fx:id="viewModel" />
</fx:define>
<children>
<Label layoutX="26.0" layoutY="14.0" text="Height of field" AnchorPane.topAnchor="14.0" />
<Label layoutX="206.0" layoutY="14.0" text="Width of field" AnchorPane.topAnchor="14.0" />
Expand All @@ -17,6 +18,7 @@
<Button fx:id="btnNext" layoutX="81.0" layoutY="67.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="94.0" text="Next" AnchorPane.leftAnchor="262.0" AnchorPane.topAnchor="67.0" />
<TextField fx:id="txtHeight" layoutX="111.0" layoutY="10.0" prefHeight="25.0" prefWidth="77.0" AnchorPane.topAnchor="10.0" />
<TextField fx:id="txtwidth" layoutX="290.0" layoutY="10.0" prefHeight="25.0" prefWidth="77.0" AnchorPane.topAnchor="10.0" />
<GridPane fx:id="gridGame" layoutX="58.0" layoutY="232.0" prefHeight="226.0" prefWidth="253.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="105.0" />
<GridPane fx:id="gridGame" layoutX="58.0" layoutY="232.0" prefHeight="400.0" prefWidth="373.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="195.0" AnchorPane.topAnchor="105.0" />
<TextArea fx:id="areaLog" text="${viewModel.logs}" editable="false" layoutX="395.0" layoutY="12.0" prefHeight="487.0" prefWidth="165.0" />
</children>
</AnchorPane>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package ru.unn.agile.gameoflife.viewmodel;

public class ILogger {
}
import java.util.List;

public interface ILogger {
void log(String s);

List<String> getLog();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,37 @@
import java.util.List;

public class ViewModel {
private SimpleStringProperty heightField = new SimpleStringProperty();
private SimpleStringProperty widthField = new SimpleStringProperty();
private SimpleStringProperty statusText = new SimpleStringProperty();
private SimpleBooleanProperty couldNotCreate = new SimpleBooleanProperty();
private SimpleBooleanProperty couldNotGetNextStep = new SimpleBooleanProperty();
private final SimpleStringProperty heightField = new SimpleStringProperty();
private final SimpleStringProperty widthField = new SimpleStringProperty();
private final SimpleStringProperty statusText = new SimpleStringProperty();
private final SimpleBooleanProperty couldNotCreate = new SimpleBooleanProperty();
private final SimpleBooleanProperty couldNotGetNextStep = new SimpleBooleanProperty();
private final StringProperty logs = new SimpleStringProperty();

private ILogger logger;

private GameOfLife gameOfLife;
private char[][] gridArray;

private final List<ValueChangeListener> valueChangedListeners = new ArrayList<>();

public final void setLogger(final ILogger logger) {
if (logger == null) {
throw new IllegalArgumentException("Logger parameter can't be null");
}
this.logger = logger;
}

public ViewModel() {
init();
}

public ViewModel(final ILogger logger) {
setLogger(logger);
init();
}

private void init() {
statusText.set(Status.WAITING.toString());
couldNotGetNextStep.set(true);
couldNotCreate.set(true);
Expand All @@ -41,6 +60,18 @@ public ViewModel() {
}
}

public StringProperty logsProperty() {
return logs;
}

public final String getLogs() {
return logs.get();
}

public final List<String> getLog() {
return logger.getLog();
}

public SimpleStringProperty heightFieldProperty() {
return heightField;
}
Expand Down Expand Up @@ -72,6 +103,10 @@ public void createGrid() {

gridArray = gameOfLife.getGrid().clone();
statusText.set(Status.INITIALISE.toString());
StringBuilder message = new StringBuilder(LogMessages.CREATE_WAS_PRESSED);
message.append("Size: height = ").append(heightField.get())
.append("; width = ").append(widthField.get());
logger.log(message.toString());
}
}

Expand Down Expand Up @@ -101,10 +136,23 @@ public void changeCellStatus(final int y, final int x) {
gameOfLife.setCell(y, x);
gridArray = gameOfLife.getGrid().clone();
statusText.set(Status.GAMING.toString());

StringBuilder message = new StringBuilder(LogMessages.CELL_WAS_CHANGED);
message.append("Cell at ").append(y)
.append(", ").append(x)
.append(" now is alive");
logger.log(message.toString());

couldNotGetNextStep.set(false);
} else {
gameOfLife.deleteCell(y, x);
gameOverCheck();

StringBuilder message = new StringBuilder(LogMessages.CELL_WAS_CHANGED);
message.append("Cell at ").append(y)
.append(", ").append(x)
.append(" now is dead");
logger.log(message.toString());
}
gridArray = gameOfLife.getGrid().clone();
}
Expand All @@ -113,6 +161,18 @@ public void getNextStep() {
gameOfLife.makeTurn();
gridArray = gameOfLife.getGrid().clone();
gameOverCheck();

StringBuilder message = new StringBuilder(LogMessages.NEXT_WAS_PRESSED);
logger.log(message.toString());
}

private void updateLogs() {
List<String> fullLog = logger.getLog();
String record = new String("");
for (String log : fullLog) {
record += log + "\n";
}
logs.set(record);
}

private Status getInputStatus() {
Expand Down Expand Up @@ -168,3 +228,11 @@ public String toString() {
return name;
}
}

final class LogMessages {
public static final String CREATE_WAS_PRESSED = "Create grid. ";
public static final String NEXT_WAS_PRESSED = "Next turn. ";
public static final String CELL_WAS_CHANGED = "Updated cell. ";

private LogMessages() { }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
package ru.unn.agile.gameoflife.viewmodel;

public class FakeLogger {
import java.util.ArrayList;
import java.util.List;

public class FakeLogger implements ILogger {
private final ArrayList<String> log = new ArrayList<>();

@Override
public void log(final String message) {
log.add(message);
}

@Override
public List<String> getLog() {
return log;
}
}
Loading

0 comments on commit 71ac1b5

Please sign in to comment.