forked from UNN-ITMM-Software/agile-course-practice-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
405 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
68 changes: 68 additions & 0 deletions
68
...oflife/Infrastructure/src/main/java/ru/unn/agile/gameoflife/infrastructure/TxtLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} > .*")); | ||
} | ||
} |
13 changes: 11 additions & 2 deletions
13
...ure/src/test/java/ru/unn/agile/gameoflife/infrastructure/ViewModelWithTxtLoggerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
code/gameoflife/ViewModel/src/main/java/ru/unn/agile/gameoflife/viewmodel/ILogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletion
16
code/gameoflife/ViewModel/src/test/java/ru/unn/agile/gameoflife/viewmodel/FakeLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.