# Field Injection When Mocking Frameworks Fail

Also published on DZone

# Challenge

You use Dependency Injection (CDI) in your application and you want to unit test your Java classes without making it an integration test by using Weld of Arquillian. You use a Mocking framework like Mockito or EasyMock but still have trouble getting all your dependencies injected into the class because one or the injections is a String type or another final class.

# Problem

1. You have a large iTunes library and want to move it to an external disk.
2. You also have an iPhone that you backup regularly but it takes up to much space.

## Solution 1

You need to consolidate the iTunes library and then move it to an external disk

### Steps

• open iTunes
• File > Library > Organize Library…
• Check the Consolidate files
• OK

This step can take a while… When done do:

• Quit iTunes
• Attach an external drive (in this example called ‘EXTDRIVE’)
• with Finder move the ~/Music/iTunes folder to /Volumes/EXTDRIVE/iTunes

This step can take a while again… When done do:

• If you copied iTunes in stead of moved then rename the ~/Music/iTunes folder to ~/Music/iTunes.bak
• Open iTunes
• iTunes will tell you that it can’t find the library…
• Choose Library by navigating to the EXTDRIVE and opening the iTunes folder
• iTunes will nog see the library again.

## Solution 2

Right now you have the complete library on an external disk but backups will still be made on your own drive (~/Library/Application Support/MobileSync).
If you have an iPhone with 128Gb space this can take up a lot of space.
This is how you can let iTunes make these backups to external disk to.

## Steps

• Quit iTunes if it was running
• open Finder
• press Cmd+Shift+G
• enter: ~/Library/Application Support/MobileSync
• Go
• Now you will see a folder called Backup
• Open a new Finder window (cmd+n)
• Navigate to the EXTDRIVE
• Create Folder called MobileSync on the EXTDRIVE
• now move the Backup folder (other Finder window) into the MobileSync folder

This step can take a while if you have lots of backups… When done do

• open a Terminal window and enter the following command
• ln -s /Volumes/EXTDRIVE/MobileSync/Backup/ ~/Library/Application\ Support/MobileSync/
• this will create a symlink called Backup from the local disk to the external disk
• Start iTumes and see if it worked by pressing cmd+, > Devices
• If You see your former backups there it worked.

# Conclusion

Now the whole iTunes library and backups are made to the EXTDRIVE. This should free up some space, at least if you delete the iTunes.bak folder after verifying the whole change :-)
Note that changing the the name of the drive will invalidate the symlink and it should be created anew with the new name.

# Force An Unlock Of Mysql Tables If Locked

Sometimes you have a locked table when something went wrong and you can not go on without it being unlocked.
here is how to do that.

# PostgreSQL with Glassfish

How to setup JDBC connection to a Postgres datasource in Glassfish

# How to get Keycloak working with Docker

NOTE: This article might be being revised continuously because of new insights.

This blog describes how I created a couple of Docker images to demonstrate Keycloak.
Important in this blog is that the whole process will be described. I attended a couple of keycloak sessions during Javaone this year and during these sessions the illusion was created that adding Keycloak as the security provider for your application is very easy and almost non-invasive for your code.
What they did not tell you that configuring a server that could use keycloak was not as trivial.
This blog will also expose a java web application with rest end-points to show how the auth works.

This blog will tell all :-)

# Journey

• Is there a marriage possible between AngularJS, Java and Maven?
• Can it be done?
• Should it be done?
• Must it be done?

Here I’ll describe the whole process of making a Java Maven AngularJS seed project.
This process will include the mistakes I make and the lessons learned.
The whole goal is to create a setup that can be build by a Java developer, with no specific front-end skills.

## Goals

• Maven as the build tool
• Java EE 7 as the back-end language
• CDI
• AngularJS as the frond-end framework
• Frond-end testing must also be done during the maven build
• Bower as the web package manager.
• Use Git as version control
• Bootstrap als css standard
• Javascript in src/main/javascript
• Minifying javascript
• Jasmine as javascript unit test tool integrated with maven

## Prerequisites

The project is hosted here

## Basic maven project

After generating I got this:

Now we start configuring it.

First create test space and remove unnecessary files:

