Aggiunti alcuni esercizi di java.

This commit is contained in:
2023-03-08 19:00:28 +01:00
parent 5bdf6fbb5f
commit c038195ed5
584 changed files with 22842 additions and 0 deletions

View File

@ -0,0 +1,23 @@
{
"authors": [
"mirkoperillo"
],
"files": {
"solution": [
"src/main/java/Lasagna.java"
],
"test": [
"src/test/java/LasagnaTest.java"
],
"exemplar": [
".meta/src/reference/java/Lasagna.java"
],
"invalidator": [
"build.gradle"
]
},
"forked_from": [
"csharp/lucians-luscious-lasagna"
],
"blurb": "Learn about the basics of Java by following a lasagna recipe."
}

View File

@ -0,0 +1 @@
{"track":"java","exercise":"lasagna","id":"08eb6ff4a5c042a1aed228551bb1c301","url":"https://exercism.org/tracks/java/exercises/lasagna","handle":"GicoProgram","is_requester":true,"auto_approve":false}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

View File

@ -0,0 +1,2 @@
#Tue Mar 07 17:27:00 CET 2023
gradle.version=7.5.1

Binary file not shown.

Binary file not shown.

View File

3
lasagna/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
lasagna/.idea/compiler.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" />
</component>
</project>

16
lasagna/.idea/gradle.xml generated Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

20
lasagna/.idea/jarRepositories.xml generated Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>

5
lasagna/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK" />
</project>

131
lasagna/HELP.md Normal file
View File

