Assertions are a very useful feature for testing and debugging, but are rather neglected. Instead of using loggers to provide continuously diagnostic messages in critical programme flow points, or even worse if- else - System.out.println() constructs, assert statements can only be enabled only when necessary. Thus, you avoid wasting valuable resources. In addition, you do not need to add any jar libraries to your IDE project, or application server to use it. In order to use assert, you need to pass the -ea argument, call a non void java method and check a Boolean condition. For instance: assert(Month < 13). Nonetheless, you can not use an assert statement to check the values passed to public methods. Consider for example the following code snippet, from an business components, employees entity object:
protected void doDML(int operation, TransactionEvent e) {
if (operation == DML_UPDATE) {
...
histStartDate = row.getEndDate();
/*Check whether end date equals current date*/
/*if (histStartDate.equals(new Date(Date.getCurrentDate()))){
System.out.println("End date equals current date!");
}*/
assert(histStartDate.equals(new Date(Date.getCurrentDate() ) )) :
"End date equals current date! ";
Another trivial example from an ejb client follows:
public class PlaceBidClient {
public static void main(String [] args) {
...
Bid bid = placeBid.addBid("Lila", Long.valueOf(100), 2001.50);
System.out.println("Bid Successful, BidId Received is:" + bid.getBidId());
assert(bid.getBidId() == 502):"BidId Received is "+bid.getBidId();
}
}
Finally, when run, the log output reads:
-javaagent:C:\Oracle\Middleware\jdev_11gR1\jdeveloper\..\modules\org.eclipse.persistence_1.0.0.0_2-0.jar -Duser.language=en -Duser.country=US -ea actionbazaar.buslogic.client.PlaceBidClient
Exception in thread "main" java.lang.AssertionError: BidId Received is 393
at actionbazaar.buslogic.client.PlaceBidClient.main(PlaceBidClient.java:24)Bid Successful, BidId Received is:393
Wednesday 6 October 2010
Sunday 3 October 2010
A free site for sports funs: http://www.woop.gr/
This is a new free online Greek sports channel covering both national and international soccer, basketball matches, motorcycle racing events and so on. Since Eurosport channel now asks for subscription and payments, this could be an interesting alternative, in case your spouse insists on watching that boring soap opera. Most text is in Greek, but you can find your way around. Try clicking on "Live" links, in order to find something interesting to watch. So, this is it: http://www.woop.gr/
Finally, if you would like to try other online tv channels there is http://2onlinetv.com.
Finally, if you would like to try other online tv channels there is http://2onlinetv.com.
Saturday 25 September 2010
JDeveloper 11g: Using Log4j in ADF view object
Just like the entity object instance, covered here, the process for an ADF view object, goes like this. First, you add the library as shown in the image on your left hand side. Second, you create a log4j.properties file in:
C:\...\Model\src
similar to the following example:
# Set root logger level to INFO and its only appender to ConsoleOut.
log4j.rootLogger=INFO, ConsoleOut
# ConsoleOut is set to be a ConsoleAppender.
log4j.appender.ConsoleOut=org.apache.log4j.ConsoleAppender
# ConsoleOut uses PatternLayout.
log4j.appender.ConsoleOut.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleOut.layout.ConversionPattern=%-5p: [%d] %c{1} - %m%n
log4j.logger.org.apache.jsp=DEBUG
#Addon for
com.sun.faces.level=FINE
Third, if you want to use logging in several view objects, you need to edit your view object super class code like that:
package tuhra.model.framework;
import oracle.jbo.server.ViewRowImpl;
import org.apache.log4j.Logger;
public class TuhraViewRowImpl extends ViewRowImpl {
protected Logger logger = Logger.getLogger(this.getClass());
}
and your view object code like that:
public class AllEmployeesRowImpl extends TuhraViewRowImpl implements AllEmployeesRow {...
public void defineDefaultImage(Number newDefaultImageId){
...
if (logger.isDebugEnabled())
logger.debug("Default Image has been defined!");
}
}
Next your diagnostic message appears in the log output window:
INFO : [2010-09-25 11:55:18,739] AllEmployeesRowImpl - DefaultImage has been defined!
Finally, if you need more tips about appenders:
http://www.mobilefish.com/developer/log4j/log4j_quickguide_appenders.html
The full source code is available on line:
http://code.google.com/p/nickaiva-blogspot/downloads/list
and is given without any guarantee of support whatsoever.
C:\...\Model\src
similar to the following example:
# Set root logger level to INFO and its only appender to ConsoleOut.
log4j.rootLogger=INFO, ConsoleOut
# ConsoleOut is set to be a ConsoleAppender.
log4j.appender.ConsoleOut=org.apache.log4j.ConsoleAppender
# ConsoleOut uses PatternLayout.
log4j.appender.ConsoleOut.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleOut.layout.ConversionPattern=%-5p: [%d] %c{1} - %m%n
log4j.logger.org.apache.jsp=DEBUG
#Addon for
com.sun.faces.level=FINE
Third, if you want to use logging in several view objects, you need to edit your view object super class code like that:
package tuhra.model.framework;
import oracle.jbo.server.ViewRowImpl;
import org.apache.log4j.Logger;
public class TuhraViewRowImpl extends ViewRowImpl {
protected Logger logger = Logger.getLogger(this.getClass());
}
and your view object code like that:
public class AllEmployeesRowImpl extends TuhraViewRowImpl implements AllEmployeesRow {...
public void defineDefaultImage(Number newDefaultImageId){
...
if (logger.isDebugEnabled())
logger.debug("Default Image has been defined!");
}
}
Next your diagnostic message appears in the log output window:
INFO : [2010-09-25 11:55:18,739] AllEmployeesRowImpl - DefaultImage has been defined!
Finally, if you need more tips about appenders:
http://www.mobilefish.com/developer/log4j/log4j_quickguide_appenders.html
The full source code is available on line:
http://code.google.com/p/nickaiva-blogspot/downloads/list
and is given without any guarantee of support whatsoever.
Subscribe to:
Posts (Atom)