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.
Merge pull request UNN-ITMM-Software#155 from SmirnovEgorRu/lab4
Смирнов -- Лабораторная работа UNN-ITMM-Software#3
- Loading branch information
Showing
10 changed files
with
294 additions
and
10 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(':polynomial-calculator-viewmodel') | ||
testCompile project(':polynomial-calculator-viewmodel').sourceSets.test.output | ||
} |
60 changes: 60 additions & 0 deletions
60
...rastructure/src/main/java/ru/unn/agile/polynomialcalculator/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,60 @@ | ||
package ru.unn.agile.polynomialcalculator.infrastructure; | ||
|
||
import ru.unn.agile.polynomialcalculator.viewmodel.ILogger; | ||
|
||
import java.io.*; | ||
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 String fileName; | ||
private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; | ||
private final BufferedWriter writer; | ||
|
||
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) { | ||
System.out.println(e.getMessage()); | ||
} | ||
writer = logWriter; | ||
} | ||
|
||
@Override | ||
public List<String> getLogMessage() { | ||
ArrayList<String> log = new ArrayList<String>(); | ||
try { | ||
BufferedReader reader = new BufferedReader(new FileReader(fileName)); | ||
var text = reader.readLine(); | ||
while (text != null) { | ||
log.add(text); | ||
text = reader.readLine(); | ||
} | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
return log; | ||
} | ||
|
||
@Override | ||
public void addLog(final String message) { | ||
try { | ||
writer.write(now() + " << " + message + '\n'); | ||
writer.flush(); | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...ucture/src/test/java/ru/unn/agile/polynomialcalculator/infrastructure/TxtLoggerTests.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,69 @@ | ||
package ru.unn.agile.polynomialcalculator.infrastructure; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static junit.framework.TestCase.assertNotNull; | ||
import static org.junit.Assert.*; | ||
|
||
public class TxtLoggerTests { | ||
private static final String FILENAME = "./TxtLoggerTests-polynomial-calculator.log"; | ||
private TxtLogger txtLogger; | ||
|
||
@Before | ||
public void setUp() { | ||
txtLogger = new TxtLogger(FILENAME); | ||
} | ||
|
||
@Test | ||
public void canLogSeveralMessages() { | ||
String m1 = "0"; | ||
String m2 = "1"; | ||
String m3 = "2"; | ||
|
||
txtLogger.addLog(m1); | ||
txtLogger.addLog(m2); | ||
txtLogger.addLog(m3); | ||
|
||
List<String> log = txtLogger.getLogMessage(); | ||
|
||
for (int i = 0; i < log.size(); i++) { | ||
assertTrue(log.get(i).matches(".*" + Integer.toString(i) + ".*")); | ||
} | ||
} | ||
|
||
@Test | ||
public void canGetLogMessages() { | ||
String m = "Test"; | ||
txtLogger.addLog(m); | ||
|
||
assertNotNull(txtLogger.getLogMessage()); | ||
} | ||
|
||
@Test | ||
public void canLogOneMessage() { | ||
String m = "Test logger"; | ||
|
||
txtLogger.addLog(m); | ||
|
||
List<String> log = txtLogger.getLogMessage(); | ||
assertTrue(log.get(0).matches(".*" + "Test logger" + ".*")); | ||
} | ||
|
||
@Test | ||
public void doesLogContainDateAndTime() { | ||
String msg1 = "Test"; | ||
txtLogger.addLog(msg1); | ||
|
||
List<String> log = txtLogger.getLogMessage(); | ||
|
||
assertTrue(log.get(0).matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} << .*")); | ||
} | ||
|
||
@Test | ||
public void canCreateLoggerWithFileName() { | ||
assertNotNull(txtLogger); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...st/java/ru/unn/agile/polynomialcalculator/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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ru.unn.agile.polynomialcalculator.infrastructure; | ||
|
||
import ru.unn.agile.polynomialcalculator.viewmodel.ViewModelTests; | ||
import ru.unn.agile.polynomialcalculator.viewmodel.ViewModel; | ||
|
||
public class ViewModelWithTxtLoggerTests extends ViewModelTests { | ||
@Override | ||
public void setUp() { | ||
TxtLogger realLogger = new TxtLogger( | ||
"ViewModelWithTxtLoggerTests-polynomial-calculator.log"); | ||
setViewModel(new ViewModel(realLogger)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...lculator/ViewModel/src/main/java/ru/unn/agile/polynomialcalculator/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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.unn.agile.polynomialcalculator.viewmodel; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public interface ILogger { | ||
void addLog(String message); | ||
|
||
List<String> getLogMessage() throws IOException; | ||
} |
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
18 changes: 18 additions & 0 deletions
18
...lator/ViewModel/src/test/java/ru/unn/agile/polynomialcalculator/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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.unn.agile.polynomialcalculator.viewmodel; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class FakeLogger implements ILogger { | ||
private ArrayList<String> logList = new ArrayList<String>(); | ||
|
||
@Override | ||
public List<String> getLogMessage() { | ||
return logList; | ||
} | ||
|
||
@Override | ||
public void addLog(final String message) { | ||
logList.add(message); | ||
} | ||
} |
Oops, something went wrong.