Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 190 Lecture Notes: Exception Handling

Similar presentations


Presentation on theme: "CS 190 Lecture Notes: Exception Handling"— Presentation transcript:

1 CS 190 Lecture Notes: Exception Handling
Junit Test Example package stanford.cs190.tweeter; import org.junit.Test; public class JsonTest extends junit.framework.TestCase public void test_array() { Json document = new Json(); document.startArray(); document.addValue(12345); document.addValue(999); document.endArray(); assertEquals("[12345, 999]", document.toString()); } ... Do something Check result CS 190 Lecture Notes: Exception Handling

2 CS 190 Lecture Notes: Exception Handling
Test Isomorphism protected void readRequest(Reader reader) { try { BufferedReader in = new BufferedReader(reader); // Read the first line of input // (method, url, protocol version). String firstLine = in.readLine(); if (firstLine == null) { throw new RequestError("empty HTTP request"); } String[] words = firstLine.split(" +", 4); if (words.length < 3) { throw new RequestError("syntax error in " + “first line: '%s'", firstLine); method = words[0]; rawUrl = words[1]; protocolVersion = words[2]; // Read the headers. Each iteration through the // following loop reads one line, which contains a // single header. while (true) { String headerLine = in.readLine(); if (headerLine == null) { throw new RequestError( "unexpected EOF while reading " + "headers"); if (headerLine.length() == 0) { // Empty line marks the end of headers. break; int i = headerLine.indexOf(": "); if (i == -1) { + "header line: '%s'", headerLine); headers.put(headerLine.substring(0, i), headerLine.substring(i+2)); ... @Test public void test_readRequest_basics() { ... } public void test_readRequest_emptyInput() { public void test_readRequest_incompleteFirstLine() { public void test_readRequest_EofInHeaders() { public void test_readRequest_headerHasNoColon() { CS 190 Lecture Notes: Exception Handling

3 CS 190 Lecture Notes: Exception Handling
Helper Methods /** * Create a bunch of new tweets in the current repo. count * Number of tweets to create. */ protected void createTweets(int count) { ... } * Returns a string containing a sorted list of the * tweet ids for each of the TweetFiles in the * in-memory cache. protected String getCachedIds() { CS 190 Lecture Notes: Exception Handling

4 CS 190 Lecture Notes: Exception Handling
Helper Methods, cont’d @Test public void test_checkCacheSize_belowLimit() { createTweets(17); repo.cacheLimit = 3; repo.checkCacheSize(); assertEquals("10 20", getCachedIds()); } CS 190 Lecture Notes: Exception Handling


Download ppt "CS 190 Lecture Notes: Exception Handling"

Similar presentations


Ads by Google