Skip to content

Commit

Permalink
Merge pull request UNN-ITMM-Software#155 from SmirnovEgorRu/lab4
Browse files Browse the repository at this point in the history
Смирнов -- Лабораторная работа UNN-ITMM-Software#3
  • Loading branch information
vgvoleg authored Jan 7, 2020
2 parents 25602bb + 094055e commit 399c765
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 10 deletions.
4 changes: 4 additions & 0 deletions code/polynomial-calculator/Infrastructure/build.gradle
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
}
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());
}
}
}
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);
}
}
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));
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import javafx.collections.ObservableList;
import ru.unn.agile.polynomialcalculator.model.Polynomial;

import java.util.List;
import java.util.regex.Pattern;

public class ViewModel {
private final StringProperty degree1 = new SimpleStringProperty();
private final StringProperty coeff1 = new SimpleStringProperty();

private ILogger logger;

private static final Pattern COEFF_INPUT_ALLOWED_SYMBOLS =
Pattern.compile("^[-+]?[0-9]+\\.?[0-9]*$");

Expand All @@ -32,11 +35,20 @@ public boolean isPolynomialInputCorrect() {
String degree = degree1.get();
String coeff = coeff1.get();

return (COEFF_INPUT_ALLOWED_SYMBOLS.matcher(coeff).matches()
var res = (COEFF_INPUT_ALLOWED_SYMBOLS.matcher(coeff).matches()
&& DEGREE_INPUT_ALLOWED_SYMBOLS.matcher(degree).matches());
if (!res) {
logger.addLog("Check of polynomial is failed: degree = "
+ degree + ", coeff = " + coeff);
} else {
logger.addLog("Check of polynomial is passed: degree = "
+ degree + ", coeff = " + coeff);
}
return res;
}

public ViewModel() {
public ViewModel(final ILogger log) {
logger = log;
polynomialsListProperty.set(FXCollections.observableArrayList());
clearFormInput();
BooleanBinding canCalculateBoolBinding = new BooleanBinding() {
Expand All @@ -54,27 +66,35 @@ public void addPolynomial() {
var polynomial = parsePolynomial(coeff1, degree1);
polynomialsList.add(polynomial);
polynomialsListProperty.add(polynomial.toString());
logger.addLog("Added new polynomial: " + polynomial.toString());
logger.addLog("Updated list of polynomials: " + polynomialsList.toString());
clearFormInput();
}

public void calcPolynomialAdd() {
if (polynomialsList.isEmpty()) {
logger.addLog("Polynomial add can't be calculated due to empty list");
return;
}
logger.addLog("Trying to compute sum of farther polynomials: "
+ polynomialsList.toString());
try {
Polynomial polySum = new Polynomial(0, 0);
for (Polynomial poly : polynomialsList) {
polySum = polySum.plus(poly);
}
result.set(polySum.toString());
} catch (IllegalArgumentException e) {
logger.addLog("Result is " + polySum.toString());
} catch (Exception e) {
logger.addLog("Polynomial add can't be calculated: " + e.getMessage());
result.setValue(e.getMessage());
}
}

private void clearFormInput() {
degree1.set("");
coeff1.set("");
logger.addLog("Clear input");
}

private Polynomial parsePolynomial(final StringProperty coeff, final StringProperty degree) {
Expand All @@ -93,11 +113,17 @@ public StringProperty coeffProperty() {
public ObservableList<Polynomial> getPolynomialsList() {
return polynomialsList;
}

public StringProperty resultProperty() {
return result;
}
public final String getResult() {
return result.get();
}

public List<String> getLog() {
try {
return logger.getLogMessage();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
}
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);
}
}
Loading

0 comments on commit 399c765

Please sign in to comment.