如何为下载管理器编写测试驱动的java类?

时间:2013-04-05 22:36:01

标签: java url testing tdd download-manager

我正在使用UP Design开发一个下载管理器。在这个Elaboration迭代中,我的主要用例:下载文件。这是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验证,还是应该控制文件是否正在下载?我该如何进行这些测试? 感谢。

2 个答案:

答案 0 :(得分: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以通过第一个测试,然后添加另一个测试(例如,恢复不完整的下载)。

答案 1 :(得分:0)

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

单元测试的优点在于它们向您展示了代码客户端使用模块的难易程度。有一个名为“面向对象的分析和设计”的大型领域,旨在帮助程序员解决这些问题。

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