Wednesday, May 26, 2010

Mock Test cases concept

Idea behind introducing Mock test cases is to avoind dependancies and do the unit testing properly.Here i have given an example to get idea.

public interface DataAccess {
BigDecimal getPriceBySku(String sku);
}
public interface PricingService {
void setDataAccess(DataAccess dataAccess);
BigDecimal getPrice(String sku) throws Exception;
}
public class PricingServiceImpl implements PricingService {
private DataAccess dataAccess;
public void setDataAccess(DataAccess dataAccess) {
this.dataAccess = dataAccess;
}
public BigDecimal getPrice(String sku) throws Exception {
final BigDecimal price = this.dataAccess.getPriceBySku(sku);
if (price == null) {
throw new Exception();
}
return price;
}
}


Lets say we want to write a test case for the PricingServiceImplclass.So we need t o have a concreate implimentation of DataAccess class. If we have get the use of a concreate classs object then our testing is no longer a unit testing.(since it is depemding on that concreate class's object and there can be some test failures due to eoors in that class).
So to avoid that dependancy what we can do is with out using a concreate class we can inject DataAccess implementation using Jmock. So our testing is not depend on the implimentation of DataAccess class and we can perform a proper unit test.

we can do it as following.

private PricingService systemUnderTest;
private DataAccess mockedDependency;
private Mockery mockingContext;
@Before
public void doBeforeEachTestCase() {
mockingContext = new JUnit4Mockery();
systemUnderTest = new PricingServiceImpl();
mockedDependency = mockingContext.mock(DataAccess.class); // here we are injecting
systemUnderTest.setDataAccess(mockedDependency);
}


Saturday, May 22, 2010

data base optimization

* Why we need optimization of our SQL queries ?
Simple you may feel you sql query executes with in few seconds even though u have not used any optimization principles.But think of a situation a busy server which has to execute thousands of queries concurrently.So here Milli second delay also will be a reason for a dead lock. So it is highly recommended to use dead data base optimization principles.

here i am going to discuss how to use indexes to optimize sql queries.
Simply an index is a key which helps to find a particular value with out searching entire table of a data base.There can be more than one values which are mapped to a particular key(Index).Lets say we are finding a value from data set which is generated by a join operation as follows.

SELECT name,price form item i,dealer d WHERE i.id = d.item_id;

here join operator produced huge resulting data set.So if id and item-id are not indexes we need to search through that huge table and it is a useless time consuming process.But if above variables are indexes mysql has to check only index tables and then directly get the mapping rows in the tables.

Some important things that we need to think when we create an index for a table

* indexed a column that you will need it for a validation.(where id = 1000, ordered by name )

* you index should be contains very few records.(if it contains large number of records it is not worth to search though the index table since it has similar number of data like in the value table)

* columns with enum data type is not recommended to indexed.because that column can contain fix number of variable records.(same as declare in enum type). So there can be many rows with the same index.So no point to have large number of records for a indexed column as i mentioned above.

* index should be short one.Because MYSQl supports indexed caching to reduce number of look ups..If the indexes are too long(data size) it cannot caches large number of records.so we cant get the maximum use from the indexed caching .(if the index field is too large we can use prefix index to reduce the length.)

Monday, May 10, 2010

Inversion of control- Spring



It is highly recomended to reduce the dependancies in our classes since it is contradict with the object oriented design pattern.As an instant think you need to get some service from a out side class. So you need to create object of that class and call it.So your code is highly depends on that class.(tightly coupled). another problem that pop up due to this dependancy is we cannot simply chage the properties(of out sider class - using setters) and run the system.To do so we need recompile the project and run.
Inorder to avoid those kind of short commings IOC concept is introduced.

concept :
lets say ther is a class DocumentCreator.And it has subclasses PdfDocCreator and HtmlDocCreator.In our class we are going to get a service from PdfDocCreator or HtmlDocCreator.Then the dependancy derection is towards to DocumentCreator,HtmlDocCreator and PdfDocCreator.That means our class is tightly coupled with those three classes.



To avoid this we can simply define interface instead of DocumentCreator class and implement it for PdfDocCreator and HtmlDocCreator.
Then inorder to get a service from above class structure we can use that interface.Then the dependancy direction is changed.(It means usage of PDf or Html is depends on our class)