Oracle UTF8 encoding check

You can pretty easily see the raw bytes used for encoding a string:

This can be useful for comparing two strings — as one knows just because two strings are rendered in the same way, it does not mean that the UNICODE characters they are made of are the same, the actual UNICODE graphemes can be different.

Now that we have the raw representation of the string we can convert it back to UTF8. This can also serve as confirmation that the string was correctly encoded and that the encoding was UTF8 to start with.

A more automated way to check that the strings are correctly encoded is below (thanks StackOverflow). It relies on the fact that incorrect encoding to UTF8 results in the following characters (hex): “EFBFBD”.

 

DIY: ventilate room for cheap

=== Warning: electricity is dangerous!!! Make sure you know what you’re doing. The author cannot be in any way held responsible for anything that may happen if you follow the below instructions ===

I have a room in my basement that I feel doesn’t get enough air. Obviously, the best would be to have a window, but since this is the basement it wouldn’t be practical.

Having a ventilation removes dampness and ensures air is always renewed in the room.

So I decided to make one for cheap. Here’s the bill of materials:

  • a PC cooler. I spent maybe 10 euros on it. It’s cheap and it doesn’t have PWM (there’s just a 3 wire cable, 2 for powering the fan and the third is the “tach” which caries back the speed of the fan. I don’t use the last one). I don’t remember exactly which of the three wires carry power, you need to experience by yourself;
  • a 12V power source. This comes from LIDL, costs around 5 euros and the voltage is also configurable between 3/6/9/12V which is awesome;
  • a hinge. This is optional, but since my fan is installed inside the room without this I wouldn’t be able to close the door anymore. With this, the fan rotates upwards and I can close the door shut;
  • two pairs of jack male female connectors so that connecting the electrical wire to the power source and then to the fan is properly done; you can see two of them in the image below
  • some electrical wire.

It’s pretty obvious to make. First of all, cut the hinge so that it matches your fan size. Most hinge holes will probably not match the fan holes, so you may want to drill some new in the hinge. Attach the hinge to the fan and then, the other side of the hinge, nail it to the wall. Plugin the fan to the power source and hopefully things will start moving ;).

And the final result buzzing happily.

TimerManager is in STOPPED state

Did you ever encounter the exception below?

I found that just removing the caches from your weblogic solves the problem. So go to $SERVER_ROOT/servers/AdminServer/cache and remove all the files in that directory.

Inject data to nested serializers in Jackson

We sometimes need to tunnel information to a nested serializer and do it from the top level.

Let’s take the following example of some Shape objects which are nested in a Drawing object.

We will be serializing the drawing object using Jackson, so let’s define the appropriate serializers.

Now, let’s say that from the Drawing serializer we would like to inject some information into the Shape serializer. But, as you see Jackson never gives us an instance of the Shape serializer so it won’t be possible to change its members.

So how do we do this? Here come the attributes. These ones are set on the SerializerProvider object and can be later retrieved from a more nested serializer.

As example, let’s say we want to inject an attribute, that, when true, will make the Shape serializer output the name as upper case.

We inject the attribute in the Drawing serializer.

Now from the nested serializer we can retrieve the value of the attribute which was injected at top level.

 

Activate logging in Hibernate

Logging is extremely well done in hibernate and can be quite useful in case of problems.

Hibernate uses slf4j to perform logging.  Slf4j is just a facade for logging and uses an underlying system to actually perform logging. One can use whatever logging system wishes, it basically depends on personal preferences and whatever you’re more familiar configuring.

For this example I choose log4j 1.2, but log4j 2, logback etc are also possible.

In order to enable logging through log4j1.2 you should have the following dependency in your pom.xml:

Above I use version 1.7.25 which is the latest at this time, but older versions should work as well.

Log4j is configured through a log4j.properties file that you should put in your resources folder (that can be under either src/main or test/main depending if you wish to use it for production logging or only in a test setting).

Here’s a sample of a log4j.properties that activates sets the rootLogger to INFO level only, but activates the DEBUG level for hibernate. Warning! it’s not exactly the TRACE level, but it can still yield a lot of output.

The correct JPA provider for hibernate

While working with hibernate you will sometimes have an error message like the following:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named <YOUR PERSISTENT UNIT NAME>

One of the possible causes for this is that the provider for hibernate in your persistence.xml is wrong. In various places on the web it is suggested that it should be: org.hibernate.ejb.HibernatePersistence

Putting the one above results in the obscure exception above.

To fix this enter the correct provider for hibernate: org.hibernate.jpa.HibernatePersistenceProvider

SecureRandom gotcha

If you need a random number you are usually advised to use SecureRandom because it gives a cryptographically secure random number.

Typical way of calling SecureRandom would be:

From a random generator seeding perspective this is also the correct way. You might be tempted to call the setSeed in order to seed with some long that you provide. Resist that temptation unless you can prove that the seed obeys the constraints of your application (like that it is different between different instantiations and invocations of SecureRandom). And no, seeding with the System.currentTimeMillis() will not work: multiple threads can get the same value.

Here’s an excerpt from the setSeed documentation:

The interesting part here is that it says that the seed “supplements” the internal seed. This is not what I found. The given seed will completely replace the internal seed. This means that when seeded the same way two SecureRandom will return the same value. Probably not what you would expect.

 

Emacs is awesome!

Here’s an elisp function that will just underline your current text. It’s simple, awesome and I use it all the time for my titles. You can put it in the *scratch* and do a C-x C-e and then call it with M-x add-underlining.

 

JUnit Argument Captor — enhance your mock testing

When writing mock tests it is, sometimes, more convenient to assert the values with which the methods were called using the ArgumentCaptor
class instead of the more usual mock verify.
Let’s see both in action.

We’ll take as sample a mock of the List interface.

Let’s see how verify-ing would work:

Or since we expect those to be called in that same exact order we could also:

Now let’s use ArgumentCaptor instead:

That seems more streamlined and readable IMO. As you can see the ArgumentCaptor will catch all arguments with which it was called.

If you expect a single value to be captured or if you’re only interested in the last value with which your method was called you can also call:

In our case that call will return “three”.

JUnit Categories

Hey there, this is my second post on JUnit and its lesser known features. To read the first one go here.

JUnit Categories allow tagging tests thus dividing the tests into separate buckets. This, in turn, becomes important in order to choose which tests run at a later time.

I find categories in combination with maven quite useful in splitting tests into unitary and integration tests. This is probably better understood with an example.

 

Following maven config will split the tests into unitary tests and integration tests. Note how we’re using surefire for the unitary tests and failsafe for the integration tests. We could have used surefire all the way and by all means do if you only have unitary tests. However, failsafe makes sure that failing tests are torn down correctly and that the post-integration-test phase (that comes after the integration-test) is called. The surefire plugin would just stop everything during the integration-test phase. But, anyways, this is maybe an article for another time. Here’s the example:

 

In order to skip the integration tests you can add -DskipITs command line parameter to the maven invocation.