More details at
http://bxm.at/blogs/java/2006/12/29/webappliction_code_coverage
Tuesday, March 31, 2009
Tuesday, March 3, 2009
JSF and Error pages
Have been looking around for a general strategy that for adding an error page to an ADF Application. found there options
Option 1 : make html error pages for each type of exception
Option 2 : make one error page and generalized bean
Option 3 : Make one error page, One task flow for it , In all the managaed beans inside try catch do add return "error_page_task_flow"
Different approaches may be useful in different scenarious
Option 1 : make html error pages for each type of exception
Option 2 : make one error page and generalized bean
Option 3 : Make one error page, One task flow for it , In all the managaed beans inside try catch do add return "error_page_task_flow"
Different approaches may be useful in different scenarious
Sunday, March 1, 2009
why we cant mock static methods
This wasn't obvious to me at first, it took me a while to get why we can't mock static methods. So here it goes the reason in one line,
"Static methods aren't polymorphic, they are resolved at compile time, therefor it's not possible to use mock objects to test."
Hope this help you guys who are scratching your head like I was.... (if you are not sure about polymorphism, think of it as late binding)
In my previous posts i showed one of the ways you can mock static methods, but the limitation is that you need to know the implementation of the class. What if i am using another classes static method whose cource code i dont know.
for example : FacesContext fc = facesContext.getInstance();
"Static methods aren't polymorphic, they are resolved at compile time, therefor it's not possible to use mock objects to test."
Hope this help you guys who are scratching your head like I was.... (if you are not sure about polymorphism, think of it as late binding)
In my previous posts i showed one of the ways you can mock static methods, but the limitation is that you need to know the implementation of the class. What if i am using another classes static method whose cource code i dont know.
for example : FacesContext fc = facesContext.getInstance();
Mocking singletons and Factories
Mocking singletons ., factories may not be that obvious.
public class Singleton {
private Singleton instance = new Singleton ();
public static Singleton getInstance() {
return instance;
}
}
@Test
public void testSingleton() {
Field instanceField = Singleton .class.getField("instance");
instanceField.setAccessible(true);
Singleton realSingleton = instanceField.get(null);
Singleton mockSingleton = EasyMock.createMock(Singleton .class);
try {
instanceField.set(null, mockSingleton);
// now do your testing, Singleton.getInstance() should now return the mock
} finally {
instanceField.set(null, reallSingleton);
instanceField.setAccessible(false);
}
}
This is fairly hacky, not robust to changes in the singleton class, won't work with a security manager, and it may not work well in the case where the field is instantiated in a static initializer and other fields use that instance when they are initialized. But it probably will work in many cases.
public class Singleton {
private Singleton instance = new Singleton ();
public static Singleton getInstance() {
return instance;
}
}
@Test
public void testSingleton() {
Field instanceField = Singleton .class.getField("instance");
instanceField.setAccessible(true);
Singleton realSingleton = instanceField.get(null);
Singleton mockSingleton = EasyMock.createMock(Singleton .class);
try {
instanceField.set(null, mockSingleton);
// now do your testing, Singleton.getInstance() should now return the mock
} finally {
instanceField.set(null, reallSingleton);
instanceField.setAccessible(false);
}
}
This is fairly hacky, not robust to changes in the singleton class, won't work with a security manager, and it may not work well in the case where the field is instantiated in a static initializer and other fields use that instance when they are initialized. But it probably will work in many cases.
Monday, February 2, 2009
Difference between rebuild and make in jdeveloper
The difference is ...
Please note that in both cases the selection makes a difference:
- Rebuild is compiling all selected .java files, regardless if an up-to-date .class file is available or not
- Make is compiling those selected .java files for which no up-to-date .class file is available PLUS all .java files which are on the source path AND dependent on the selected .java files
Please note that in both cases the selection makes a difference:
- if your selection includes only .java files or packages, only files in the same project will be compiled
- if you select one or more projects, compilation will also include any projects which depend on the selected project(s). To prevent building a long list of dependend projects the latest builds of JDev offer the feature Build | Make
Only and Build | Rebuild Only, respectively.
Monday, January 26, 2009
Java Util package doesnt have a generic tree ?
Hi
Many a times developers say that java util package doesn't support a geveric java tree
the api does have
A generic tree can be written as here and here
Google created its own collections framework that is being used in many of its applications as explained in videos 1 and 2. the project can be downloaded from here.
Many a times developers say that java util package doesn't support a geveric java tree
the api does have
A generic tree can be written as here and here
Google created its own collections framework that is being used in many of its applications as explained in videos 1 and 2. the project can be downloaded from here.
Friday, January 23, 2009
Oracle doesnt support dirty read
more details at
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.OracleStatement;
public class ConnectionClass {
public ConnectionClass() {
}
public Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@hyc65003fwks.idc.oracle.com:1521:nn11g",
"scott","tiger");
con.setAutoCommit(false);
System.out.println("isolation lvl "+con.getTransactionIsolation());
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
return con;
}
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
ConnectionClass obj = new ConnectionClass();
Connection con1 = obj.getConnection();
Connection con2 = obj.getConnection();
obj.doUpdate(con1);
obj.read(con2);
con1.rollback();
con1.close();
System.out.println("again reading.......");
obj.read(con2);
con2.close();
}
int i=0;
public void doUpdate(Connection con) throws SQLException {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
i++;
String sql = "insert into delme values('aa"+i+"')";
System.out.println("sql >>>"+sql);
System.out.println(stmt.executeUpdate(sql));
stmt.close();
}
public void read(Connection con) throws SQLException {
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from delme";
System.out.println("sql >>>"+sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
}
}
/* Exception in thread "main" java.sql.SQLException: READ_COMMITTED and SERIALIZABLE are the only valid transaction levels
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:171)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:439)
at oracle.jdbc.driver.PhysicalConnection.setTransactionIsolation(PhysicalConnection.java:3988)
at jdbcproj.ConnectionClass.getConnection(ConnectionClass.java:22)
at jdbcproj.ConnectionClass.main(ConnectionClass.java:29)
Process exited with exit code 1. */
Subscribe to:
Posts (Atom)