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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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,8 +24,7 @@ public class ExerciseIntroduction {
* @return "The key to efficient learning is practice!"
*/
public String getWelcomeMessage() {
// todo: implement a method and return a message according to javadoc
throw new ExerciseNotCompletedException();
return "The key to efficient learning is practice!";
}

/**
Expand All @@ -39,7 +39,6 @@ public String getWelcomeMessage() {
* @return encoded message
*/
public String encodeMessage(String message) {
// todo: switch to branch "completed" in order to see how it should be implemented
throw new ExerciseNotCompletedException();
return Base64.getEncoder().encodeToString(message.getBytes());
}
}
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,18 +7,18 @@
* <p>
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it
*/
public class Box {
private Object value;
public class Box<T> {
private T value;

public Box(Object value) {
public Box(T value) {
this.value = value;
}

public Object getValue() {
public T getValue() {
return value;
}

public void setValue(Object value) {
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 Down
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);
}
}