import java.awt.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import com.sun.java.swing.plaf.windows.*;public class MyGUI extends JPanel{   private JTextArea txtTestInfo, txtMyCase;   private JLabel lblMyCases;   private JPanel buttonPanel;   private JComboBox cmbMyCases;
   private static final String CASE_BUBBLE= "TC1-Test Bubble Sort";   private static final String CASE_HEAP= "TC2-Test Heap Sort";   private static final String CASE_INSERTION= "TC3-Test Insertion Sort";   private static final String EXECUTE = "Execute";   private static final String EXIT = "Exit";
   public MyGUI(){    txtTestInfo=new JTextArea("Test output from source shown here\n", 6, 20);    txtTestInfo.setLineWrap(true);    txtMyCase = new JTextArea("MyCase info and test validation shown here\n", 4, 15);    txtMyCase.setLineWrap(true);      buildUpScrollGUI();   }   private void buildUpScrollGUI(){      setUpButtonPanel();    JScrollPane btnPane = new JScrollPane(buttonPanel);    JScrollPane textPane = new JScrollPane(txtMyCase);    textPane.setMinimumSize(new Dimension(250, 150));    JScrollPane testDataPane = new JScrollPane(txtTestInfo);
    JSplitPane upSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);    upSplitPane.setLeftComponent(btnPane);    upSplitPane.setRightComponent(testDataPane);    JScrollPane downPane = new JScrollPane(textPane);
    Dimension minimumSize = new Dimension(130, 100);    btnPane.setMinimumSize(minimumSize);    textPane.setMinimumSize(new Dimension(100, 100));    upSplitPane.setDividerLocation(270);    upSplitPane.setPreferredSize(new Dimension(500, 300));
    JSplitPane bigSplitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane, downPane);    bigSplitPane.setDividerLocation(190);
    add(bigSplitPane);    setSize(new Dimension(500, 400));      setVisible(true);  }
  private void setUpButtonPanel(){    lblMyCases = new JLabel("Test Cases:");    cmbMyCases = new JComboBox();      cmbMyCases.addItem(CASE_BUBBLE);      cmbMyCases.addItem(CASE_HEAP);      cmbMyCases.addItem(CASE_INSERTION);
     //Create the open button     JButton executeBtn = new JButton(EXECUTE);     executeBtn.setMnemonic(KeyEvent.VK_S);     JButton exitButton = new JButton(EXIT);     exitButton.setMnemonic(KeyEvent.VK_X);
     BtnListener objButtonHandler = new BtnListener();     // add action Listener     executeBtn.addActionListener(objButtonHandler);     exitButton.addActionListener(objButtonHandler);     buttonPanel = new JPanel();
     GridBagLayout gridbag = new GridBagLayout();     buttonPanel.setLayout(gridbag);     GridBagConstraints gbc = new GridBagConstraints();     buttonPanel.add(lblMyCases);     buttonPanel.add(cmbMyCases);     buttonPanel.add(executeBtn);     buttonPanel.add(exitButton);     gbc.insets.top = 5;     gbc.insets.bottom = 5;     gbc.insets.left = 5;     gbc.insets.right = 5;
     gbc.anchor = GridBagConstraints.EAST;     gbc.gridx = 0;     gbc.gridy = 0;     gridbag.setConstraints(lblMyCases, gbc);     gbc.anchor = GridBagConstraints.WEST;     gbc.gridx = 1;     gbc.gridy = 0;     gridbag.setConstraints(cmbMyCases, gbc);     gbc.anchor = GridBagConstraints.EAST;     gbc.insets.left = 2;     gbc.insets.right = 2;     gbc.insets.top = 25;     gbc.anchor = GridBagConstraints.EAST;     gbc.gridx = 0;     gbc.gridy = 7;     gridbag.setConstraints(executeBtn, gbc);     gbc.anchor = GridBagConstraints.WEST;     gbc.gridx = 1;     gbc.gridy = 7;     gridbag.setConstraints(exitButton, gbc);  }  public void showTestInfo(int[] str ){    txtTestInfo.setText("");    for(int n=0; n< str.length; n++)          txtTestInfo.append(""+str[n]+" ");  }  public void showErrors(String err){   txtMyCase.append(err+"\n");  }  public String getSelectedMyCase() {        return (String) cmbMyCases.getSelectedItem();  }
 class BtnListener implements ActionListener{    private MyCase test;    private String selectedMyCase;
    public void actionPerformed(ActionEvent e){      String searchResult = null;      int[] output=null;
      if (e.getActionCommand().equals(EXIT)){         System.exit(1);      }      if (e.getActionCommand().equals(EXECUTE)){         selectedMyCase = getSelectedMyCase();         if(selectedMyCase.equals(CASE_BUBBLE))             test = new MyCaseMaoPao();         else if(selectedMyCase.equals(CASE_HEAP))             test = new MyCaseHaXi();         else if(selectedMyCase.equals(CASE_INSERTION))             test = new MyCaseInsertion();         output = test.execute(3000);         showTestInfo(output);         }          showErrors(selectedMyCase);          boolean result = ResultVerification.isResultCorrect(output );          showErrors("No Error found = " +result);          long timeTaken = test.getTimeTaken();          showErrors("Testing Time takes = " + timeTaken+"\n");   } } // End of class BtnListener
 private static void createAndShowGUI(){     JFrame.setDefaultLookAndFeelDecorated(true);     JFrame frame = new JFrame("Layered Architecture- Software Testing");     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     MyGUI newContentPane = new MyGUI();     newContentPane.setOpaque(true);     frame.setContentPane(newContentPane);     frame.pack();     frame.setVisible(true);  }  static public void main(String argv[]) {   javax.swing.SwingUtilities.invokeLater(new Runnable() {      public void run() {       createAndShowGUI();    }        });  }}
