COMP220/COMP285: Software Development Tools
Lab 1-3: Check that Ant and JUnit are properly installed,
work with Eclipse+Java, Eclipse+JUnit and start working
with Ant
General Note
Please, do not start any later lab assignment if previous ones were not finished.
If you will not be able to finish all the required steps during the lab sessions, continue this
yourself in your own time. On this lab session any previous work could possibly be used by
you in your Lab Test. If not finalised strictly before this date, you will have not enough time
in the Class Test to do that.
Please do step-by-step everything suggested below.
1. To check that Ant does work,
Open command prompt window, and RUN
ant -version
You should see something like
H:\>ant -version
Apache Ant(TM) version 1.8.2 compiled on December 20 2010
2. To check that new version of JUnit (4.9) does work,
RUN
java org.junit.runner.JUnitCore org.junit.tests.AllTests
Note that org.junit.runner.JUnitCore is a test runner of JUnit4.9, and it runs the tests
from org.junit.tests.AllTests. The result should be
H:\>java org.junit.runner.JUnitCore org.junit.tests.AllTests
JUnit version 4.9
...........................................................................
.....
...........................................................................
.....
...........................................................................
.....
...........................................................II..............
.....
...........................................................................
.....
...................................................................I.......
.....
............................................
Time: 9.562
OK (521 tests)
This can go slowly (longer than 9.5 seconds as above) and even not always succeed. Just try
again when finished.
JUnit runs some example test cases from the test suite in
C:\JAVA\junit4.9\org\junit\tests\AllTests.class
(Open the file AllTests.java to see the list of these test cases. Please, do not change that file!).
Each dot shows one of the test methods in these test cases passed successfully.
ADDED:In the above run, "I" means a test method which was annotated as @Ignored and so
actually not run. Sometimes in the above run of AllTests we can see "E" after a dot ".". This
means that correspponding test method failed. The reason could be like "test timed out after
1500 milliseconds" in a test method @Test(timeout=1500) public void
testsRunInParallel() {...}. Evidently, passing such a test with a given "timeout" can
depend on a current state of your computer. So, it can either pass successfully or not in
different circumstances.
3. Browse and bookmark the documentation for JUnit
here
4. Browse and bookmark the documentation of Ant here
Run the simplest Ant build file
Create the file build.xml
Put this file build.xml in a directory, say H:\tmp and run Ant from this directory:
H:\tmp>ant
Buildfile: H:\tmp\build.xml
init:
[mkdir] Created dir: H:\tmp\somedir
BUILD SUCCESSFUL
Total time: 0 seconds
Look at the directory tmp to find new subdirectory somedir.
Now, run
H:\tmp>ant clean
Buildfile: H:\tmp\build.xml
clean:
[delete] Deleting directory H:\tmp\somedir
BUILD SUCCESSFUL
Total time: 0 seconds
Now, you hopefully should understand the meaning of what is written in the build file and of
the above two commands ant and ant clean
5. Eclipse
To open the required version of Eclipse, do: Start -> All Programs -> JAVA Apps ->
Eclipse.
Note that Eclipse itself resides in C:\JAVA\eclipse.
Please wait: Eclipse starts slowly.
You might be asked to select a workspace - either to choose an existing directory or to enter
the name of a new one (in the appearing dialog box). Here, in the lab machines the
workspace might be already pre-selected in your home directory H: as subdirectory
H:\eclipse
You will need to find it out in your H: drive, just to look at it time to time to see what really
happens there.
If necessary, you can switch to any other workspace or to create new workspace by choosing
File -> Switch Workspace...
At the start you should see: Welcome window with the buttons Oveview, Tutorials,
Samples, What's New, and (in the upper right corner) Workbench.
Note, that Welcome window contains links to Eclipse Tutorials. You can read them yourself
to start using Eclipse independently or alongside with COMP220.
If you see anything else (actually the Workbench of Eclipse), but not Welcome window, you
can open it via Help in the menu. You can Also use "Home" button (if you see it in the
current state) in the upper right part to return back to the Welcome window.
6. Creating a New Project in Eclipse and further work
with Eclipse related to Java programming
a. Click on "Workbench" button in the Welcome window looking like an arrow. Most
probably you will see the Java perspective (look at the first word in the title bar). It could
also be Resource perspective . You can choose any existing perspective via menu:
Window -> Open Perspective (-> Other...) -> Resource.
This can also be done by using the button "Open Perspective" in the upper right corner.
b. Follow instructions in Creating a Java Project and further steps.
Note that anything what was required from you to resolve, generate, etc. can possibly be
used in your LAB TEST. If missing some steps now, then during LAB TEST you will
have not enough time!
c. Pay attention to the COMPULSORY requirement on that you should always create
your own package declarations involving of your surname, name and student ID (in the
form like ID123456; note that it is not allowed a component of package declaration
consisting only of digits).
Note that slides labelled as for self-study are not compulsory and will not be included in class
and lab tests and the exam. However, it is in your interest to have a try.
d. Exit Eclipse
7. Eclipse and JUnit:
Creating and running a JUnit test case
1. Go to the group of slides Eclipse and JUnit, Slide 3, etc.
2. Then follow step-by-step all instructions in slides, trying to repeat (understand and
remember) everything what was demonstrated in lectures.
Note that anything what was required from you to resolve, generate, etc. can possibly be
used in your LAB TEST. If missing some steps now, then during LAB TEST you will
have not enough time!
After doing this you will be ready to start using JUnit for test-driven programming (or test
driven development, TDD) from inside of Eclipse.
8. Running Simple Ant Build File
See also Lecture Slides:
6. Introducing Ant
7. Ant and XML: Getting Started
1. Create directory
H:\Antbook\ch02\firstbuild -- called below as current directory
2. Populate this directory by two files from the lecture:
Main.java:
public class Main {
public static void main(String args[]) {
for(int i=0;i
compilation complete!
3. RUN Ant from the current directory
H:\Antbook\ch02\firstbuild>ant
and look at the Ant output, including the contents of the current directory.
4. RUN Ant again. Do you see any difference? Try to understand what it means and why.
5. RUN Ant again with the content of Main.java changed inessentially (say, by adding an
unnecessary space and saving). Again, what is the difference and how can it be explained?
6. What if task in build.xml is misspelled, say, as ? RUN it, and after
realizing what happened recover build.xml back.
7. RUN it with omitted the end tag in build.xml and see what is the effect. Do
not forget to recover build.xml back.
8. RUN it, with misspelled srcdir attribute as sourcedir in build.xml. Do not forget to
recover build.xml back.
If your build file has no errors, there may be errors because comiler fails to compile
your code.
9. Delete semicolon after println call in Main.java. RUN Ant. In this case build.xml is
not responsible for BUILD FAILED. Do not forget to recover Main.java back.
The key point: failure of a single task halts the entire build resulting in BUILD FAILED.
There is no point in packaging or delivering a project if it did not compile.
Looking at the build in more detail:
10. TRY the command ant with verbose mode (twice—when Main.class does already
exist, and when it does not exist yet (or just deleted))
H:\Antbook\ch02\firstbuild>ant -verbose
What is the difference? Pay attention to and explain these lines of the Ant output:
H:\Antbook\ch02\firstbuild>ant -verbose
...
compile:
[javac] Main.class skipped - don't know how to handle it
[javac] Main.java omitted as Main.class is up to date.
...
The above was quite a simple laboratory work. You should try to get a complete
understanding.
Later the level of difficulty will increase as we will consider more complex tasks.
If the material from the following Labs was not covered yet on the lectures, you can still
try to go ahead to have more time in the future labs.