Open a terminal in the root of the project. (from now on all commands are assumed to be executed from the root of the project)

## Initialize npm

As I want to use npm so I want to initialize it.

adjust the ./package.json file. It needs a bit more tweaking:

I want to use bower as the the web package manager.

The default place bower will install its dependencies is ./bower-components but as I want them to conform to the maven structure I will add a directive to the .bowerrc file we just created.
Information hiding dictates that I won’t tell users that I manage stuff with bower.

This will tell bower to copy the downloaded packages to that place.

This should be enough for a first basic setup. You might want to change the versions.

Because I want to have an easy setup I want to eliminate all these manual steps so now I want to have a config file that helps me to do this automagically :-).
The following command will help create a setup script for bower.

The resulting ./bower.json file should look something like:

Now that I have this config file I should be able to add the dependencies with a command.
But first to test it We need to remove the manually added dependencies :-)

Now to test the try out the setup:

It should recreate it all again :-)

It will also add a folder node_modules to the project.
These folders should be excluded from version control, but that will be done later

I want to use Karma as the testrunner for javascript.
Create the ./src/test/javascript/karma.conf.js containing:

Now we can run the unit tests by using the command npm test

I want to use protractor as the end-2-end tester.
Create the ./src/test/javascript/protractor-conf.js

Now we can run the integration tests by using the command npm run protractor

index.html:

partials/home.html:

js/apps.js:

js/controlers.js:

src/test/javascript/e2e/scenarios.js:

## Test basic setup

For this we need two terminal windows opened in the root of the project

In one of the terminals start the test server

Go here in a browser. You should see “Hello world!”

In the other terminal try out:

This should result in an executed e2e test with no errors.

Try out:

This should result in a working unit test and it should keep on monitoring for test changes.
If you change the test in controllersSpec.js to a wrong text you should see this almost immediately in the terminal window.

I want to be able to use the EE 7 stack.

## CDI

Now we want to have CDI so we need ths file ./src/main/webapp/WEB-INF/beans.xml:

We now have a basic maven project that can be build by the standard maven commands.

## Intermezzo

So now I have a working project that can contain java and angular with all its functionality. The trouble is there is no integration yet, no version control and no maven build that does it all.

I don’t even know if it can be done with currently available tools.

Goals met:

• AngularJS as the frond-end framework
• Bower as the web package manager.

Goals partially met:

• Maven as the build tool, but no javascript tests yet
• Java EE 7 as the back-end language, but no example yet
• CDI, but no example yet
• bootstrap
• Javascript unit testing

Goals to be met:

• Maven as the build tool
• Java EE 7 as the back-end language - with example
• Frond-end testing must also be able done during the maven build
• Use Git as version control
• Maven integration for javascript testing
• Maven for minifying javascript
• Javascript jslint
• Javascript as resource

I use Git for this.

add a .gitignore file to the root of the project containing:

add a .gitignore file to the src/main/webapp of the project containing:

now create the git repository:

In order to get make it possible to create restful services we need a root context for these services. As I want to talk json I also define a json provider.

Now for a Hello world service…

Change the HomeController.js

And the home.html

For this all to work we need a Java EE Container and Node.js just won’t do :-)
I work with Glassfish for my test environment and deployed it.

And now we get:

## Refactoring…

I stared a bit at the code and concluded that I’m not happy with the current state of affairs. Right now I have my javascript code in the src/main/webapp/js folder and it shouldn’t be there. According to maven rulez it should be in src/main/javascript just like java. I also want to have a minified javascript in the artifact and not the origional.

So added the folder and told maven it needed to recognize it as a resource folder.

Add the folling config snippet to the <build> tag in the pom.xml

now I moved all files in the js folder of the webapp to that folder and removed the js folder in webapp.
Now I wanted the minifacation to work and the jslint. I used a maven plugin for that. After some trials and errors the following config seems to work the best.

and a small change to the karma.conf.js. Change src/main/webapp/js/**/*.js to src/main/javascript/**/*.js. now the npm test should work again.

The mvn clean package command failed because my app.js didn’t get through the jslint step and that is just what I wanted. Fixed it and voila.

The code until now is checked in.

## Intermezzo

Goals met:

