diff --git a/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java b/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java index 35d92563..4e6fa2a2 100644 --- a/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java +++ b/0-0-intro/src/main/java/com/bobocode/intro/ExerciseIntroduction.java @@ -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. @@ -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!"; } /** @@ -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()); } } diff --git a/0-0-intro/src/test/java/com/bobocode/intro/ExerciseIntroductionTest.java b/0-0-intro/src/test/java/com/bobocode/intro/ExerciseIntroductionTest.java index 093909fe..f8a37c24 100644 --- a/0-0-intro/src/test/java/com/bobocode/intro/ExerciseIntroductionTest.java +++ b/0-0-intro/src/test/java/com/bobocode/intro/ExerciseIntroductionTest.java @@ -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. *

* A typical Java test uses JUnit framework to run the test, and may also use some other frameworks for assertions. @@ -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"); + } +} \ No newline at end of file diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java index 5a2d860e..180603a2 100644 --- a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java +++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java @@ -7,18 +7,18 @@ *

* 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 { + 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; } } diff --git a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/BoxDemoApp.java b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/BoxDemoApp.java index bc12174e..fcc9b7fc 100644 --- a/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/BoxDemoApp.java +++ b/1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/BoxDemoApp.java @@ -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 diff --git a/1-0-java-basics/1-3-0-hello-generics/src/test/java/com/bobocode/basics/BoxTest.java b/1-0-java-basics/1-3-0-hello-generics/src/test/java/com/bobocode/basics/BoxTest.java index 76e0f385..1d033665 100644 --- a/1-0-java-basics/1-3-0-hello-generics/src/test/java/com/bobocode/basics/BoxTest.java +++ b/1-0-java-basics/1-3-0-hello-generics/src/test/java/com/bobocode/basics/BoxTest.java @@ -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); + } +} \ No newline at end of file