Skip to content

Виконані завдання з java-fundamentals-exercises #201

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix
  • Loading branch information
Kateryna0508 committed Oct 31, 2024
commit ad8d087183fa2db3544c8027bec8d6c26595ed09
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bobocode.intro;

import com.bobocode.util.ExerciseNotCompletedException;
import java.util.Base64;

/**
* Welcome! This is an introduction exercise that will show you a simple example of Bobocode exercises.
Expand All @@ -23,7 +24,7 @@ public class ExerciseIntroduction {
* @return "The key to efficient learning is practice!"
*/
public String getWelcomeMessage() {
return "The key to efficient learning is practice!";
return "The key to efficient learning is practice!";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* This is a {@link ExerciseIntroductionTest} that is meant to verify if you properly implement {@link ExerciseIntroduction}.
* This is a {@link ExerciseIntroductionTest} that is meant to verify if you properly implement {@link ExerciseIntroduction}.
* It is a simple example that shows how each exercise is organized: todo section + tests.
* <p>
* A typical Java test uses JUnit framework to run the test, and may also use some other frameworks for assertions.
Expand All @@ -22,30 +22,30 @@
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ExerciseIntroductionTest {
private ExerciseIntroduction exerciseIntroduction = new ExerciseIntroduction();
private String EXPECTED_MESSAGE = "The key to efficient learning is practice!";

@Test
@Order(1)
@DisplayName("getWelcomeMessage method returns correct phrase")
void getWelcomeMessage() {
String message = exerciseIntroduction.getWelcomeMessage();

assertThat(message).isEqualTo(EXPECTED_MESSAGE);
}

@Test
@Order(2)
@DisplayName("encodeMessage returns correct encoded message")
@SneakyThrows
void encodeMessageReturnsCorrectPhrase() {
var encodeMessageMethod = Arrays.stream(ExerciseIntroduction.class.getDeclaredMethods())
.filter(method -> method.getName().equals("encodeMessage"))
.findAny()
.orElseThrow();

var encodedMessage = encodeMessageMethod.invoke(new ExerciseIntroduction(), EXPECTED_MESSAGE);

assertThat(encodedMessage).isEqualTo("VGhlIGtleSB0byBlZmZpY2llbnQgbGVhcm5pbmcgaXMgcHJhY3RpY2Uh");
}
private ExerciseIntroduction exerciseIntroduction = new ExerciseIntroduction();
private String EXPECTED_MESSAGE = "The key to efficient learning is practice!";

@Test
@Order(1)
@DisplayName("getWelcomeMessage method returns correct phrase")
void getWelcomeMessage() {
String message = exerciseIntroduction.getWelcomeMessage();

assertThat(message).isEqualTo(EXPECTED_MESSAGE);
}

@Test
@Order(2)
@DisplayName("encodeMessage returns correct encoded message")
@SneakyThrows
void encodeMessageReturnsCorrectPhrase() {
var encodeMessageMethod = Arrays.stream(ExerciseIntroduction.class.getDeclaredMethods())
.filter(method -> method.getName().equals("encodeMessage"))
.findAny()
.orElseThrow();

var encodedMessage = encodeMessageMethod.invoke(new ExerciseIntroduction(), EXPECTED_MESSAGE);

assertThat(encodedMessage).isEqualTo("VGhlIGtleSB0byBlZmZpY2llbnQgbGVhcm5pbmcgaXMgcHJhY3RpY2Uh");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* <p>
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
*/
public class Box {
public class Box<T> {
private T value;

public Box(T value) {
Expand All @@ -21,3 +21,4 @@ public T getValue() {
public void setValue(T value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bobocode.basics;

import com.bobocode.basics.Box;

/**
* This demo demonstrates why using Object is not safe. It's not safe because runtime casting can cause runtime
* exceptions. We should always fail as soon as possible. So in this code we should not allow setting String
Expand All @@ -9,12 +11,13 @@
*/
public class BoxDemoApp {
public static void main(String[] args) {
Box<Integer> intBox = new Box<>(123);
Box<Integer> intBox2 = new Box<>(321);
System.out.println((int) intBox.getValue() + intBox2.getValue());
Box intBox = new Box(123);
Box intBox2 = new Box(321);
System.out.println((int) intBox.getValue() + (int) intBox2.getValue());

intBox.setValue(222);
// intBox.setValue("abc"); // this should not be allowed
System.out.println((int) intBox.getValue() + intBox2.getValue());
intBox.setValue("abc"); // this should not be allowed
// the following code will compile, but will throw runtime exception
System.out.println((int) intBox.getValue() + (int) intBox2.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,78 +7,78 @@

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BoxTest {
private final String TYPE_PARAMETER_NAME = "T";

@Test
@Order(1)
@DisplayName("Box class has one type parameter")
void boxClassHasOneTypeParameter() {
var typeParameters = Box.class.getTypeParameters();

assertThat(typeParameters.length).isEqualTo(1);
}

@Test
@Order(2)
@DisplayName("Type parameter is called \"T\"")
void typeParameterIsCalledT() {
var typeParameter = Box.class.getTypeParameters()[0];

assertThat(typeParameter.getName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@Order(3)
@DisplayName("Type parameter \"T\" is not bounded")
void typeParameterIsNotBounded() {
var typeParameter = Box.class.getTypeParameters()[0];

assertThat(typeParameter.getBounds()).hasSize(1);
assertThat(typeParameter.getBounds()[0].getTypeName()).isEqualTo(Object.class.getTypeName());
}

@Test
@SneakyThrows
@Order(4)
@DisplayName("Field \"value\" is \"T\"")
void valueFieldIsGeneric() {
var valueField = Box.class.getDeclaredField("value");
var genericType = valueField.getGenericType();

assertThat(genericType.getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@SneakyThrows
@Order(5)
@DisplayName("Constructor parameter type is \"T\"")
void constructorParameterIsGeneric() {
var constructor = Box.class.getDeclaredConstructors()[0];
assert (constructor.getParameters().length == 1);
var parameter = constructor.getParameters()[0];

assertThat(parameter.getParameterizedType().getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@SneakyThrows
@Order(6)
@DisplayName("Getter return type is \"T\"")
void getterReturnTypeIsGeneric() {
var getter = Box.class.getDeclaredMethod("getValue");

assertThat(getter.getGenericReturnType().getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@SneakyThrows
@Order(7)
@DisplayName("Setter parameter type is \"T\"")
void setterParameterIsGeneric() {
var setter = Box.class.getDeclaredMethod("setValue", Object.class);
assert (setter.getParameters().length == 1);
var parameter = setter.getParameters()[0];

assertThat(parameter.getParameterizedType().getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}
private final String TYPE_PARAMETER_NAME = "T";

@Test
@Order(1)
@DisplayName("Box class has one type parameter")
void boxClassHasOneTypeParameter() {
var typeParameters = Box.class.getTypeParameters();

assertThat(typeParameters.length).isEqualTo(1);
}

@Test
@Order(2)
@DisplayName("Type parameter is called \"T\"")
void typeParameterIsCalledT() {
var typeParameter = Box.class.getTypeParameters()[0];

assertThat(typeParameter.getName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@Order(3)
@DisplayName("Type parameter \"T\" is not bounded")
void typeParameterIsNotBounded() {
var typeParameter = Box.class.getTypeParameters()[0];

assertThat(typeParameter.getBounds()).hasSize(1);
assertThat(typeParameter.getBounds()[0].getTypeName()).isEqualTo(Object.class.getTypeName());
}

@Test
@SneakyThrows
@Order(4)
@DisplayName("Field \"value\" is \"T\"")
void valueFieldIsGeneric() {
var valueField = Box.class.getDeclaredField("value");
var genericType = valueField.getGenericType();

assertThat(genericType.getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@SneakyThrows
@Order(5)
@DisplayName("Constructor parameter type is \"T\"")
void constructorParameterIsGeneric() {
var constructor = Box.class.getDeclaredConstructors()[0];
assert (constructor.getParameters().length == 1);
var parameter = constructor.getParameters()[0];

assertThat(parameter.getParameterizedType().getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@SneakyThrows
@Order(6)
@DisplayName("Getter return type is \"T\"")
void getterReturnTypeIsGeneric() {
var getter = Box.class.getDeclaredMethod("getValue");

assertThat(getter.getGenericReturnType().getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}

@Test
@SneakyThrows
@Order(7)
@DisplayName("Setter parameter type is \"T\"")
void setterParameterIsGeneric() {
var setter = Box.class.getDeclaredMethod("setValue", Object.class);
assert (setter.getParameters().length == 1);
var parameter = setter.getParameters()[0];

assertThat(parameter.getParameterizedType().getTypeName()).isEqualTo(TYPE_PARAMETER_NAME);
}
}
Loading