• AngularJS as the frond-end framework
• Bower as the web package manager.
• Javascript jslint
• Javascript as resource
• Javascript testing with Karma / Jasmine
• Maven for minifying javascript
• Maven as the build tool
• Java EE 7 as the back-end language
• bootstrap
• CDI
• Use Git as version control

Goals to be met:

• Frond-end testing must also be able done during the maven build
• Maven integration for javascript testing

So I guess that my main goal now is to get jasmine working with maven…

At first I removed all jasmine pom stuff to start from scratch. Then I added the most basic jasmine config to it.
But that didn’t work at all. Which was actually logical but I had to do some reading to understand that :-). I had to tell (in the correct order) what the plugin already needs to know before it can test my personal javascript code. At this time this means that I have to tell it to load JQuery and Angular and the Angular mocks. That seemed to do a bit more but now it didn’t find any *Spec.js files and I had to tell the plugin where to find that and the sources.
This seemed to work but I got terrible stacktraces. After some research I found out that it had nothing to do with my configuration but with htmlunit, which is used by default by the plugin. So I had to install phantomjs and configure it as the driver.

Or install it according to the these instructions.

The plugin configuration below finally worked.

As I want to have the integration as complete as possible I want to have the initial bower install also part of maven. Again some research and for me the maven-exec-plugin seems a good solution:

Now during the generate-source phase it will run bower and put all the javascript dependencies into src/main/webapp/vendor because that has been configured in .bowerrc and bower.json. Good stuff :-)

## Clean

As the vendor folder in webapp is not part of our version control and cleanup is also part of the fun I added the maven clean plugin with a bit of configuration.

## Removed protractor

As I tend to use Fitnesse and Selenium (in comination) to do my end-to-end testing I decided to remove protractor from the project. I want to test against the real server with all stuff working before running e2e tests.

I had to refactor some stuff like the path to the javascript unit tests as I moved the other scripts to src/test/javascript and removing references to protractor.

## Conclusion (for now :-))

I committed the whole seed project to github and except for the end to end tests it all seems to work. I don’t mind about that because I tend to use Fitnesse for these kinds of tests so I can test against the real server.

It is a learning process and to say that I’m completely happy with the configuration is to lie.

In this version of the seed project it was all about integration. Integrating the different worlds of front-end and back-end with all it’s tools.

The question remains if that should be the way to go? I don’t know yet. Decoupling these worlds might be better.

I tend to think that these worlds should only meet through services and the front-end and back-end should be build separately. But as I was writing this a colleague of mine told me that he really wanted a setup like this because he wanted the java developers to be able to build the whole project with a simple command like mvn clean install, because it cuts down on the learning curve.

Final conclusion: Who am I to judge?!

# Extra’s

## IDE Configuration

I assume IntelliJ because lets be real… There can be only one :-)
I also assume that glassfish is already installed.

The first time you check out the project you should run mvn package or mvn exec:exec to get the vendor resources installed.

Now you can deploy to glassfish through the IDE but you will still not have the javascript resources copied to the right place. In order to get this to work you can tell IntelliJ to perform a step before every make (see the picture below)

Choose on yuicompressor:compress the right mouse option: Execute Before Make.

Now whenever you press cmd+F9 or ctrl+r the compressor plugin will do its thing and update the resources.

Cheerz,

Ivo.

# Open Questions

• Is Yeoman something to look at?
• What about grunt? is it better?
• Or Gulp?
• Should I make the back-end and frond-end separate projects so they can be developed separately?
• JHipster?
• run with jetty
• jslint as part of the config again
• webjars

# Java Maven Angular Seed Project

• Is there a marriage possible between AngularJS, Java and Maven?
• Can it be done?
• Should it be done?
• Must it be done?

Here I’ll describe the whole process of making a Java Maven AngularJS seed project.
This process will include the mistakes I make and the lessons learned.
The whole goal is to create a setup that can be build by a Java developer, with no specific front-end skills.

# How To Get Samsung Led Tv Pvr Recording As Mkv

## Challenge

You have recording(s) made with your Samsung LED TV on a harddisk and you want to keep them as Matroska (mkv) files.

# Challenge

You use IntelliJ 14 (EAP) and Java 8 and now you can’t seem to get the IDE to work.