Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

25
Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto

Transcript of Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Page 1: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Servlet-JSP and HtmlFixture exercise and solution

Alessandro Marchetto

Page 2: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Exercise:

NameWeb, four tasks: a) testingb) bug fixingc) maintenanced) evolution

Page 3: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Note

- Download fitnesse.zip http://selab.fbk.ru/marchetto/software/fitnesseImproved.zip

- Unzip it into the desktop

- Take a note of the directory path

Page 4: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Steps

Download the zip file “NameWeb.zip” and put it in the desktop http://selab.fbk.eu/marchetto/exercise/exe_testing/NameWeb.zip

1. Import it into EclipseFileImport”Existing Project into Workshopace” and click “next” select “Archive File” Browser your desktop to select the “exercise1.zip” click “Finish”

2. In the new Eclipse project: -- (if needed) create a directory called "work" if it doesn't exist -- (if needed) delete subdirectories of  the directory "work" if it exist and contains errors

-- (if needed) Start Tomcat or restart it-- open the "Tomcat project" menu (right-button mouse in the project) and update the Tomcat context

Page 5: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

3. Start Fitnesse for the imported project (named nameWeb)-- remember to modify the classpath with the path of the fitnesse.zip in your desktop

!path C:/…/fitnesseImproved/lib/*.jar

4. Execute the tasks described in wiki page: a) write Fit tables

b) bug fixingc) maintenanced) evolution

Page 6: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

NameWeb is composed of:

1. A client side page named index.html- it contains a form composed of 2 text fields (name and surname) and a submit button - the form sends its data to the JSP page

2. A JSP page named nameWeb.jsp- it gets name and surname sent by the client - it stores name and surname in a JavaBean- it reads the data stored in the JavaBean- it writes in output a string such as “Welcome: name surname”

3. A JavaBean named beanPck.NameBean- it defines a field of type string - it contains two methods used to set/get the string

The NameWeb application

Page 7: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.
Page 8: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

NameWeb –JSP and Form – (1)

Please, insert the requested data for JSP:<br><form method="get" action="nameWeb.jsp" > Name: <input type="text" name="nameJ" /> <br /> Surname: <input type="text" name="surnameJ" /> <br /> <input type="submit" value="submitToJSP" /></form>

index.html

Page 9: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

NameWeb – Servlet, JSP and Form – (3)

<HTML> <HEAD>

<TITLE> JSP for name and surname </TITLE> </HEAD><BODY> <P> <jsp:useBean id="beanN" class="beanPck.NameBean" /> <% String name=request.getParameter("nameJ"); String surname=request.getParameter("surnameJ"); beanN.setContent(name, surname); %> <H1>Welcome: <I> <% String content=beanN.getContent(); out.println(content); %> </I> </H1> </BODY></HTML>

nameWeb.jsp

Page 10: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

NameWeb – Servlet, JSP and Form – (5)

package beanPck;

public class NameBean {

private String content="anonymous";

public String getContent() { return(content); }

public void setContent(String name, String surname) { content=name+surname; }

}

beanPck.NameBean

Page 11: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

The Fitnesse-wiki contained in the Eclipse project to do these tasks:

1) TestingWrite a test-case using the HtmlFixture for testing the application...

2) Bug FixingThere is a bug in the main functionality of the NameWeb application.

3) Maintenance:Refactoring: extract in a function the code used to concatenate name and surname written in the form by the user

4) Evolution:Add a field “nation” in the form so that the new output will be such as:“Welcome: name surname from nation” instead of “Welcome: namesurname”

Page 12: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

1) Testing:

Write a test-case using the HtmlFixture for testing the application as follows:

A)- call the index.html- verify that the page does not contain/define a title- verify if the name of the “Surname” field of the HTML form is

“surnameJ” (note that it is an attribute of the element input)- verify how many input of type submit are contained in the page- verify that only 1 form element is contained in the page

B)- call the page index.html- fill the form with “myName” and “mySurname”- click the submit button- verify if the title of the built page is “JSP for name and surname”

Page 13: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

|!-com.jbergin.HtmlFixture-!|| http://localhost:8080/NameWeb/index.html | requestPage |?

Test case (A)

Page 14: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

|!-com.jbergin.HtmlFixture-!|| http://localhost:8080/NameWeb/index.html | requestPage |?

Test case (B)

Page 15: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

2) Bug fixing:

There is a bug in the main functionality of the NameWeb application.

Page 16: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

!path fitnesse.jar!path *.jar!path lib/*.jar!path classes!path lib

|!-com.jbergin.HtmlFixture-!|| http://localhost:8080/ex2/index.html | requestPage || Focus | requestPage || Element Focus | nameJ | input | textField1 || Set Value | myName || Focus | requestPage || Element Focus | surnameJ | input | textField2 || Set Value | mySurname || Focus | requestPage || Element Focus | f_jsp | form || Submit | response1 || Focus | response1 || Has Text | Welcome: myName mySurname |

Test case

Page 17: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

JavaBea

n

Before and After bug-fixing

Page 18: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

3) Maintenance:

Refactoring: extract in a function the code used to concatenate name and surname written in the form by the user

Page 19: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

!path fitnesse.jar!path *.jar!path lib/*.jar!path classes!path lib

|!-com.jbergin.HtmlFixture-!|| http://localhost:8080/ex2/index.html | requestPage || Focus | requestPage || Element Focus | nameJ | input | textField1 || Set Value | myName || Focus | requestPage || Element Focus | surnameJ | input | textField2 || Set Value | mySurname || Focus | requestPage || Element Focus | f_jsp | form || Submit | response1 || Focus | response1 || Has Text | Welcome: myName mySurname |

Test case

Page 20: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

JavaBea

n

Before and After the task

Page 21: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

package beanPck;

public class NameBean {

private String content="anonymous";

public String getContent() { return(content); }

public void setContent(String name, String surname) { content=concatenate(name, surname); }

public String concatenate(String name, String surname){return name+" "+surname;

}

}

beanPck.NameBean

Page 22: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

4) Evolution:

Add a field “nation” in the Form so that the new output will be such as:“Welcome: name surname from nation” instead of “Welcome: name surname”

Page 23: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Test case!path fitnesse.jar!path *.jar!path lib/*.jar!path classes!path lib

|!-com.jbergin.HtmlFixture-!|| http://localhost:8080/ex2/indexEvolution.html | requestPage || Focus | requestPage || Element Focus | nameJ | input | textField1 || Set Value | myName || Focus | requestPage || Element Focus | surnameJ | input | textField2 || Set Value | mySurname || Focus | requestPage || Element Focus | nationJ | input | textField3 || Set Value | myNation || Focus | requestPage || Element Focus | f_jsp | form || Submit | response1 || Focus | response1 || Has Text | Welcome: myName mySurname from myNation |

Page 24: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

Before the evolution

Page 25: Servlet-JSP and HtmlFixture exercise and solution Alessandro Marchetto.

After the evolution