Saturday, February 9, 2013

Assertions: Not so Used but yet so Powerful

Before I do something productive on a Saturday morning, might as well quickly blog about a not-so-commonly used feature in Java (well I practically don't see a lot of people using it that's all)... but they should!

Searching the net will give you tons of result about how to use Assertion so I'm not going to go through that, what I will demonstrate is the use of Assertion in JDeveloper.

I'm currently using JDeveloper 11.1.2.x but it should also work in any version of JDeveloper.

So quickly, let me show you a project specific configuration called Run Configuration. If you're coming from an Eclipse background, this should be one of your "must-know" setup but in JDeveloper, it's a not required setup when running your normal BC App or ADF View App.

Choose and Configure Run Configuraitons
What you'll need to know thought is that this configuration is project specific, in fact, by setting the Assertion command argument in a normal java project - and running it of course - you'll right away see assertions kicking in.

Assertion Argument
The part that kind'a sucks is turning it on in a running ADF View App... ok well it doesn't really suck but it wasn't straightforward to figure out.

Basically, when you're starting up your Weblogic Server (Integrate), part of the domain configuration startup includes a "-da" command which basically says that by default, all assertions are off.

So what does this mean, it means that even if you put "-ea" while booting up, the "-da" will cancel it out.

But this comes with a lot of pros because you don't really want everything to be asserted at that point. So just make sure that when you're asserting in a ViewController - do make sure to point the exact package structure to assert.

You should see something like this in your console on startup, in my case... I'm enabling assertion in all of my model classes (defined by: -ea:model...). Notice how I'm also highlighting the -da argument, you can see this in your setDomainEnv.cmd file - I don't recommend removing it though.


Starting WLS with line:
d:\Oracle\MW\11123~1.0\JDK160~1\bin\java -client   [MORE STUFF...] -ea:model... [MORE STUFF...]  -da [EVEN MORE STUFF...]
.....

IntegratedWebLogicServer startup time: 959 ms.
IntegratedWebLogicServer started.