Showing posts with label explanation. Show all posts
Showing posts with label explanation. Show all posts

Thursday, June 11, 2009

JUnit on myeclipse


JUnit on myeclipse

by Satya Siripuram





What is Junit


JUnit is a simple open source Java testing framework used to write and run repeatable automated

tests. It is an instance of the xUnit architecture for unit testing framework. Eclipse supports

creating test cases and running test suites, so it is easy to use for your Java applications.

JUnit features include:


Assertions for testing expected results

Test fixtures for sharing common test data

Test suites for easily organizing and running tests

Graphical and textual test runners


What is a test case


A test case is a class which holds a number of test methods. For example if you want to test some

methods of a class AddClass you create a class AddClassTest which extends the JUnit TestCase class and place your test methods in there.




Working with Junit on myeclipse:


Step 1: select Window → Show View → Other → Java → select Junit → Ok


Just beside Package Explorer tree you could find JUnit view.



Example application:


1. Create a project :

a. go to File → New → Other → Java Project

b. name the project as TestingJUnitFinish

    1. Create a simple class:

a. right click on the project TestingJUnit goto New → Class

b. give package name as test.junit, class name as AddClass → Finish


AddClass.java


package test.junit;

public class AddClass {

public int add(int c ,int d)

{

return c+d;

}

}


3. Adding junit.jar to the project:

a. right click on the project → Properties → Java Build Path → select Libraries tab → Add External Jars → select the junit.jar → Ok


Note: download junit.jar here http://sourceforge.net/project/showfiles.php?group_id=15278&package_id=12472


  1. Creating test cases :

a. right click on the project TestingJUnit → New → Other → select Junit from the tree → Junit Test Case

b. give the package name as test.junit , class name as AddClassTest → only check setUP().

c. click on Browse for Class Under Select and type the name of the class that you want to test , here AddClass → Finish

d. add the following line of code inside testAdd()


assertEquals(13, ac.add(6, 7));


13 is the expected value.

ac.add(6,7) is the actual value



AddClassTest.java


package test.junit;


import junit.framework.TestCase;


public class AddClassTest extends TestCase {

private AddClass ac;


protected void setUp() throws Exception {

ac = new AddClass();

}


public void testAdd() {


assertEquals(13, ac.add(6, 7));


}


}


5. Running the Test Cases :

a. Right click on the project TestingJUnit → Run As → JUnit test





on Junit view you could see the following.




for testing give some different value like


assertEquals(12, ac.add(6, 7));


and Run again now you could see the following.






This is how you can test your application.


References:


http://www.scribd.com/doc/18134/Eclipse-tutorial-part-10-Using-JUnit-in-Eclipse


thanks and please send your comments to sat4java@gmail.com



Bookmark this pageDigg this sitesimpyslashdot

Wednesday, June 18, 2008

Explanation on Struts flow.


Note before reading this article : this is not for professionals and it is for the people who stepped into Struts(Beginners) and it is for my future reference.



1. When the jsp page is loaded into server, the "" (what you wrote in corresponding jsp page) creates the neccessary html code to represent the form and then checks for the instance of the corresponding ActionForm in the current session scope.If there was ActionForm object available in the session scope then the data members of the ActionForm is mapped with the input form fields respectively. and then that jsp page is presented to the user.

Let me explain with one example.

JSP pages: login.jsp, success.jsp
Action class: LoginAction
Form name: LoginForm

in login.jsp
two fields username and password
login.jsp

2.When you click on Login button the server receives that request and it wil check in web.xml for the form action with login.do
In web.xml there you can see the following url mapping with .do extension. then it calls the ActionServlet
From there the ActionServlet will take the request processing.





