Thursday, August 10, 2006

TestNG 5.0 Release

Test NG is replacement for JUnit. Super strong and support Eclipse as well. But our shop is using JUnit. So I dont have much time to investigate it.

http://testng.org/doc/

Tony

Java EE XML handling

Java EE provides various technology choices for handling XML documents. Three of these technologies are Java Architecture for XML Binding (JAXB), Streaming API for XML (StAX), and the Document Object Model (DOM) API. This Tech Tip compares these choices, and shows the technologies in use in a sample application.

JAXB

JAXB technology provides a way to bind XML schemas to Java objects so that developers can easily process data in their Java applications. The JAXB API provide methods to unmarshal an XML document into a Java object and marshal a Java object into an XML document. For more information about JAXB, see the Tech Tip What's New in JAXB 2.0.

A significant advantage of using JAXB is that you can compile the schema (or dtd) to generate a Java content tree, and then work with plain Java objects. JAXB is not particularly good in cases where complex schemas are involved and you want to work with only a small set of content.

StAX

StAX is a streaming API for processing XML documents. It's an event-driven, "pull" parser that reads and writes XML documents. For more information about StAX, see the Tech Tip Introducing the Sun Java Streaming XML Parser.

StAX's bidirectional features, small memory footprint, and low processor requirements give it an advantage over APIs such as JAXB or DOM. StAX is particularly effective in extracting a small set of information from a large document. The primary drawback in using StAX is that you get a narrow view of the document -- essentially you have to know what processing you will do before reading the XML document. Another drawback is that StAX is difficult to use if you return XML documents that follow complex schema.

DOM

DOM is platform-neutral and language-neutral API that enables programs to dynamically update the contents of XML documents. For more information about DOM, see the Tech Tip Using the Document Object Model.

DOM creates an in-memory object representation of an entire XML document. This allows extreme flexibility in parsing, navigating, and updating the contents of a document. DOM's drawbacks are high memory requirements and the need for more powerful processing capabilities.


http://java.sun.com/developer/EJTechTips/2006/tt0527.html#2

Tony

Wednesday, August 09, 2006

jMock

This is a really cool open source java testing packages which works very well with JUnit. For a simple example we are going to test a publish/subscribe message system. A Publisher sends objects to zero or more Subscribers. We want to test the Publisher

import org.jmock.*;

class PublisherTest extends MockObjectTestCase {

public void testOneSubscriberReceivesAMessage() {

// set up

Publisher publisher = new Publisher();

Mock mockSubscriber = mock(Subscriber.class);

publisher.add((Subscriber) mockSubscriber.proxy());

// expectations Next we define expectations on the mock Subscriber that specify the methods that we expect to be called upon it during the test run.

final String message = "message";

mockSubscriber.expects(once()).method("receive").with( eq(message) );

publisher.publish(message);

}

}

We expect the receive method to be called with a single argument, the message that will be sent. The eq method is defined in the MockObjectTestCase class and specifies a "constraint1" on the value of the argument passed to the subscriber: we expect the argument to be the equal to the message, but not necessarily the same object. (jMock provides several constraint types1 that can be used to precisely specify expected argument values). We don't need to specify what will be returned from the receive method because it has a void return type.

http://www.jmock.org/

Tony

Thursday, August 03, 2006

Apache Commons-Email 1.0

Commons-Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.

Some of the mail classes that are provided are as follows:

·         SimpleEmail - This class is used to send basic text based emails.

·         MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.

·         HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.

·         EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.

 

http://jakarta.apache.org/commons/email/



Tony

 

Wednesday, August 02, 2006

Hibernate Customized Query To Get Max Values

This is the query to how to get the max string value from a table.

 

Option 1

Use max function inside the DBMS.

 

Double max = (Double) sess.createSQLQuery("select max(cat.weight) as maxWeight  from cats cat where extLicenseID < '5000-0000' ")

        .addScalar("extLicenseID", Hibernate.STRING);

        .uniqueResult();

 

Option 2

Use setMaxResults of the hibernate and addOrder of asc or desc.

 

List cats = sess.createCriteria(Cat.class)

    .add( Property.forName("name").like("F%") )

    .addOrder( Property.forName("name").asc() )

    .addOrder( Property.forName("age").desc() )

    .setMaxResults(50)

    .list();

 

Tony

Forward vs Redirect

 

Forward

  • forward is performed internally by the servlet
  • the browser is completely unaware that it has taken place, so its original URL remains intact
  • any browser reload will simple repeat the original request, with the original URL

Redirect

  • redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
  • a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
  • redirect is always slower than a forward, since it requires a second browser request
  • beans placed in the original request scope are not available to the second request

 

 private void redirect(
    ResponsePage aDestinationPage, 
    HttpServletResponse aResponse
  ) throws IOException {
    
    String urlWithSessionID = aResponse.encodeRedirectURL(aDestinationPage.toString());
    fLogger.fine("REDIRECT: " + Util.quote(urlWithSessionID));
    aResponse.sendRedirect( urlWithSessionID );
  }
 
  private void forward(
    ResponsePage aDestination, 
    HttpServletRequest aRequest, 
    HttpServletResponse aResponse
  ) throws ServletException, IOException {
    
    RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aDestination.toString());
    dispatcher.forward(aRequest, aResponse);
  }

http://www.javapractices.com/Topic181.cjp

 

Tony

 

Content-disposition: Attachment

Best of all, prevent the browser from opening the document in the first place. Instead offer users the choice to save the file on their hard disk or to open it in its native application (Adobe Reader for PDF, PowerPoint for slides, etc.). Unfortunately, doing so requires a bit of technical trickery: you have to add an extra HTTP header to the transmission of the offending file. The header line to be added is "Content-disposition: Attachment". If possible, also add "; filename=somefile.pdf" at the end of this line, to give the browser an explicit filename if the user chooses to save the file.

 

http://www.useit.com/alertbox/open_new_windows.html

 

Tony

 

Be A Developer That Uses AI

Developers will not be replaced by AI, they'll be replaced by developers that use AI. Generative AI tools are revolutionizing the way de...