Posts

Showing posts from February, 2020

How to test file operations with Junit5 TempDir

In Junit4 we had TemporaryFolder. In Junit5 the way to test objects that are interacting with the file system it is pretty straightforward with the use of @TempDir.

To make things easier to understand lets assume that we have a class that reads all the files, directories, sub directories and files in sub directories and gathers information about them in a form of a simple dto that contains the extension, if it is directory, the absolute path etc. and adds them in a list.

So given a Path you get a list of objects that contain the file information.

In order to test this functionality we are going to use the TempDir helper from Junit5.
This will create a temporary directory in our filesystem that will be cleaned automatically when our testing is finished.

Check out the example bellow



Check on line 20 how we pass the temporary directory as a test parameter annotated accordingly.

@TempDir will create a directory (depending on your OS) on a tmp folder like /tmp/junit6110933399026626368, but th…

Java Primitive's little secrets

Here is a bunch of java primitives' interesting behaviour that we ofter ignore or forget! Recap time in 3 - 2 - 1...
Data type promotions When you try to perform a numerical operation on a short type for example, the result will be promoted to int, therefore it wont compile if you try to store the result in a short variable unless you explicitly cast it (check line x).


The same applies to byte. Adding two numbers of type byte they will automatically be promoted to short.

What about int? Adding two number of type int together wont result to a long promotion (of course)...

Same as int if you try to to add together two float numbers, the result wont be promoted to double.

All the above are great... but... There is an interesting behaviour when you do the following


the above is perfectly valid, but the bellow results in a compilation error:


Yes indeed!

an easier one now:
Please welcome the infamous Numeric Overflow!



Make your bash script runnable from every directory on the terminal

Have you ever wanted to create a utility like ls for example and make it available from everywhere in the linux's/mac's terminal?

There two ways to do that.
Way 1 (lets assume that my script is in a file with the name hello.)
a.move your script to /bin

sudo mv hello /bin/hello

b.make your script executable

sudo chmod +x /bin/hello

you are ready to ramble!


Way 2
create a file with different functions and aliases like the example bellow. Lets assume that the file name is .myfunc

the file might look like:


Now on your home directory find a file called .bashrc. If you are using a different shell (like zsh) the file might be different. For example if you are using zsh you should look for .zshrc.

Mind that the file names starting with dot (.) in POSIX systems (like linux and macOs) are hidden files.

when you find the file .bashrc, edit it with VI or the editor of your choice and add somewhere (it does not matter where) the line
source {path to your .myfunc file}
If .myfunc is stored …