Junit 什么是软件测试的子系统测试
子系统测试到底是什么?它与集成测试有什么不同吗?子系统测试是否只是测试当参数传递给方法时方法是否有效 对于这段代码,什么是好的子系统测试Junit 什么是软件测试的子系统测试,junit,mocking,mockito,Junit,Mocking,Mockito,子系统测试到底是什么?它与集成测试有什么不同吗?子系统测试是否只是测试当参数传递给方法时方法是否有效 对于这段代码,什么是好的子系统测试 package server.controller; import server.storage.ParkedUsers; import java.net.ServerSocket; import java.net.Socket; import java.io.IOException; import java.io.InputStream; import ja
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;
}
}
package server.controller;
导入server.storage.ParkedUsers;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.io.PrintWriter;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Scanner;
导入java.util.TreeSet;
导入server.storage.ParkingSpot;
/**
*这是控制所有数据访问和流的服务器类。
*它还负责创建所有停车用户对象
*一旦它在金融情报室数据库中搜索它们。
*/
公共类AccessControlServer扩展线程
{
private static ParkedUsers garage=ParkedUsers.instance(“garage.txt”);
私有最终int端口号;
私有PrintWriter sout=null;
私有HashMap显示连接
=新HashMap();
/**
*初始化端口号变量的构造函数
*@param p包含端口号的整数
*此服务器将侦听的
*/
公共访问控制服务器(INTP)
{
portNum=p;
mapConnections();
}
/**
*将文本文件映射到hashmap的helper方法
*/
私有void mapConnections()
{
sout=null;
displayConnections.put(“安全”,sout);
对于(树集:garage.values())
用于(停车点p:t)
{
displayConnections.put(p.getParkingNumber(),
p、 getPrintWriter());
}
}
@凌驾
公开募捐
{
尝试
{
startServer();
}
捕获(IOE异常)
{
System.out.println(“无法侦听端口”+portNum);
}
}
/**
*使用指定的端口号初始化服务器,并
*开始侦听连接
*@throws IOException出现问题时引发异常
*使用服务器
*/
public void startServer()引发IOException
{
ServerSocket ss=新的ServerSocket(portNum);
while(true)
{
套接字s=ss.accept();
线程t=新线程(新连接处理程序);
t、 start();
}
}
/**
*方法向客户端发送消息
*@param msg向指定的客户端发送字符串消息
*@param将打印写入程序连接到客户端
*/
public void sendMessage(字符串消息,PrintWriter pout)
{
味精;
}
/**
*向安全显示器发送所有停车位的状态
*/
公共状态()
{
ArrayList stat=garage.getStatus();
对于(int i=0;i