3.the ActionServlet will then looks for the LoginForm and populates the data members with form fields and then the LoginForm will be associated with the session.
4.Once, the request comes to the ActionServlet the ActionServlet [ActionServlet will call the RequestProcessor's process()method.
The RequestProcessor's process() method] wil read the struts-config.xml file and looks for the following entry:




if the path attribute equals to the form action attribute i.e login
5. it then creates the LoginAction object and calls the execute() method on that object by passing the ActionMapping[creates ActionMapping instance and passed to execute()], ActionForm,HttpServletRequest,HttpServletResponse objects.
the execute() method will process the business logic [by calling the model component] and then calls the ActionMapping.findForward("string argument") can be success or failure.
6.this ActionMapping.findForward("string argument") will looks for the subelement of the tag with the name attribute equal to the "string argument" . It then returns the ActionForward object which contains results of the
lookup, which is the value of the path attribute /welcome.jsp (upon success) or /login.jsp (upon failure).

If it is success then the LoginAction returns the ActionForward object to the ActionServlet, which will then forward to the view for presentation.
here now you can see welcome.jsp

uhhhhhh......, I did it. please give me feedback.

resources i have used to write this
Mastering Jakarta Struts book by James Goodwill[It is a good book for struts]
and my class notes.


what is the use of reset() method in ActionForm?

The use of reset() method in ActionForm is it is used by the Struts Framework with each request that uses the ActionForm. The main purpose of reset() method is to reset all the data members of the ActionForm object and allow the object to be pooled for reuse.

The difference between MVC and MVC 2(MODEL 2) ?

MVC was a first generation approach that used JSP pages and the JavaBeans component architecture to implement the MVC architecture for the Web. HTTP requests are sent to a JSP page that implements Controller logic and calls out to the “Model” for data to update the “View.” This approach combines Controller and View functionality within a JSP page and therefore breaks the MVC paradigm. MVC1 is appropriate for simple development and prototyping. It is not, however, recommended for serious development.

MVC2 is a term invented by Sun to describe an MVC architecture for Web-based applications in which HTTP requests are passed from the client to a “Controller” servlet which updates the “Model” and then invokes the appropriate “View” renderer-for example, JSP technology, which in turn renders the View from the updated Model.
The hallmark of the MVC2 approach is the separation of Controller code from
content. (Implementations of presentation frameworks such as Struts, adhere to the MVC2 approach).

copied from sun forums site

What is AOP Aspect Oriented Programming ?

generally definition of AOP is modularizing the cross-cutting concerns.

each module main concern is to provide services for its particular domain.
The common object-oriented technique for reusing common functionality is through inheritance or delegation.
But inheritance can lead to a brittle object hierarchy if the same base class is used throughout an application, and delegation can be cumbersome and still
requires duplicated calls to the delegate object.

AOP is an alternative, With
AOP, you still define the common functionality in one place, but you can declaratively
define how and where this functionality is applied without having to modify
the class to which you are applying the new feature.Cross-cutting concerns can
now be modularized into special objects called aspects.
This has two benefits.
1. the logic for each concern is now in one place, as opposed to being scattered all
over the code base.ex. logging.
2. our service modules are now cleaner since they only
contain code for their core functionality and secondary concerns have been
moved to aspects.ex. student, course module.

Aspect
An aspect is the cross-cutting functionality you are implementing. It is the aspect,
or area, of your application you are modularizing. The most common (albeit simple)
example of an aspect is logging. Logging is something that is required
throughout an application. However, because applications tend to be broken
down into layers based on functionality, reusing a logging module through inheritance
does not make sense. However, you can create a logging aspect and apply it
throughout your application using AOP.
Joinpoint
A joinpoint is a point in the execution of the application where an aspect can be
plugged in. This point could be a method being called, an exception being
thrown, or even a field being modified. These are the points where your aspect’s
code can be inserted into the normal flow of your application to add new behavior.
Adviceis the actual implementation of Aspect..It is advising ur application of new behaviour.
In our Logging example, the logging advice would contain the
code that implements the actual logging, such as writing to a log file. Advice is
inserted into our application at joinpoints.
Pointcuts
A pointcut defines at what joinpoint the advice should be applied. Advice can be applied at any joinpoint supported by AOP framework.
These pointcuts will tell where is the advice to be applied .
You can specify these pointcuts using an explicit class or method names or through regular expressions
that define matching class and method name patterns.Some AOP frameworks
allow you to create dynamic pointcuts that determine whether to apply advice
based on runtime decisions, such as the value of method parameters.
Introductionallows you to add new methods or attributes to the existing classes.
TargetA target is the class that is being advised.

Note: the above information i got it from the Manning in Action Series : Spring ebook and some other books