0

我正在使用 UP Design 开发一个下载管理器。在这个细化迭代中,我的主要用例:下载文件。这里是 Download.java

public class Download  implements Runnable {

// Max size of download buffer.
private static final int MAX_BUFFER_SIZE = 1024;

// These are the status names.
public static final String STATUSES[] = {"Downloading", "Complete"};

// These are the status codes.
public static final int DOWNLOADING = 0;
public static final int COMPLETE = 1;
public static void main(String[] args) throws MalformedURLException {
    System.out.println("Welcome to Download Manager.");
    System.out.println("Enter a URL.");
    URL url;
    String s;
    Scanner scan= new Scanner(System.in);
    s=scan.nextLine();
    url= new URL(s);
    Download download=new Download(url);

}

private URL url; // download URL
private int size; // size of download in bytes
private int downloaded; // number of bytes downloaded
private int status; // current status of download

// Constructor for Download.
public Download(URL url) {
    this.url = url;
    size = -1;
    downloaded = 0;
    status = DOWNLOADING;

    // Begin the download.
    download();
}
 // Start or resume downloading.
private void download() {
    System.out.println("Starting.");
    Thread thread = new Thread(this);
    thread.start();
}
// Download file.
public void run() {
    RandomAccessFile file = null;
    InputStream stream = null;

    try {
        // Open connection to URL.
        HttpURLConnection connection =
                (HttpURLConnection) url.openConnection();

        // Specify what portion of file to download.
        connection.setRequestProperty("Range",
                "bytes=" + downloaded + "-");

        // Connect to server.
        connection.connect();


        int contentLength = connection.getContentLength();


  /* Set the size for this download if it
     hasn't been already set. */
        if (size == -1) {
            size = contentLength;

        }

        // Open file and seek to the end of it.
        file = new RandomAccessFile(getFileName(url), "rw");
        file.seek(downloaded);

        stream = connection.getInputStream();
        while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
       file is left to download. */
            byte buffer[];
            if (size - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }
            System.out.print("%"+(downloaded/size)+'\r');
            // Read from server into buffer.
            int read = stream.read(buffer);
            if (read == -1){
                System.out.println("File was downloaded");
                break;
            }

            // Write buffer to file.
            file.write(buffer, 0, read);
            downloaded += read;

        }

  /* Change status to complete if this point was
     reached because downloading has finished. */
        if (status == DOWNLOADING) {
            status = COMPLETE;

        }
    } catch (Exception e) {
       System.out.println("Error!");
    } finally {
        // Close file.
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {}
        }

        // Close connection to server.
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {}
        }
    }
}

我该如何编写此代码的测试代码?例如我应该测试URL验证,还是应该控制文件是否正在下载?我该如何进行这些测试?谢谢。

4

2 回答 2

1

您编写的课程很难测试,因为它试图做的太多了。如果您要将一些职责提取到外部依赖项中,您最终可能会得到一个类来管理从 URL 下载的低级下载,而另一个类来管理本地文件系统。就像是:

interface UrlDownloader {
    InputStream download(URL url, int offset) throws IOException;
}

interface DownloadFolder {
    List<String> getFiles();
    void writeToFile(String filename, InputStream contents) throws IOException;
    void getFileSize(String filename);
}

然后可以使用这些类的模拟版本来测试下载管理器。使用 mockito 之类的库,您可以编写如下测试:

@Test
public void canDownloadCompleteFile() throws IOException {
    URL url = new URL("http://example.com/file.txt");
    InputStream inputStream = new ByteArrayInputStream("abc".getBytes());
    UrlDownloader urlDownloader = mock(UrlDownloader.class);
    DownloadFolder downloadFolder = mock(DownloadFolder.class);
    when(urlDownloader.download(url, 0)).thenReturn(inputStream);

    DownloadManager manager = new DownloadManager(urlDownloader, downloadFolder);

    manager.download(url);

    verify(downloadFolder).writeToFile("file.txt", inputStream);
}

使用 mockito,您可以控制依赖项何时抛出异常,或验证使用特定参数调用方法。

另一种方法是创建假类来实现你的接口并使用内存中的数据结构而不是真正的文件系统/网络。然后可以使用 assertEquals 等测试这些类的状态:

@Test
public void canDownloadCompleteFile() throws IOException {
    URL url = new URL("http://example.com/file.txt");
    FakeDownloadFolder downloadFolder = new FakeDownloadFolder();
    FakeUrlDownloader urlDownloader = new FakeUrlDownloader();
    urlDownloader.setUrlContents(url, "abc".getBytes());

    DownloadManager manager = new DownloadManager(urlDownloader, downloadFolder);

    manager.download(url);

    assertEquals("abc".getBytes(), downloadFolder.getFileAsByteArray("file.txt"));
}

测试驱动开发背后的理念是您只编写由测试支持的代码。因此,您将实现足够的 DownloadManager 以通过第一个测试,然后添加另一个测试(例如,恢复不完整的下载)。

于 2013-04-06T12:27:05.623 回答
0

回答您的直接问题:为这些代码编写测试真的很难。

单元测试的美妙之处在于它们向您展示了您的代码的客户端可以多么容易地使用该模块。有一个名为“面向对象的分析与设计”的大领域旨在帮助程序员解决此类问题。

在这里,您应该更改您的代码,以便存在几个具有单一职责的例程,例如,一个用于通过网络进行通信,一个用于将自定义数据流存储到 HDD,另一个用于处理使用输入。这样,您可以单独测试这些“例程”,甚至提供您自己的“网络”环境(例如,将放置硬编码值而不是真正连接到网络的类)

于 2013-04-05T23:17:24.147 回答