Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Java Tools for Extreme programming mastering open source tools including - Hightower R.

Hightower R. Java Tools for Extreme programming mastering open source tools including - Wiley publishing , 2002. - 417 p.
ISBN: 0-471-20708
Download (direct link): javatoolsforextremeprog2002.pdf
Previous << 1 .. 58 59 60 61 62 63 < 64 > 65 66 67 68 69 70 .. 159 >> Next

"org.jnp.interfaces.NamingContextFactory"); env.setProperty("java.naming.provider.url", "localhost:1099"); env.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming");
}
}
IDEs Support JUnit and Ant
Plug-ins are available for Forte, NetBeans, TogetherSoft ControlCenter, JBuilder, VisualAge, and so on for both JUnit and Ant. We create many of our tests by generating the tests' started code in the NetBeans IDE.
191
The ProductTest started code was initially generated with NetBeans support for JUnit. You specify the class, and NetBeans generates the started skeleton to test your class. Cool beans!
It's our considered opinion that no JSP should know whether you are using JDBC, flat files, or entity beans to manage the persistence of the system. Thus, we decided to add support for adding and removing products behind the CategorySystem fa3ade class. In fact, the implementation of the client-side piece of the Product entity is in CategoryDB, and the public interface is defined in the Category abstract class.
Here are the additional methods that we added to the CategorySystem class (xptoolkit.petstore.model. CategorySystem):
public void createProduct(Product product) throws Exception{ currentCategory.createProduct(product); if(currentSubcategory!-null)currentSubcategory.invalidate();
}
public void editProduct(Product product) throws Exception{ currentCategory.editProduct(product); if(currentSubcategory!-null)currentSubcategory.invalidate();
}
public void deleteProduct(int id) throws Exception{ currentCategory.deleteProduct(id);
if(currentSubcategory!-null)currentSubcategory.invalidate();
}
Here are the corresponding methods we added to the Category class:
public abstract void createProduct(Product product) throws Exception;
public abstract void editProduct(Product product) throws Exception;
public abstract void deleteProduct(int id) throws Exception;
The actual implementation of these methods is in the CategoryDB class, as follows:
public void createProduct(Product product) throws Exception{ getHome().create(new Integer(product.getId()), product.getName(), product.getQty(), product.getDescription(), product.getFkSubcategoryId(), new java.math.BigDecimal(product.getPrice()));
}
public void editProduct(Product product) throws Exception{ xptoolkit.petstore.entity.Product p
-getHome().findByPrimaryKey( new Integer(product.getId())); p.setName(product.getName()); p.setDescription(product.getDescription()); p.setPrice(new java.math.BigDecimal(product.getPrice())); p.setQty(product.getQty());
}
public void deleteProduct(int id) throws Exception{
192
getHome().findByPrimaryKey(new Integer(id)).remove();
}
private ProductHome getHome() throws Exception{
Object ref;
InitialContext jndiContext=null; jndiContext = new InitialContext(env); ref = jndiContext.lookup("ProductBean"); return (ProductHome)
PortableRemoteObject.narrow (ref, ProductHome.class);
}
This code should look familiar. It is much like the code in our test, except that now we are using it to implement the public interface to our Web application. You should add tests at the boundary points to every tier in an n-tier architecture. That way, you test the public interface of each tier. This approach becomes particularly useful if things begin to go wrong; when debugging a distributed multitiered application, it's helpful to be able to test access to each tier independently from the rest of the added business logic in the encapsulating tier.
Because the CategorySystem is a very thin wrapper in the case of adding, removing, and editing products, we decided to add the tests in the CategoryTest as follows:
public void testCreateDeleteProduct() throws Exception {
Product p = new Product(){};
p.setId(1119996);
p.setFkSubcategoryId(111);
p.setName("Test1");
p.setDescription("Test1 Description");
p.setPrice(11);
p.setPrice(6);
category.createProduct(p);
Product p2 = category.getProduct(1119996); assertEquals("name after create",p2.getName(), p.getName());
p.setName("Test2");
category.editProduct(p);
Product p3 = category.getProduct(1119996); assertEquals("name after edit", p3.getName(), p.getName());
category.deleteProduct(p.getId());
Product p4 = category.getProduct(1119996); this.assertEquals("product should be gone", -1, p4.getId());
}
This code is fairly simple, because the actual product implementation is tested thoroughly in the entity ProductTest. Essentially, the test creates a product, edits it, and deletes it. It makes sure the product data is added, edited, and removed. Here the test creates the product by calling the category createProduct() method:
Product p = new Product(){};
193
p.setId(1119996);
p.setFkSubcategoryId(111);
p.setName("Test1");
p.setDescription("Test1 Description");
p.setPrice(11);
p.setPrice(6);
category.createProduct(p);
Next, the test makes sure that the product actually was created by looking it up:
Product p2 = category.getProduct(1119996); assertEquals("name after create",p2.getName(), p.getName());
Previous << 1 .. 58 59 60 61 62 63 < 64 > 65 66 67 68 69 70 .. 159 >> Next