Eclipse actual combat reconstruction and testing

xiaoxiao2021-03-06  19

Eclipse is an open source, based on Java-based extensible development platform. As far as it is itself, it is just a framework and a set of services (Figure 1) for building development environments through the plug-in assembly. Fortunately, Eclipse comes with a standard plug-in set, including Java Development Tools, JDTs. Eclipse Platform provides a robust service and API for tool development. It makes integration from a completely different supplier's tools smoothes, and creates a seamless environment for different types of development work.

Figure 1: Eclipse framework

What is reconstructed?

Reconstruction - refers to the modification of the code without changing the software any function, adjust its structure, and improve its understandability, reducing the cost of its modification.

Why is it reconstructed?

The basic idea of ​​reconstruction is to concentrate on simplify design and provide a sustainable development (rather than extended) environment when new demand occurs. Refactoring is a powerful technology, but it is necessary to have a small pace modification program. Because some errors you don't care when you reconstruct, especially when we are handle. There is such a risk that we have to reconstruct existing procedures for existing operations?

Refactoring can improve software design;

Reconstructing can make your code look more easily;

Refactoring can find a latent bug;

Reconstruction can help you improve the speed of programming - prevent system corruption and deterioration during debugging during an iterative process;

Reconstructing this tool has too much benefits, which allows us to develop software faster, and even improve our design quality.

The front also mentioned that manual reconstruction may introduce some errors, so we need a tool -eclipse that can be reconstructed. As long as we know what reconstructed tools have achieved, and understand their applicable, our productivity will greatly improve. Of course, it is necessary to reduce the risk of damage to the code, and a reliable test mechanism is essential. A complete automated test of a group is a powerful bug detector that can greatly reduce the time required for the bug. Frequent operation of this test can greatly reduce the possible possible possible, so it is also very important in reconstruction, Martin Fowler said: "No test is not easy to recover, which will give you trouble." . The same Eclipse also integrates support for JUnit.

Figure 2: Java Workbench for Eclipse

Introduction to the reconstruction technique used herein:

Method 1: Encapsulate Field (package value domain) - There is a public value domain in the class. Declare it as private and provide a response access function.

Public string _name;

Private string _name;

