子系统测试到底是什么,与集成测试有什么不同?子系统测试是否仅测试该方法传递给它们时可以工作?
该代码的良好子系统测试
package server.controller;
import server.storage.ParkedUsers;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeSet;
import server.storage.ParkingSpot;
/**
* This is the server class that controls all data access and flow.
* It is also responsible of creating all the parking user objects
* once it searches them in the FIU Database.
*/
public class AccessControlServer extends Thread
{
private static ParkedUsers garage = ParkedUsers.instance("garage.txt");
private final int portNum;
private PrintWriter sout = null;
private HashMap<String, PrintWriter> displayConnections
= new HashMap<String, PrintWriter>();
/**
* constructor that initializes the port number variable
* @param p integer that contains the port number
* to which this server will listen to
*/
public AccessControlServer(int p)
{
portNum = p;
mapConnections();
}
/**
* helper method that maps the text file into the hashmap
*/
private void mapConnections()
{
sout = null;
displayConnections.put("security", sout);
for(TreeSet<ParkingSpot> t: garage.values())
for(ParkingSpot p: t)
{
displayConnections.put(p.getParkingNumber(),
p.getPrintWriter());
}
}
@Override
public void run()
{
try
{
startServer();
}
catch(IOException e)
{
System.out.println("Can't listen on port " + portNum);
}
}
/**
* initializes the server with the specified port number and
* starts listening for connections
* @throws IOException exception thrown if there is a problem
* with the server
*/
public void startServer() throws IOException
{
ServerSocket ss = new ServerSocket(portNum);
while(true)
{
Socket s = ss.accept();
Thread t = new Thread(new ConnectionHandler(s));
t.start();
}
}
/**
* method to send a message to a client
* @param msg sends a string message to the specified client
* @param pout the print writer connection to the client
*/
public void sendMessage(String msg, PrintWriter pout)
{
pout.println(msg);
}
/**
* send the security display the status of all the parking spots
*/
public void sendStatus()
{
ArrayList<String> stat = garage.getStatus();
for(int i = 0;i < stat.size(); i++)
sendMessage(stat.get(i), sout);
}
/**
* method called to reserve a spot for the user
* @param spot the spot to be reserved by the user
* @param id the ID of the user reserving the spot
*/
synchronized public void reserveSpot(ParkingSpot spot, String id)
{
PrintWriter pout = displayConnections.get(spot.getParkingNumber());
sendMessage("reserve", pout);
sendMessage(id, pout);
}
/**
* sends the message to the security display that an
* user has parked on the wrong spot
* @param msg the message to be sent
*/
synchronized void wrongUserDetected(String msg)
{
if(sout == null)
return;
sendMessage("wrong", sout);
sendMessage(msg, sout);
}
/**
* sends the message to the security display that a
* duplicate ID was detected
* @param msg the message to be sent
*/
synchronized void duplicateIdFound(String msg, String msg2)
{
if(sout == null)
return;
sendMessage("duplicate", sout);
sendMessage(msg, sout);
sendMessage(msg2, sout);
}
/**
* adds the client to the hashmap containing all the connections
* @param key the key of the hashmap
* @param out the print writer connection of the server to the client
* @param spot the parking spot of the corresponding display
*/
synchronized private void addDisplay(String key, PrintWriter out,
ParkingSpot spot)
{
if(!displayConnections.containsKey(key))
System.out.println( "Invalid spot number.");
else
{
displayConnections.put(key, out);
System.out.println( "Connected display to spot successfully.");
spot.setPrintWriter(out);
}
}
/**
* removes the display from the hashmap containing all the connections
* @param key the key of the hashmap
* @param spot the parking spot of the corresponding display
*/
synchronized private void removeDisplay(String key, ParkingSpot spot)
{
displayConnections.put(key, null);
spot.setPrintWriter(null);
}
/**
* accessor
* @param key the clients spot number
* @return iff the connection is still alive with the client
*/
public boolean isConnectionAvailable(String key)
{
return (displayConnections.get(key) == null);
}
/**
* private class that handles each connection on a separate thread.
*/
private class ConnectionHandler implements Runnable
{
/*
* constructor of the private class
*/
public ConnectionHandler(Socket sock)
{
theSocket = sock;
}
@Override
public void run()
{
PrintWriter pout = null;
String spotNumber = "";
try
{
InputStream in = theSocket.getInputStream();
OutputStream out = theSocket.getOutputStream();
Scanner scan = new Scanner(in);
pout = new PrintWriter(out, true);
spotNumber = scan.nextLine();
System.out.println(spotNumber);
if(spotNumber.equalsIgnoreCase("security"))
runSecurityDisplay(scan, pout);
else if(displayConnections.containsKey(spotNumber))
runParkingDisplay(scan, pout, spotNumber);
}
catch( IOException e )
{
System.out.println( "Socket error: " + e + " " +
theSocket.getRemoteSocketAddress( ) );
}
finally
{
//removes the display from the list
removeDisplay(spot.getParkingNumber(), spot);
try
{
if( theSocket != null )
theSocket.close( );
}
catch( IOException e )
{
System.out.println( "Socket error: " + e + " " +
theSocket.getRemoteSocketAddress( ) );
}
}
}
/*
* runs the thread for the security display client
*/
private void runSecurityDisplay(Scanner scan, PrintWriter pout)
{
if(sout != null)
{
sendMessage("another", pout);
return;
}
sout = pout;
sout.println("successful connection!");
while(theSocket.isConnected())
{
if(theSocket.isClosed())
break;
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
System.out.println(e);
}
sendStatus();
}
sout = null;
displayConnections.put("security", null);
}
/**
* runs the displays threads for each of the parking display clients
*/
private void runParkingDisplay(Scanner scan, PrintWriter pout,
String spotNumber)
{
spot = garage.searchBySpotNumber(spotNumber);
if(!isConnectionAvailable(spotNumber))
{
System.out.println("Another display is connected to spot #"
+ spotNumber);
sendMessage("another", pout);
return;
}
addDisplay(spotNumber, pout, spot);
sendMessage(spot.getParkingType(), pout);
boolean correct = true;
while(scan.hasNextLine())
{
String oneLine = scan.nextLine();
if(oneLine.equals("leave"))
{
if(correct)
spot.removeAssignedUser();
else
correct = true;
}
else if(oneLine.equals("wrong"))
{
wrongUserDetected("Wrong user detected on parking spot #"
+ spot.getParkingNumber());
if(spot.isAvailable())
{
ParkingUser user = new GuestUser();
spot.assignParkingSpot(user);
}
else
correct = false;
}
}
}
private ParkingSpot spot;
private Socket theSocket;
}
}
这些术语相当"模糊";没有在IT世界中普遍接受/传播的定义。
我会从术语本身中处理:
- A 子系统测试在包含许多较小子系统的大型系统中有意义。示例:在我的日常工作中,我正在研究一些"子系统" X,该X嵌入了更大的交付中。含义:X提供了独特的独特功能;它对其他子系统具有明显的界限。但是我们的客户购买了一个大型产品。因此,"我们的"子系统测试重点是测试X。 中的所有用户功能
- 从那里出发,集成测试 x确保x在我们向客户提供的大型系统交付的背景下工作。
您的代码示例包含一个 class 。可以肯定的是,对于"系统"或" Sub System"测试而言,这是。因此:您应该研究两种测试:
- 真正的单位测试:确保此类完成工作;隔离测试时;例如,通过使用模拟框架将此类别从其使用的非平凡组件中解脱出来。
- 集成/端到端测试:确保班级在客户使用的上下文中完成其工作。