Difference between revisions of "User:Qinzhi"
(One intermediate revision by the same user not shown) | |||
Line 101: | Line 101: | ||
It's very convenience for programming. | It's very convenience for programming. | ||
16.Writing and running JUnit tests | 16.Writing and running JUnit tests | ||
+ | Since the package cs.ecl.basics.simple is already include test folder. | ||
+ | Also it includes testAddMedia() and testRetrieveLast() functions. | ||
+ | I clicked run, it shows 2 errors: | ||
+ | @Test | ||
+ | public void testAddMedia() { | ||
+ | fail("Not yet implemented"); | ||
+ | } | ||
+ | @Test | ||
+ | public void testRetrieveLast() { | ||
+ | fail("Not yet implemented"); | ||
+ | } | ||
+ | I changed to following: | ||
+ | package cs.ecl.basics.simple.example1; | ||
+ | import junit.framework.Assert; | ||
+ | import org.junit.Test; | ||
+ | public class LibraryTest { | ||
+ | Library<DVD> Ld = new Library<DVD>(); | ||
+ | @Test | ||
+ | public void testAddMedia() { | ||
+ | DVD d = null; | ||
+ | Ld.addMedia(d); | ||
+ | } | ||
+ | @Test | ||
+ | public void testRetrieveLast() { | ||
+ | Assert.assertEquals(null,Ld.retrieveLast()); | ||
+ | } | ||
+ | } | ||
+ | The program runs correctly. | ||
+ | Assert.asserEquals is a function to check 2 parameters are equal or not. | ||
+ | End. |
Latest revision as of 23:34, 2 February 2011
Basic Lab:
1. Preparing Eclipse 1)Goto Eclipse from http://www.eclipse.org. 2)Choose Eclipse IDE for java Developers and download it. 3)Unzip eclipse to c:\eclipse\Basics folder 4)Doubleclick eclipse.exe in c:\eclipse\Basics\eclipse 5)Workspace type ./wksp/lab 6)Download and install SVN according the instruction at http://zenit.senecac.on.ca/wiki/index.php/Basics_:_Install_Eclipse_Plugins 7)Download JUnit from https://github.com/KentBeck/junit/downloads and unzip it to c:\junit4.9b2. 8)Add junit-4.9b2 to the CLASSPATH, for example: classpath=.;c:\junit4.9b2\junit-4.9b2.jar;c:\junit4.9b2 9)Test if junit is installed correct or not by typing: java org.junit.runner.JUnitCore org.junit.tests.AllTests If it display ok (508 tests), it means junit is installed correctly. 2. Creating your first Java project 1)Open our SVN server and export all samples to c:\ecl500samples. 2)Click Import->existing projects, select c:\ecl500samples\w11-basics-simple, check copy projects into workspace. 3)Edit Library.java by adding System.out.println("Hello"); in main. 4)Click run, and the console will display Hello. 3. Browsing Java elements using the package explorer In the left side of Eclipse, that is the package explorer. Use mouse to expend cs.ecl.basics.simple, I can see src and JRE System Library. 4. Editing Java element I clicked Library.java and added following code: @Override public String toString() { return "Library [resources=" + resources + "]"; } 5. Creating a Java class In package explorer, right click on src folder, and select new->class, type student, and click enter key. Actually Library.java is a class, I'm going to use it as an example instead of student.java. 6. Renaming Java elements I changed Video.java to Audio.java by changing the interface name. 7. Moving and copying Java elements I copied Audio.java and paste it in the project, and changed name to Video, then Video.java is created. If I use mouse to move Video.java to Media.java, it's the same as copying. 8. Navigate to a Java element's declaration In Library.java, I hightlight Media, the Media became gray background. Then I right click on Media, and select open declearation, it switch to Media.java. 9. Viewing the type Hierarchy In Media.java, I hightlight Media, the Media became gray background. Then I right click on Media, and select open type hierarchy. The left panel show Media as a menu, Audio, Book, Video as sub menu. 10.Searching the workbench In menu, select search, a window pop up. I input Audio, and click search. At the bottom of eclipse, a window shows 1 match in workspace. And it showed public interface Audio extends Media{ I click on that, it switched to Audio.java. 11.Running your programs I changed the main in Library.java: public static void main(String[] args) { System.out.println("Hello"); Audio a = null; Library<Audio> La = new Library<Audio>(); La.addMedia(a); System.out.println(La.retrieveLast()); } I clicked run, it shows: Hello null 12.Debugging your programs In Main procedure, I click the line La.addMedia(a), a blue dot appear on the left La.addMedia(a). In menu, I click run->debug. when popup a window, click yes. Then the program will stop at La.addMedia(a). I can choose run->step into in the menu to follow the program. I can click run->terminate to end the debugging. 13.Evaluating expressions In menu, I select window->show view->expressions. I added La, Audio and 3+3. Then I click debug. The program stopped at La.AddMedia(a); In expressions window, it shows: "La" (id=16) "Audio" <error(s)_during_the_evaluatior 6 So expressions can be used in debug mode to check the runtime value. 14.Evaluating snippets snippets used to speed up typing. for example: I put Audio.java as a snippet, named InterfaceTemplate, and add a variable name_1, default value is "Audio". package cs.ecl.basics.simple.example1; public interface ${name_1} extends Media { } I created a DVD.java interface, open it, and I deleted everything in DVD.java. Then I drag it to the middle empty place, it popped a window. I changed the value of name_1 to DVD. Then DVD.java is created. 15.Using the Java browsing perspective In menu, select Window->Open perspective->Java browsing. A new windows arrangement shows: The top 4 windows are projects, packages, types and members. The bottom window is the program window. I can drag what I want from types and members to the bottom window. It's very convenience for programming. 16.Writing and running JUnit tests Since the package cs.ecl.basics.simple is already include test folder. Also it includes testAddMedia() and testRetrieveLast() functions. I clicked run, it shows 2 errors: @Test public void testAddMedia() { fail("Not yet implemented"); } @Test public void testRetrieveLast() { fail("Not yet implemented"); } I changed to following: package cs.ecl.basics.simple.example1; import junit.framework.Assert; import org.junit.Test; public class LibraryTest { Library<DVD> Ld = new Library<DVD>(); @Test public void testAddMedia() { DVD d = null; Ld.addMedia(d); } @Test public void testRetrieveLast() { Assert.assertEquals(null,Ld.retrieveLast()); } } The program runs correctly. Assert.asserEquals is a function to check 2 parameters are equal or not. End.