@ -0,0 +1,131 @@
# Help
## Running the tests
Choose your operating system:
* [Windows](#windows)
* [macOS](#macos)
* [Linux](#linux)
----
1. Open a Command Prompt.
2. Get the first exercise:
```batchfile
C:\Users\JohnDoe>exercism download --exercise hello-world --track java
Not Submitted: 1 problem
java (Hello World) C:\Users\JohnDoe\exercism\java\hello-world
New: 1 problem
java (Hello World) C:\Users\JohnDoe\exercism\java\hello-world
unchanged: 0, updated: 0, new: 1
```
3. Change directory into the exercism:
```batchfile
C:\Users\JohnDoe>cd C:\Users\JohnDoe\exercism\java\hello-world
```
4. Run the tests:
```batchfile
C:\Users\JohnDoe>gradle test
```
*(Don't worry about the tests failing, at first, this is how you begin each exercise.)*
5. Solve the exercise. Find and work through the `instructions.append.md` guide ([view on GitHub](https://github.com/exercism/java/blob/main/exercises/practice/hello-world/.docs/instructions.append.md#tutorial)).
Good luck! Have fun!
----
1. In the terminal window, get the first exercise:
```
$ exercism download --exercise hello-world --track java
New: 1 problem
Java (Etl) /Users/johndoe/exercism/java/hello-world
unchanged: 0, updated: 0, new: 1
```
2. Change directory into the exercise:
```
$ cd /Users/johndoe/exercism/java/hello-world
```
3. Run the tests:
```
$ gradle test
```
*(Don't worry about the tests failing, at first, this is how you begin each exercise.)*
4. Solve the exercise. Find and work through the `instructions.append.md` guide ([view on GitHub](https://github.com/exercism/java/blob/main/exercises/practice/hello-world/.docs/instructions.append.md#tutorial)).
Good luck! Have fun!
----
1. In the terminal window, get the first exercise:
```
$ exercism download --exercise hello-world --track java
New: 1 problem
Java (Etl) /home/johndoe/exercism/java/hello-world
unchanged: 0, updated: 0, new: 1
```
2. Change directory into the exercise:
```
$ cd /home/johndoe/exercism/java/hello-world
```
3. Run the tests:
```
$ gradle test
```
*(Don't worry about the tests failing, at first, this is how you begin each exercise.)*
4. Solve the exercise. Find and work through the `instructions.append.md` guide ([view on GitHub](https://github.com/exercism/java/blob/main/exercises/practice/hello-world/.docs/instructions.append.md#tutorial)).
Good luck! Have fun!
## Submitting your solution
You can submit your solution using the `exercism submit src/main/java/Lasagna.java` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Java track's documentation](https://exercism.org/docs/tracks/java)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
If you need some help you can visit these resources:
* [Stack Overflow](https://stackoverflow.com/questions/tagged/java),
* [The Java subreddit](https://www.reddit.com/r/java),
* [Official Java documentation](https://docs.oracle.com/en/java/javase/11/docs/api/index.html).

38
lasagna/HINTS.md Normal file
View File

@ -0,0 +1,38 @@
# Hints
## General
- An [integer value][integers] can be defined as one or more consecutive digits.
## 1. Define the expected oven time in minutes
- You need to define a [method][methods] without any arguments.
- You need to return an [integer][integers].
## 2. Calculate the remaining oven time in minutes
- You need to define a [method][methods] with a single parameter.
- You have to [explicitly return an integer][return] from a method.
- The method's parameter is an [integer][integers].
- You can use the [mathematical operator for subtraction][operators] to subtract values.
## 3. Calculate the preparation time in minutes
- You need to define a [method][methods] with a single parameter.
- You have to [explicitly return an integer][return] from a method.
- The method's parameter is an [integer][integers].
- You can use the [mathematical operator for multiplicaton][operators] to multiply values.
## 4. Calculate the total working time in minutes
- You need to define a [method][methods] with two parameters.
- You have to [explicitly return an integer][return] from a method.
- The method's parameter is an [integer][integers].
- You can [invoke][invocation] one of the other methods you've defined previously.
- You can use the [mathematical operator for addition][operators] to add values.
[methods]: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
[return]: https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
[operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
[integers]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
[invocation]: https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html

105
lasagna/README.md Normal file
View File

@ -0,0 +1,105 @@
# Cook your lasagna
Welcome to Cook your lasagna on Exercism's Java Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
Java is a statically-typed language, which means that everything has a type at compile-time. Assigning a value to a name is referred to as defining a variable. A variable is defined by explicitly specifying its type.
```java
int explicitVar = 10;
```
Updating a variable's value is done through the `=` operator. Once defined, a variable's type can never change.
```java
int count = 1; // Assign initial value
count = 2; // Update to new value
// Compiler error when assigning different type
// count = false;
```
Java is an [object-oriented language][object-oriented-programming] and requires all functions to be defined in a _class_. The `class` keyword is used to define a class.
```java
class Calculator {
// ...
}
```
A function within a class is referred to as a _method_. Each method can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. Similarly, the return type must also be made explicit. Values are returned from functions using the `return` keyword. To allow a method to be called by other classes, the `public` access modifier must be added.
```java
class Calculator {
public int add(int x, int y) {
return x + y;
}
}
```
Invoking a method is done by specifying its class and method name and passing arguments for each of the method's parameters.
```java
int sum = new Calculator().add(1, 2);
```
Scope in Java is defined between the `{` and `}` characters.
Java supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`.
[object-oriented-programming]: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
## Instructions
In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
You have four tasks, all related to the time spent cooking the lasagna.
## 1. Define the expected oven time in minutes
Define the `expectedMinutesInOven()` method that does not take any parameters and returns how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:
```java
Lasagna lasagna = new Lasagna();
lasagna.expectedMinutesInOven();
// => 40
```
## 2. Calculate the remaining oven time in minutes
Define the `remainingMinutesInOven()` method that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.
```java
Lasagna lasagna = new Lasagna();
lasagna.remainingMinutesInOven(30);
// => 10
```
## 3. Calculate the preparation time in minutes
Define the `preparationTimeInMinutes()` method that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
```java
Lasagna lasagna = new Lasagna();
lasagna.preparationTimeInMinutes(2);
// => 4
```
## 4. Calculate the total working time in minutes
Define the `totalTimeInMinutes()` method that takes two parameters: the first parameter is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven. The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
```java
Lasagna lasagna = new Lasagna();
lasagna.totalTimeInMinutes(3, 20);
// => 26
```
## Source
### Created by
- @mirkoperillo

24
lasagna/build.gradle Normal file
View File

@ -0,0 +1,24 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"
// set default encoding to UTF-8
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"
repositories {
mavenCentral()
}
dependencies {
testImplementation "junit:junit:4.13"
testImplementation "org.assertj:assertj-core:3.15.0"
}
test {
testLogging {
exceptionFormat = 'full'
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}

Binary file not shown.

View File

@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

240
lasagna/gradlew vendored Executable file
View File

@ -0,0 +1,240 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

91
lasagna/gradlew.bat vendored Normal file
View File

@ -0,0 +1,91 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@ -0,0 +1,27 @@
public class Lasagna {
private static final int TIME = 40;
private static final int LAYER_TIME = 2;
// TODO: define the 'expectedMinutesInOven()' method
public int expectedMinutesInOven() {
return TIME;
}
// TODO: define the 'remainingMinutesInOven()' method
public int remainingMinutesInOven(int timePast) {
return Math.max(0, TIME-timePast);
}
// TODO: define the 'preparationTimeInMinutes()' method
public int preparationTimeInMinutes(int layers) {
return Math.max(0, layers*LAYER_TIME);
}
// TODO: define the 'totalTimeInMinutes()' method
public int totalTimeInMinutes(int layers, int timeOven) {
return preparationTimeInMinutes(layers) + timeOven;
}
}

View File

@ -0,0 +1,90 @@
import org.junit.Test;
import utils.Lasagna;
import static org.assertj.core.api.Assertions.*;
public class LasagnaTest {
@Test
public void implemented_expected_minutes_in_oven() {
assertThat(new Lasagna().hasMethod("expectedMinutesInOven"))
.withFailMessage("Method expectedMinutesInOven must be created")
.isTrue();
assertThat(new Lasagna().isMethodPublic("expectedMinutesInOven"))
.withFailMessage("Method expectedMinutesInOven must be public")
.isTrue();
assertThat(new Lasagna().isMethodReturnType(int.class, "expectedMinutesInOven"))
.withFailMessage("Method expectedMinutesInOven must return an int")
.isTrue();
}
@Test
public void expected_minutes_in_oven() {
assertThat(new Lasagna().expectedMinutesInOven()).isEqualTo(40);
}
@Test
public void implemented_remaining_minutes_in_oven() {
assertThat(new Lasagna().hasMethod("remainingMinutesInOven", int.class))
.withFailMessage("Method remainingMinutesInOven must be created")
.isTrue();
assertThat(new Lasagna().isMethodPublic("remainingMinutesInOven", int.class))
.withFailMessage("Method remainingMinutesInOven must be public")
.isTrue();
assertThat(new Lasagna().isMethodReturnType(int.class, "remainingMinutesInOven", int.class))
.withFailMessage("Method remainingMinutesInOven must return an int")
.isTrue();
}
@Test
public void remaining_minutes_in_oven() {
assertThat(new Lasagna().remainingMinutesInOven(25)).isEqualTo(15);
}
@Test
public void implemented_preparation_time_in_minutes() {
assertThat(new Lasagna().hasMethod("preparationTimeInMinutes", int.class))
.withFailMessage("Method preparationTimeInMinutes must be created")
.isTrue();
assertThat(new Lasagna().isMethodPublic("preparationTimeInMinutes", int.class))
.withFailMessage("Method preparationTimeInMinutes must be public")
.isTrue();
assertThat(new Lasagna().isMethodReturnType(int.class, "preparationTimeInMinutes", int.class))
.withFailMessage("Method preparationTimeInMinutes must return an int")
.isTrue();
}
@Test
public void preparation_time_in_minutes_for_one_layer() {
assertThat(new Lasagna().preparationTimeInMinutes(1)).isEqualTo(2);
}
@Test
public void preparation_time_in_minutes_for_multiple_layers() {
assertThat(new Lasagna().preparationTimeInMinutes(4)).isEqualTo(8);
}
@Test
public void implemented_total_time_in_minutes() {
assertThat(new Lasagna().hasMethod("totalTimeInMinutes", int.class, int.class))
.withFailMessage("Method totalTimeInMinutes must be created")
.isTrue();
assertThat(new Lasagna().isMethodPublic("totalTimeInMinutes", int.class, int.class))
.withFailMessage("Method totalTimeInMinutes must be public")
.isTrue();
assertThat(new Lasagna().isMethodReturnType(int.class, "totalTimeInMinutes", int.class, int.class))
.withFailMessage("Method totalTimeInMinutes must return an int")
.isTrue();
}
@Test
public void total_time_in_minutes_for_one_layer() {
assertThat(new Lasagna().totalTimeInMinutes(1, 30)).isEqualTo(32);
}
@Test
public void total_time_in_minutes_for_multiple_layers() {
assertThat(new Lasagna().totalTimeInMinutes(4, 8)).isEqualTo(16);
}
}

View File

@ -0,0 +1,41 @@
package utils;
public class Lasagna extends ReflectionProxy {
@Override
public String getTargetClassName() {
return "Lasagna";
}
public int expectedMinutesInOven() {
try {
return invokeMethod("expectedMinutesInOven", new Class[]{});
} catch (Exception e) {
throw new UnsupportedOperationException("Please implement the expectedMinutesInOven() method");
}
}
public int remainingMinutesInOven(int actualMinutes) {
try {
return invokeMethod("remainingMinutesInOven", new Class[]{int.class}, actualMinutes);
} catch (Exception e) {
throw new UnsupportedOperationException("Please implement the remainingMinutesInOven(int) method");
}
}
public int preparationTimeInMinutes(int amountLayers) {
try {
return invokeMethod("preparationTimeInMinutes", new Class[]{int.class}, amountLayers);
} catch (Exception e) {
throw new UnsupportedOperationException("Please implement the preparationTimeInMinutes(int) method");
}
}
public int totalTimeInMinutes(int amountLayers, int actualMinutes) {
try {
return invokeMethod("totalTimeInMinutes", new Class[]{int.class, int.class}, amountLayers, actualMinutes);
} catch (Exception e) {
throw new UnsupportedOperationException("Please implement the totalTimeInMinutes(int, int) method");
}
}
}

View File

@ -0,0 +1,487 @@
package utils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import static java.lang.Class.forName;
public abstract class ReflectionProxy {
/**
* An instance of the target class (if found)
*/
private final Object target;
/**
* A constructor to instantiate the target class with parameters
* @param args An array of parameters matching the constructor from the target class
*/
protected ReflectionProxy(Object... args) {
this.target = instantiateTarget(args);
}
/**
* Abstract method that defines the fully qualified name of the target class
* @return The fully qualified name of the target class
*/
public abstract String getTargetClassName();
/**
* Getter for the target instance
* @return The target instance
*/
public Object getTarget() {
return target;
}
/**
* Gets the target class
* @return The target class if it exists, null otherwise
*/
public Class<?> getTargetClass() {
try {
return forName(this.getTargetClassName());
} catch (ClassNotFoundException e) {
return null;
}
}
/**
* Checks if the target class has a specific method
* @param name The name of the method to find
* @param parameterTypes The list of parameter types
* @return True if the method is found, false otherwise
*/
public boolean hasMethod(String name, Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Method m = targetClass.getDeclaredMethod(name, parameterTypes);
return m != null;
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Checks if a method from the target class is public
* @param name The name of the method
* @param parameterTypes A list of method parameters
* @return True if the method exists and is public, false otherwise
*/
public boolean isMethodPublic(String name, Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Method m = targetClass.getDeclaredMethod(name, parameterTypes);
return Modifier.isPublic(m.getModifiers());
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Checks if a method from the target class returns the correct type
* @param returnType The type of return value
* @param name The name of the method
* @param parameterTypes The list of method parameters
* @return
*/
public boolean isMethodReturnType(Class<?> returnType, String name, Class<?>... parameterTypes) {
return isMethodReturnType(returnType, null, name, parameterTypes);
}
/**
* Invokes a method from the target instance
* @param methodName The name of the method
* @param parameterTypes The list of parameter types
* @param parameterValues The list with values for the method parameters
* @param <T> The result type we expect the method to be
* @return The value returned by the method
*/
protected <T> T invokeMethod(String methodName, Class<?>[] parameterTypes, Object... parameterValues) {
if (target == null) {
return null;
}
try {
// getDeclaredMethod is used to get protected/private methods
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
} catch (NoSuchMethodException e) {
try {
// try getting it from parent class, but only public methods will work
Method method = target.getClass().getMethod(methodName, parameterTypes);
method.setAccessible(true);
return (T) method.invoke(target, parameterValues);
} catch (Exception ex) {
return null;
}
} catch (Exception e) {
return null;
}
}
/**
* Creates an instance of the target class
* @param args The list of constructor parameters
* @return An instance of the target class, if found, or null otherwise
*/
private Object instantiateTarget(Object... args) {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return null;
}
Constructor<?>[] constructors = getAllConstructors();
for (Constructor<?> c : constructors) {
if (c.getParameterCount() == args.length) {
try {
return c.newInstance(args);
} catch (Exception e) {
// do nothing;
}
}
}
return null;
}
/**
* Gets a list with all the constructors defined by the target class
* @return A list with all constructor definitions
*/
private Constructor<?>[] getAllConstructors() {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return new Constructor<?>[]{};
}
return targetClass.getConstructors();
}
//region Unused
/**
* The default constructor, for when you have already an instance of the target class
* @param target An instance of the target class
*/
protected ReflectionProxy(Object target) {
this.target = target;
}
/**
* Checks if the target class exists
* @return True if the class exists, false otherwise
*/
public boolean existsClass() {
return getTargetClass() != null;
}
/**
* Checks if the class implements a specific interface
* @param anInterface The interface to check
* @return True if the class implements the referred interface, false otherwise
*/
public boolean implementsInterface(Class<?> anInterface) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || anInterface == null) {
return false;
}
return anInterface.isAssignableFrom(targetClass);
}
/**
* Checks if the target class has a specific property
* @param name The name of the property to find
* @return True if the property is found, false otherwise
*/
public boolean hasProperty(String name) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Field f = targetClass.getDeclaredField(name);
return f != null;
} catch (NoSuchFieldException e) {
return false;
}
}
/**
* Checks if an existing property has the type we expect
* @param name The name of the property to check
* @param type The type you are expecting the property to be
* @return True if the property is found and has the specified type, false otherwise
*/
public boolean isPropertyOfType(String name, Class<?> type) {
return isPropertyOfType(name, type, null);
}
/**
* Checks if an existing Collection type has the parameterized type (Generics) as expected (eg. List<String>)
* @param name The name of the property
* @param type The type of the property (eg. List)
* @param parameterizedType The parameterized property (eg. String)
* @return True if the parameterized type matches the desired type, false otherwise
*/
public boolean isPropertyOfType(String name, Class<?> type, Class<?> parameterizedType) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null || type == null) {
return false;
}
try {
Field f = targetClass.getDeclaredField(name);
if (!f.getType().equals(type)) {
return false;
}
if (parameterizedType == null) {
return true;
}
if (!(f.getGenericType() instanceof ParameterizedType)) {
return false;
}
ParameterizedType pType = (ParameterizedType) f.getGenericType();
return pType.getActualTypeArguments()[0].equals(parameterizedType);
} catch (NoSuchFieldException e) {
return false;
}
}
/**
* Checks if a property is private
* @param name The name of the property
* @return True if the property exists and is private, false otherwise
*/
public boolean isPropertyPrivate(String name) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Field f = targetClass.getDeclaredField(name);
return Modifier.isPrivate(f.getModifiers());
} catch (NoSuchFieldException e) {
return false;
}
}
/**
* Checks if a method from the target class returns a correct parameterized collection (Generics)
* @param returnType The return type we expect (eg. List)
* @param parameterizedType The parameterized type we expect (eg. String)
* @param name The name of the method
* @param parameterTypes A list of method parameter types
* @return True if the method returns the correct parameterized collection, false otherwise
*/
public boolean isMethodReturnType(Class<?> returnType, Class<?> parameterizedType,
String name, Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Method m = targetClass.getDeclaredMethod(name, parameterTypes);
if (!m.getReturnType().equals(returnType)) {
return false;
}
if (parameterizedType == null) {
return true;
}
if (!(m.getGenericReturnType() instanceof ParameterizedType)) {
return false;
}
ParameterizedType pType = (ParameterizedType) m.getGenericReturnType();
return pType.getActualTypeArguments()[0].equals(parameterizedType);
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Checks if a target class has a specific constructor
* @param parameterTypes The list of desired parameter types
* @return True if the constructor exists, false otherwise
*/
public boolean hasConstructor(Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return false;
}
try {
Constructor<?> c = targetClass.getDeclaredConstructor(parameterTypes);
return c != null;
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Checks if a specific constructor from the target class is public
* @param parameterTypes A list of parameter types
* @return True if the constructor is found and is public, false otherwise
*/
public boolean isConstructorPublic(Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return false;
}
try {
Constructor<?> c = targetClass.getDeclaredConstructor(parameterTypes);
return Modifier.isPublic(c.getModifiers());
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Proxy for the 'equals' method
* @param obj The ReflexionProxy object you want to compare against
* @return True if both targets are equal, false otherwise
*/
public boolean equals(Object obj) {
if (target == null || !(obj instanceof ReflectionProxy)) {
return false;
}
try {
Method method = target.getClass().getMethod("equals", Object.class);
method.setAccessible(true);
return (boolean) method.invoke(target, ((ReflectionProxy) obj).getTarget());
} catch (Exception e) {
return false;
}
}
/**
* Proxy for the 'hashCode' method
* @return The hashCode from the target class
*/
public int hashCode() {
if (target == null) {
return 0;
}
try {
Method method = target.getClass().getMethod("hashCode");
method.setAccessible(true);
return (int) method.invoke(target);
} catch (Exception e) {
return 0;
}
}
/**
* Proxy for the 'toString' method from the target class
* @return The result of 'toString' from the target instance
*/
public String toString() {
return invokeMethod("toString", new Class[]{ });
}
/**
* Gets a property value from the target instance (if it exists)
* @param propertyName The name of the property
* @param <T> The type we are expecting it to be
* @return The value of the property (if it exists)
*/
protected <T> T getPropertyValue(String propertyName) {
if (target == null || !hasProperty(propertyName)) {
return null;
}
try {
Field field = target.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return (T) field.get(target);
} catch (Exception e) {
return null;
}
}
/**
* Checks if the target class is abstract
* @return True if the target class exists and is abstract, false otherwise
*/
public boolean isAbstract() {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return false;
}
return Modifier.isAbstract(targetClass.getModifiers());
}
/**
* Checks if the target class extends another
* @param className The fully qualified name of the class it should extend
* @return True if the target class extends the specified one, false otherwise
*/
public boolean extendsClass(String className) {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return false;
}
try {
Class<?> parentClass = Class.forName(className);
return parentClass.isAssignableFrom(targetClass);
} catch (ClassNotFoundException e) {
return false;
}
}
/**
* Checks if the target class is an interface
* @return True if the target class exists and is an interface, false otherwise
*/
public boolean isInterface() {
Class<?> targetClass = getTargetClass();
if (targetClass == null) {
return false;
}
return targetClass.isInterface();
}
/**
* Checks if a method is abstract
* @param name The name of the method
* @param parameterTypes The list of method parameter types
* @return True if the method exists and is abstract, false otherwise
*/
public boolean isMethodAbstract(String name, Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Method m = targetClass.getDeclaredMethod(name, parameterTypes);
return Modifier.isAbstract(m.getModifiers());
} catch (NoSuchMethodException e) {
return false;
}
}
/**
* Checks if a method is protected
* @param name The name of the method
* @param parameterTypes The list of method parameter types
* @return True if the method exists and is protected, false otherwise
*/
public boolean isMethodProtected(String name, Class<?>... parameterTypes) {
Class<?> targetClass = getTargetClass();
if (targetClass == null || name == null) {
return false;
}
try {
Method m = targetClass.getDeclaredMethod(name, parameterTypes);
return Modifier.isProtected(m.getModifiers());
} catch (NoSuchMethodException e) {
return false;
}
}
//endregion
}