Public string getname () {return_name;

Public void setname (string arg) {_name = arg;}

Technical 2: PULL UP METHOD - Some functions generate exact same results in each SubClass. Move this function to SuperClass.

Three: Extract Interface (Refining Interface) - Several customers use the same subset in the CLASS interface; or part of the two Classes interfaces. Refine the same subset into a separate interface.

Ok, after a simple understanding of why the introduction and testing introduction and importance, let us use Eclipse step by step to refactively and test:

Step 1: Establish an engineering

press

Button Select Java Open the Java Working Environment (Figure 2), select Menu File> New> Project ... (click Right-click on the Package Explorer bar) to pop up the New Project dialog box, select Java> Java Project> Next, fill in the project name (I am traffic)> Next, select the libraries tab in the Settings dialog (Figure 3), click the Add External Jars button to join Junit.jar (you can go to http://www.junit.org/ free download), last Click Finish to complete. Such a project is built. Figure 3: Add JUnit

Step 2: Establish a class

Originally want to follow the principle of extreme programming, before establishing a new class write code, you must first create a completely automatic unit test (especially before reconstruction). But because Eclipse seems to be unable to run this test before this class (maybe, please tell me if you know), say this class. So I first set up these classes and write tests.

In the Package Explorer column (menu selection can also) Right-click Select New> Source Folder pop-up New source folder dialog box in Folder Name (I am SRC). Then right-click on the Package Explorer column Select New Java Class dialog box, enter the class name in Name (I am car), press Finish to complete. The same method is also established to establish TRUCK and PERSON classes (class diagrams 1), and the code is inventory.

Listing 1:

//Car.java........

Public class car {

Public string go () {

EnginesTarted

Return "VROOM";

Else

Return "...";

}

Public void startENGINE () {

Enginestarted = True;

}

Public void stopenge () {

EnginesTARTED = FALSE;

}

Private boolean engineestarted;

}

//Truck.java......

Public class truck {

Public string go () {

EnginesTarted

Return "rumble";

Else

Return "...";

}

Public void startENGINE () {

Enginestarted = True;

}

Public void stopenge () {

EnginesTARTED = FALSE;

}

Public void loadingcargo () {

// load cargo

}

Private boolean engineestarted;

}

//Person.java........

Public class person {

Public Person (String Arg) {

IF (arg.equals ("car") {

Car vehicle = new car ();

Vehicle.startenge ();

Vehicles = vehicle.go ();

}

ELSE IF (arg.equals ("truck")) {

Truck Vehicle = New Truck ();

Vehicle.startenge ();

Vehicles = vehicle.go ();

}

}

Public string getvehicle () {

Return vehicles;

Private string vehicles;

}

Class diagram 1

Step 3: Establish test selection File> New> Other pop-up New dialog box Select JUnit> TestCase (Figure 4)> Next Go to the next dialog, fill in Test Calss (I am Person here), Test Case will automatically fill in to Persontest (Figure 5), pressing Finish to complete the establishment of test cases. The code is like a list.

Figure 4: New test case

Figure 5: New test case

Listing 2:

Import junit.framework.testcase;

Public Class Persontest Extends Testcase {

Public Persontest (String Name) {

Super (Name);

}

Public void testGetvehicle () {

Assertequals ("VROOM", ControlCar.getVehi ());

}

Public static void main (String [] args) {

JUnit.textui.teStrunner.Run (New Persontest ("TestgetVehi");

}

Protected void setup () throws exception {

Super.setup ();

Controlcar = New Person ("car");

Controltruck = New Person ("TRUCK");

}

Private persot controlcar;

Private persot control;

}

Step 5: Testing the Person class finally writes these classes, let me test it! Select menu Run> Run ... Popked the Run dialog (Figure 6), select Juint Click New, fill in the test class name in the Test Class (I am Persontest), then click Run, will have JUnit's graphical interface appear In the left bar, if you don't appear, click the JUnit tab in the right lower corner of the original Package Explorer column (Figure 7). Among them, the green representative test is successful, and the red fails.

Figure 6: Run test

Figure 7: JUINT's GUI

Step 6: Start reconstruction of the final round to refactor, establish a good test, no convenience of reforming. Ok! Since there? Let's see if the code is there any bad taste? It is clear that the method in the CAR class and the TRUCK class can extract an abstract base class. Before resolving this bad taste, you should first apply the two categories of encapsulation. In the CAR class, use the mouse to use the mouse to select Refactor> Encapsulate Field ... (Fig. 8) to pop up the dialog box of the package value domain, do not have to fill in anything, press OK to complete the value of the value. There is no error in running test (test is very important, you have to do one step, test once). Of course, you can also press the Preview button to see it for those changes. If you don't need to modify, you can get it. The value field package for the TRUCK class is also completed.

Figure 8: Refactor menu

Next, create a VEHICLE class as the base class for Car and TRUCK, without writing any code. Inherit the PUBLIC CLASS Car Extends Vehicle in the CAR class. Ok, I can move these functions (2). On the CAR class, the cho () method is right-click, select Refactor> Pull Up ... pop-up the moving pair of picture. Set as shown in Figure 9. You can see how he is converted all the way, pressing this reconstruction according to Finish. Run the test. This refactor is also carried out on the TRUCK class. Figure 9: PULL UP dialog

Ok, let's take a look at what reconstruction can you do? It can be seen that the loadCargo () function in the TRUCK class is a method of truck loading. If there is a truck type in the future, this method is also required. So you should refine this interface. With this Eclipse automatic reconstruction tool, the refining interface can become so simple. Select the class name of the TRUCK class, right-click Select Refactor> Extract Interface ... Pop-up the Extract Interface dialog (Figure 10), enter the interface name in the Interface Name in the dialog (I am cargotransport), then select the loadCargo () function, You can choose the Preview button to see if it makes changes, click OK to complete the refining interface. Run unit test, the display strip is green. OK succeeded.

Figure 10: Extract interface dialog

Step 7: The final modification finally we will modify the Person class to see the reconstruction, how to reduce the structure, how to reduce the revision cost? The Person class code is modified as a list, then look at the retrograms after the class map (class) figure 2).

Listing 3:

Public class person {

Public Person (String Arg) {

Try {

Class VehicleClass = Class.Forname (arg);

Vehicle = (vehicle) vehicleclass.newinstance ();

Vehicle.startenge ();

} catch (exception e) {

E.PrintStackTrace ();

}

}

Public string getvehicle () {

Return vehicle.go ();

}

PRIVATE VEHICLE VEHICLE;

}

Class Diagraph 2 can see the ability after the reconstruction has a good sustainable development. When adding a new truck, you can do not need to modify the Person class, of course, the reconstructed code also follows an OCP's principle to reduce classes. The coupling between the abstract layers builds the association between the class. This is the power of this reconstruction. Conclusion: The reconstruction tools provided by Eclipse make reconstruction easy, reconstruction can improve your programming speed, so familiar with these tools will help improve your efficiency. Agile development methods use an iterative method to increase program characteristics, so you need to rely on reconstruction techniques to change and expand the design. Of course, the reconstruction tools provided by Eclipse are not necessarily used in the reconstruction. They can be derived when you usually encode, but also provide a method of saving time when making a general code modification. You can also use the code to use (such as a value field package). If you spend more than these tools, you can realize that the time spent is worth it when you use them. Note: Some of this article may be a little powerful, but this does not affect the use of Eclipse actual combat reconstruction.

转载请注明原文地址:https://www.9cbs.com/read-52830.html

New Post(0)