Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hw2 #3

Merged
merged 9 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/edu/hw2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.hw2;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class Main {
bifidok marked this conversation as resolved.
Show resolved Hide resolved
private final static Logger LOGGER = LogManager.getLogger();

private Main() {
}

public static void main(String[] args) {
}
}
40 changes: 40 additions & 0 deletions src/main/java/edu/hw2/Task1/Expr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.hw2.Task1;
bifidok marked this conversation as resolved.
Show resolved Hide resolved

public sealed interface Expr {
double evaluate();

record Constant(double value) implements Expr {
@Override
public double evaluate() {
return value;
}
}

public record Negate(Expr value) implements Expr {
@Override
public double evaluate() {
return value.evaluate() * -1;
}
}

record Exponent(Expr value, double exponent) implements Expr {
@Override
public double evaluate() {
return Math.pow(value.evaluate(), exponent);
}
}

record Addition(Expr value1, Expr value2) implements Expr {
@Override
public double evaluate() {
return value1.evaluate() + value2.evaluate();
}
}

record Multiplication(Expr value1, Expr value2) implements Expr {
@Override
public double evaluate() {
return value1.evaluate() * value2.evaluate();
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/edu/hw2/Task2/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.hw2.Task2;

public class Rectangle {
private int width;
private int height;

public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}

public Rectangle setWidth(int width) {
return new Rectangle(width, height);
}

public Rectangle setHeight(int height) {
return new Rectangle(width, height);
}

public double area() {
return width * height;
}
}
17 changes: 17 additions & 0 deletions src/main/java/edu/hw2/Task2/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.hw2.Task2;

public class Square extends Rectangle {
public Square(int sideLength) {
super(sideLength, sideLength);
}

@Override
public Rectangle setWidth(int width) {
return new Square(width);
}

@Override
public Rectangle setHeight(int height) {
return new Square(height);
}
}
11 changes: 11 additions & 0 deletions src/main/java/edu/hw2/Task3/Connection/Connection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package edu.hw2.Task3.Connection;

public interface Connection extends AutoCloseable {
double PROBABLITY_BOUND = 0.5;

void execute(String command);

default boolean isConnectionFailed() {
return Math.random() > PROBABLITY_BOUND;
}
}
22 changes: 22 additions & 0 deletions src/main/java/edu/hw2/Task3/Connection/FaultyConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.hw2.Task3.Connection;

import edu.hw2.Task3.ConnectionException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class FaultyConnection implements Connection {
private final static Logger LOGGER = LogManager.getLogger();

@Override
public void execute(String command) {
if (isConnectionFailed()) {
throw new ConnectionException("Connection cannot be established", new Exception());
}
LOGGER.info(command);
}

@Override
public void close() {
LOGGER.info("Connection closed");
}
}
18 changes: 18 additions & 0 deletions src/main/java/edu/hw2/Task3/Connection/StableConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.hw2.Task3.Connection;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class StableConnection implements Connection {
private final static Logger LOGGER = LogManager.getLogger();

@Override
public void execute(String command) {
LOGGER.info(command);
}

@Override
public void close() {
LOGGER.info("Connection closed!");
}
}
7 changes: 7 additions & 0 deletions src/main/java/edu/hw2/Task3/ConnectionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.hw2.Task3;

public class ConnectionException extends RuntimeException {
public ConnectionException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package edu.hw2.Task3.ConnectionManager;

import edu.hw2.Task3.Connection.Connection;

public interface ConnectionManager {
double PROBABLITY_BOUND = 0.5;

Connection getConnection();

default boolean isFaultyConnection() {
return Math.random() > PROBABLITY_BOUND;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.hw2.Task3.ConnectionManager;

import edu.hw2.Task3.Connection.Connection;
import edu.hw2.Task3.Connection.FaultyConnection;
import edu.hw2.Task3.Connection.StableConnection;

public class DefaultConnectionManager implements ConnectionManager {
@Override
public Connection getConnection() {
if (isFaultyConnection()) {
return new FaultyConnection();
}
return new StableConnection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package edu.hw2.Task3.ConnectionManager;

import edu.hw2.Task3.Connection.Connection;
import edu.hw2.Task3.Connection.FaultyConnection;

public class FaultyConnectionManager implements ConnectionManager {
@Override
public Connection getConnection() {
return new FaultyConnection();
}
}
42 changes: 42 additions & 0 deletions src/main/java/edu/hw2/Task3/PopularCommandExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.hw2.Task3;

import edu.hw2.Task3.Connection.Connection;
import edu.hw2.Task3.ConnectionManager.ConnectionManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class PopularCommandExecutor {
private final ConnectionManager manager;
private final int maxAttemps;
private final static Logger LOGGER = LogManager.getLogger();

public PopularCommandExecutor(ConnectionManager manager, int maxAttemps) {
this.manager = manager;
this.maxAttemps = maxAttemps;
}

public void updatePackages() {
tryExecute("apt update && apt upgrade -y");
}

private void tryExecute(String command) {
int attemps = 0;
while (attemps < maxAttemps) {
Connection connection = manager.getConnection();
try {
connection.execute(command);
} catch (ConnectionException e) {
if (attemps == maxAttemps - 1) {
LOGGER.info(e.getMessage());
}
attemps++;
} finally {
try {
connection.close();
} catch (Exception closeException) {
LOGGER.info(connection.toString() + " didnt close!");
}
}
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/edu/hw2/Task4/Call.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.hw2.Task4;

public class Call {
private Call() {
}

public static CallingInfo callingInfo() {
var stackTrace = new Throwable().getStackTrace();
int stackLength = stackTrace.length;
String callerMethodName = stackTrace[1].getMethodName();
String callerClassName = stackTrace[1].getClassName();
return new CallingInfo(callerClassName, callerMethodName);
}
}
4 changes: 4 additions & 0 deletions src/main/java/edu/hw2/Task4/CallingInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package edu.hw2.Task4;

public record CallingInfo(String className, String methodName) {
}
23 changes: 23 additions & 0 deletions src/test/java/edu/hw2/CallingInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package edu.hw2;

import edu.hw2.Task4.Call;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class CallingInfoTest {
@Test
@DisplayName("Вызов метода из CallingInfoTest")
public void callingInfo_ShouldReturnThisClassAndMethodName() {
var info = Call.callingInfo();

String className = this.getClass().getName();
String methodName = new Object() {}
.getClass()
.getEnclosingMethod()
.getName();

assertThat(info.className()).isEqualTo(className);
assertThat(info.methodName()).isEqualTo(methodName);
}
}
38 changes: 38 additions & 0 deletions src/test/java/edu/hw2/ExprTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package edu.hw2;
bifidok marked this conversation as resolved.
Show resolved Hide resolved

import edu.hw2.Task1.Expr;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class ExprTest {
@Test
@DisplayName("Базовый тест")
void expr_shouldCorrectlyEvaluate_basicTest() {
var two = new Expr.Constant(2);
var four = new Expr.Constant(4);
var negOne = new Expr.Negate(new Expr.Constant(1));
var sumTwoFour = new Expr.Addition(two, four);
var mult = new Expr.Multiplication(sumTwoFour, negOne);
var exp = new Expr.Exponent(mult, 2);
var res = new Expr.Addition(exp, new Expr.Constant(1));

assertThat(res.evaluate()).isEqualTo(37.0);
}

@Test
@DisplayName("Отрицательное число в Negate")
void expr_shouldEvaluateMinusOne_whenValueAlreadyNagative() {
var negOne = new Expr.Negate(new Expr.Constant(-1));

assertThat(negOne.evaluate()).isEqualTo(1);
}

@Test
@DisplayName("Отрицательные числа в Exponent")
void expr_shouldExponentCorrectly_whenValueAndExponentNegative() {
var negOne = new Expr.Exponent(new Expr.Constant(-2), -5);

assertThat(negOne.evaluate()).isEqualTo(-0.03125);
}
}
38 changes: 38 additions & 0 deletions src/test/java/edu/hw2/RectangleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package edu.hw2;

import edu.hw2.Task2.Rectangle;
import edu.hw2.Task2.Square;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class RectangleTest {
@Test
@DisplayName("Проверка LSP")
public void squareArea_shouldReturnCorrectSquareArea() {
Rectangle square = new Square(30);
double area = square.area();
assertThat(area).isEqualTo(900);
}

@Test
@DisplayName("Квадрату устанавливается ширина и высота")
public void squareArea_shouldReturnCorrectSquareArea_whenSquareHasWidthAndHeight() {
Rectangle square = new Square(30);
square = square.setHeight(99);
square = square.setWidth(20);
double area = square.area();
assertThat(area).isEqualTo(400);
}

@Test
@DisplayName("Квадрату устанавливается ширина и высота, но объект не изменяется")
public void squareArea_shouldReturnInitialSquareArea() {
Rectangle square = new Square(30);
square.setHeight(99);
square.setWidth(20);
double area = square.area();
assertThat(area).isEqualTo(900);
}

}