Accepting code review comments ... simple stuffmaster
authorMatěj Cepl <[email protected]>
Tue, 20 Apr 2010 08:39:03 +0000 (20 10:39 +0200)
committerMatěj Cepl <[email protected]>
Tue, 20 Apr 2010 08:39:03 +0000 (20 10:39 +0200)
src/eu/cepl/network/SimpleHTTPTest.java

index 4b869eb..98124db 100644 (file)
@@ -1,9 +1,5 @@
 package eu.cepl.network;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
@@ -22,6 +18,7 @@ import java.util.prefs.Preferences;
 
 import javax.naming.AuthenticationException;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.monkey.nelson.io.BrokenInputStream;
@@ -39,15 +36,11 @@ public class SimpleHTTPTest {
     private static final int MAX_BYTE_ARRAY = 100000;
 
     @Before
-    public void setupTestURLs() {
+    public void setupTestURLs() throws Exception {
         InputStream stringIS = new ByteArrayInputStream(testDataString
                 .getBytes());
         InputStream imageIS = null;
-        try {
-            imageIS = new FileInputStream(new File(testFileName));
-        } catch (FileNotFoundException e) {
-            // Should never happen, we have this file as part of the project
-        }
+        imageIS = new FileInputStream(new File(testFileName));
         TestURLRegistry.register("machaPuskin", stringIS);
         TestURLRegistry.register("dukeAI", imageIS);
 
@@ -64,9 +57,9 @@ public class SimpleHTTPTest {
     @Test
     public final void virtualTestSimpleHTTPGet() throws IOException {
         SimpleHTTP testURL = new SimpleHTTP(new URL("testurl:machaPuskin"));
-        assertNotNull(testURL);
+        Assert.assertNotNull(testURL);
         String caughtString = testURL.get();
-        assertEquals(testDataString, caughtString);
+        Assert.assertEquals(testDataString, caughtString);
     }
 
     @Test
@@ -74,27 +67,22 @@ public class SimpleHTTPTest {
         SimpleHTTP testURL = null;
 
         testURL = new SimpleHTTP(new URL("testurl:dukeAI"));
-        assertNotNull(testURL);
+        Assert.assertNotNull(testURL);
 
         byte[] caughtByteArray = new byte[MAX_BYTE_ARRAY];
         BufferedInputStream caughtIS = testURL.getInputStream(null);
         int caughtReadCount = caughtIS.read(caughtByteArray);
         caughtIS.close();
 
-        BufferedInputStream expectedIS = null;
         byte[] expectedByteArray = new byte[MAX_BYTE_ARRAY];
-        try {
-            expectedIS = new BufferedInputStream(new FileInputStream(
-                    testFileName));
-        } catch (FileNotFoundException e) {
-            // Should never happen, the file is part of the project.
-        }
+        BufferedInputStream expectedIS = new BufferedInputStream(
+                new FileInputStream(testFileName));
         int expectedReadCount = expectedIS.read(expectedByteArray);
         expectedIS.close();
 
-        assertEquals(expectedReadCount, caughtReadCount);
+        Assert.assertEquals(expectedReadCount, caughtReadCount);
 
-        assertTrue(Arrays.equals(expectedByteArray, caughtByteArray));
+        Assert.assertTrue(Arrays.equals(expectedByteArray, caughtByteArray));
     }
 
     @Test
@@ -108,7 +96,7 @@ public class SimpleHTTPTest {
         while ((inputLine = in.readLine()) != null)
             outString += inputLine + "\n";
 
-        assertTrue(outString.length() > 0);
+        Assert.assertTrue(outString.length() > 0);
     }
 
     @Test
@@ -122,13 +110,26 @@ public class SimpleHTTPTest {
         while ((inCh = in.readLine()) != null)
             outString += inCh;
 
-        assertTrue(outString.length() > 0);
+        Assert.assertTrue(outString.length() > 0);
     }
 
     @Test
     public final void yahooTestSimpleHTTPGet() throws IOException {
         String content = new SimpleHTTP("http://www.yahoo.com/").get();
-        assertTrue(content.length() > 0);
+        Assert.assertTrue(content.length() > 0);
+    }
+
+    @Test
+    public final void yahooTestCheckResponseCode() throws Exception {
+        SimpleHTTP req = new SimpleHTTP("http://www.ceplovi.cz/");
+        @SuppressWarnings("unused")
+        String content = req.get();
+        Assert.assertEquals(HttpURLConnection.HTTP_OK, req.getResponseCode());
+
+        req = new SimpleHTTP("http://www.ceplovi.cz/testFailureSimpleHTTP");
+        content = req.get();
+        Assert.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, req
+                .getResponseCode());
     }
 
     /**
@@ -144,7 +145,8 @@ public class SimpleHTTPTest {
             throws IOException {
         String logname = prefs.get("login", "");
         String passwd = prefs.get("password", "");
-        if (logname.length() == 0) {
+        if (logname.length() == 0) { // TODO testing of environment should be
+            // separated
             throw new RuntimeException(
                     "Missing login in the system preferences");
         }
@@ -156,18 +158,12 @@ public class SimpleHTTPTest {
         loginHeaders.put("service", "blogger");
         loginHeaders.put("source", "none-mcepl-simpleHTTPTest");
 
-        String result = "";
-        try {
-            result = poster.post(loginHeaders);
-        } catch (IOException e) {
-            System.err.println("Cannot run HTTP command to login into Google!");
-            throw e;
-        }
+        String result = poster.post(loginHeaders);
 
         int respCode = 0;
         if ((respCode = poster.getResponseCode()) != HttpURLConnection.HTTP_OK) {
-            throw new IOException("Login to Google failed (response code "
-                    + respCode + ")");
+            Assert.fail("Login to Google failed (response code " + respCode
+                    + ")");
         }
 
         String[] tmpSplit;
@@ -190,16 +186,14 @@ public class SimpleHTTPTest {
                 "eu.cepl.postBlog.PostBlog");
 
         String token = googleLogin(new SimpleHTTP(GoogleLoginURL), prefs);
+        Assert.assertTrue("Not having Google token!", token.length() > 0);
 
-        if (token.length() == 0) {
-            throw new AuthenticationException("Not having Google token!");
-        }
         Map<String, String> postHeaders = new HashMap<String, String>();
         postHeaders.put("GData-Version", "2");
         postHeaders.put("Authorization", "GoogleLogin auth=" + token);
         int respLen = new SimpleHTTP(feedURL).get(postHeaders).length();
 
-        assertTrue("response.length = " + respLen, respLen > 0);
+        Assert.assertTrue("response.length = " + respLen, respLen > 0);
     }
 
 }
\ No newline at end of file