public class ResultVerification{     static boolean flag = true;
     public static boolean isResultCorrect(int[] arr){       for(int k=0; k<arr.length-1; k++){           if(arr[k] > arr[k+1]){               flag=false;               System.out.println("error  "+ k);               //break;           }       }         return flag;      }}
第二层为测试案例层,包括软件测试工程师所编写的测试案例public interface MyCase{     public  abstract  int[] execute(int len);     public  abstract  long getTimeTaken();}
class Context {    SortAlgorithm alg;
    // Constructor    public Context(SortAlgorithm alg) {        this.alg = alg;    }
    public int[] sortIntArray(int[] a) {        return this.alg.sort(a);    }}
import java.util.Random;public class IntegerArrGenerator{    public static int[] generateInput(int len){     int[] input= new int[len];     Random r = new Random();         for(int m=0; m< len; m++){       input[m] = r.nextInt(len);       }
         return input;    }}
import java.util.*;public class MyCaseMaoPao implements MyCase{   private long startTime;   private long timeTaken=0;
     public int[] execute(int len) {      startTime = System.currentTimeMillis();
      int[] input = IntegerArrGenerator.generateInput(len);
         SortAlgorithm sa = new MaoPao();         Context context = new Context(sa);         int[] intArray = context.sortIntArray(input);
          timeTaken = System.currentTimeMillis() - startTime;         return intArray;    }
    public long getTimeTaken(){      return   timeTaken;  }}
import java.util.*;public class MyCaseHaXi implements MyCase{     //static long time;     private long startTime;   private long timeTaken=0;
     public int[] execute(int len){    startTime = System.currentTimeMillis();    int[] input = IntegerArrGenerator.generateInput(len);
    SortAlgorithm sa = new HaXi();    Context context = new Context(sa);    int[] intArray = context.sortIntArray(input);    timeTaken = System.currentTimeMillis()-startTime;
        return intArray;    }    public long getTimeTaken(){        return   timeTaken;  }}
import java.util.*;public class MyCaseInsertion implements MyCase{     private long startTime;   private long timeTaken=0;
     public  int[] execute(int len){        startTime = System.currentTimeMillis();
         int[] input = IntegerArrGenerator.generateInput(len);       SortAlgorithm sa = new ChaRu();       Context context = new Context(sa);       int[] intArray = context.sortIntArray(input);
       timeTaken = System.currentTimeMillis()-startTime;             return intArray;    }    public long getTimeTaken(){        return   timeTaken;  }}
第三层为被测试软件层(排序算法)public interface SortAlgorithm {     int[] sort(int[] nums);}
public class MaoPao implements SortAlgorithm {   public int[] sort(int[] nums){      for(int i = nums.length; --i >= 0;)         for(int j = 0; j < i; j++){            if(nums[j] > nums[j + 1]){
               //exchange nums[j+1] with nums[j]               int T = nums[j];               nums[j] = nums[j + 1];               nums[j + 1] = T;            }         }      return nums;   }}
public class HaXi implements SortAlgorithm{     public int[] sort(int nums[ ]) {          for(int i=nums.length; i>1; i--){               buildBinaryHeapTree(nums, i - 1);               swapLeadingNodeWithLastNode(nums, i - 1);          }          return nums;     }
     public void buildBinaryHeapTree(int array[], int arrayBound){         int leftChild, rightChild, biggerChild, temp;         int root = (arrayBound-1)/2;
           // Find the bigger child index           for(int i=root; i>=0; i--) {                 leftChild = (2*i)+1;                 rightChild = (2*i)+2;
                 if((leftChild <= arrayBound) && (rightChild <= arrayBound)){                    if(array[rightChild] >= array[leftChild])                        biggerChild = rightChild;                    else                       biggerChild = leftChild;                 }                else{                    if(rightChild > arrayBound)                       biggerChild = leftChild;                   else                      biggerChild = rightChild;                }
               //swap the integer contained in the bigger child index               //with that in the current parent node               if(array[i] < array[biggerChild]){                   temp = array[i];                  array[i] = array[biggerChild];                  array[biggerChild] = temp;               }          }           return;  }
  public static void swapLeadingNodeWithLastNode(int array[], int arrayBound){        int  temp;        temp = array[0];         array[0] = array[arrayBound];         array[arrayBound] = temp;          return;  }}
public class ChaRu implements SortAlgorithm {    public int[] sort(int[] nums){       for (int i = 1; i < nums.length; i++){          int j = i;          int numToBeInserted = nums[i];
          while ((j > 0) && (nums[j-1] > numToBeInserted) ) {             nums[j] = nums[j-1];             j--;          }          nums[j] = numToBeInserted;       }       return nums;   }}
评论