From 88a220a0f0e8da4409b3faaf25e4a475337d5155 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C4=9Bj=20Cepl?= Date: Fri, 16 Apr 2010 17:41:37 +0200 Subject: [PATCH] Adding and completing tests with testurl. --- .classpath | 6 + .gitignore | 2 + src/eu/cepl/{util => network}/SimpleHTTP.java | 27 +-- src/eu/cepl/network/SimpleHTTPTest.java | 238 ++++++++++++++++++++++++++ src/eu/cepl/util/SimpleHTTPTest.java | 148 ---------------- test/Wave.svg | 26 +++ 6 files changed, 286 insertions(+), 161 deletions(-) rename src/eu/cepl/{util => network}/SimpleHTTP.java (92%) create mode 100644 src/eu/cepl/network/SimpleHTTPTest.java delete mode 100644 src/eu/cepl/util/SimpleHTTPTest.java create mode 100644 test/Wave.svg diff --git a/.classpath b/.classpath index 3e0fb27..12c2092 100644 --- a/.classpath +++ b/.classpath @@ -3,5 +3,11 @@ + + + + + + diff --git a/.gitignore b/.gitignore index 47366a0..ac024d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *~ *.class bin/ +docs/ +javadoc/ diff --git a/src/eu/cepl/util/SimpleHTTP.java b/src/eu/cepl/network/SimpleHTTP.java similarity index 92% rename from src/eu/cepl/util/SimpleHTTP.java rename to src/eu/cepl/network/SimpleHTTP.java index 86dafe7..4501c02 100644 --- a/src/eu/cepl/util/SimpleHTTP.java +++ b/src/eu/cepl/network/SimpleHTTP.java @@ -1,4 +1,4 @@ -package eu.cepl.util; +package eu.cepl.network; /* * TODO: @@ -11,7 +11,6 @@ import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; -import java.io.StringBufferInputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; @@ -132,7 +131,7 @@ public class SimpleHTTP { * @return * @throws IOException */ - public BufferedInputStream getReader(Map headers) + public BufferedInputStream getInputStream(Map headers) throws IOException { try { connection = getConnection(headers); @@ -154,10 +153,11 @@ public class SimpleHTTP { * @return String in UTF-8 encoding * @throws IOException */ - public String suck(BufferedInputStream in) throws IOException { - - StringBuffer inS = new StringBuffer(); + String suck(BufferedInputStream in) throws IOException { + StringBuffer inBuf = new StringBuffer(); InputStreamReader inR = null; + int count = 0; + try { inR = new InputStreamReader(in, "UTF-8"); } catch (UnsupportedEncodingException e) { @@ -165,11 +165,11 @@ public class SimpleHTTP { } char[] cbuf = new char[BUFMAX]; - while ((inR.read(cbuf)) != -1) { - inS.append(cbuf); + while ((count = inR.read(cbuf)) != -1) { + inBuf.append(cbuf, 0, count); } - return inS.toString(); + return inBuf.toString(); } /** @@ -186,7 +186,7 @@ public class SimpleHTTP { } try { - inCh = getReader(headers); + inCh = getInputStream(headers); } catch (IOException e) { throw new IOException("Cannot create Reader for URL " + url.toString(), e); @@ -206,9 +206,10 @@ public class SimpleHTTP { /** * - * @param data + * @param inStream * @param headers - * @return + * @param mime String with MIME type of data in {@literal inStream} + * @return BufferedInputStream with the data returned from the server * @throws IOException */ @SuppressWarnings("unused") @@ -249,7 +250,7 @@ public class SimpleHTTP { } try { - inCh = getReader(null); + inCh = getInputStream(null); } catch (IOException e) { throw new IOException("Cannot create Reader for URL " + url.toString(), e); diff --git a/src/eu/cepl/network/SimpleHTTPTest.java b/src/eu/cepl/network/SimpleHTTPTest.java new file mode 100644 index 0000000..76cfaf5 --- /dev/null +++ b/src/eu/cepl/network/SimpleHTTPTest.java @@ -0,0 +1,238 @@ +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; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.prefs.Preferences; + +import javax.naming.AuthenticationException; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.monkey.nelson.io.BrokenInputStream; +import org.monkey.nelson.testurl.TestURLRegistry; + +import java.nio.ByteBuffer; + +public class SimpleHTTPTest { + + final static private String GoogleLoginURL = "https://www.google.com/accounts/ClientLogin"; + final static private String feedURL = "http://www.blogger.com/feeds/default/blogs"; + final static private String testDataString = "Byl pozdní večer první máj!\n" + + "Нас было много на челне;\nИные парус напрягали,\nДругие дружно упирали\n" + + "В глубь мощны веслы. В тишине\nНа руль склонясь, наш кормщик умный\nВ " + + "молчаньи правил грузный чолн;\nА я — беспечной веры полн —\nПловцам я пел...."; + final static private String testFileName = "test/Wave.svg"; + final static private int MAX_BYTE_ARRAY = 100000; + + @Before + public void setupTestURLs() { + 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 + } + TestURLRegistry.register("machaPuskin", stringIS); + TestURLRegistry.register("dukeAI", imageIS); + + // Special inputStream which fails + // when read more than 10 bytes it will throw an IOException. + // saved for future testing, not used now + int bytesToRead = 10; + @SuppressWarnings("unused") + BrokenInputStream testStringIS = new BrokenInputStream(stringIS, + bytesToRead); + + } + + @Test + public final void virtualTestSimpleHTTPGet() throws IOException { + SimpleHTTP testURL = null; + try { + testURL = new SimpleHTTP(new URL("testurl:machaPuskin")); + } catch (MalformedURLException e) { + // URL is a constant string, so it should be always right. + } + assertNotNull(testURL); + String caughtString = testURL.get(); + assertEquals(testDataString, caughtString); + } + + @Test + public final void virtualTestSimpleHTTPGetBinary() throws IOException { + SimpleHTTP testURL = null; + + try { + testURL = new SimpleHTTP(new URL("testurl:dukeAI")); + } catch (MalformedURLException e) { + // URL is a constant string, so it should be always right. + } + 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. + } + int expectedReadCount = expectedIS.read(expectedByteArray); + expectedIS.close(); + + assertEquals(expectedReadCount, caughtReadCount); + + assertTrue(Arrays.equals(expectedByteArray, caughtByteArray)); + } + + @SuppressWarnings("null") + @Ignore + public final void yahooTestSimpleHTTPBufReader() throws IOException { + String outString = null; + + SimpleHTTP yahoo = new SimpleHTTP("http://www.yahoo.com/"); + BufferedReader in = new BufferedReader(new InputStreamReader(yahoo + .getInputStream(null), "UTF-8")); + + String inputLine; + + while ((inputLine = in.readLine()) != null) + outString += inputLine + "\n"; + + assertTrue(outString.length() > 0); + } + + @SuppressWarnings("null") + @Ignore + public final void yahooTestSimpleHTTPReader() throws IOException { + String outString = null; + String inCh = new String(); + + SimpleHTTP yahoo = new SimpleHTTP("http://www.yahoo.com/"); + BufferedReader in = new BufferedReader(new InputStreamReader(yahoo + .getInputStream(null), "UTF-8")); + + while ((inCh = in.readLine()) != null) + outString += inCh; + + assertTrue(outString.length() > 0); + } + + @Ignore + public final void yahooTestSimpleHTTPGet() throws IOException { + SimpleHTTP yahoo = null; + String content = ""; + + try { + yahoo = new SimpleHTTP("http://www.yahoo.com/"); + content = yahoo.get(); + System.err.println(content); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + System.err.println("Cannot close the connection to URL " + + yahoo.getUrl()); + e.printStackTrace(); + throw e; + } + + assertTrue(content.length() > 0); + } + + /** + * Get ClientLogin token from Google Blogger. + * + * @see {@link http + * ://code.google.com/apis/accounts/docs/AuthForInstalledApps.html} + * + * @return String ClientLogin token + * @throws IOException + */ + private String googleLogin(SimpleHTTP poster, Preferences prefs) + throws IOException { + String result = ""; + String logname = ""; + String passwd = ""; + + logname = prefs.get("login", null); + if (logname.length() == 0) { + throw new RuntimeException( + "Missing login in the system preferences"); + } + passwd = prefs.get("password", null); + + Map loginHeaders = new HashMap(); + loginHeaders.put("accountType", "HOSTED_OR_GOOGLE"); + loginHeaders.put("Email", logname); + loginHeaders.put("Passwd", passwd); + loginHeaders.put("service", "blogger"); + loginHeaders.put("source", "none-mcepl-simpleHTTPTest"); + + try { + result = poster.post(loginHeaders); + } catch (IOException e) { + System.err.println("Cannot run HTTP command to login into Google!"); + throw e; + } + + String[] tmpSplit; + String token = ""; + String[] splitted = result.split(System.getProperty("line.separator")); + + for (String spl : splitted) { + tmpSplit = spl.split("=", 2); + if (tmpSplit[0].equals("Auth")) { + token = tmpSplit[1]; + } + } + return token; + } + + @Ignore + public final void googleLoginTestSimpleHTTPPost() throws IOException, + AuthenticationException { + SimpleHTTP googleLogin = new SimpleHTTP(GoogleLoginURL); + Preferences prefs = Preferences.userRoot().node( + "eu.cepl.postBlog.PostBlog"); + + String token = googleLogin(googleLogin, prefs); + + if (token.length() == 0) { + throw new AuthenticationException("Not having Google token!"); + } + SimpleHTTP blogger = new SimpleHTTP(feedURL); + Map postHeaders = new HashMap(); + postHeaders.put("GData-Version", "2"); + postHeaders.put("Authorization", "GoogleLogin auth=" + token); + String response = blogger.get(postHeaders); + + System.err.println("response.length = " + response.length()); + + assertTrue(response.length() > 0); + } + +} \ No newline at end of file diff --git a/src/eu/cepl/util/SimpleHTTPTest.java b/src/eu/cepl/util/SimpleHTTPTest.java deleted file mode 100644 index fbfa164..0000000 --- a/src/eu/cepl/util/SimpleHTTPTest.java +++ /dev/null @@ -1,148 +0,0 @@ -package eu.cepl.util; - -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.util.HashMap; -import java.util.Map; -import java.util.prefs.Preferences; - -import javax.naming.AuthenticationException; - -import org.junit.Test; - -public class SimpleHTTPTest { - - final private String GoogleLoginURL = "https://www.google.com/accounts/ClientLogin"; - final private String feedURL = "http://www.blogger.com/feeds/default/blogs"; - - @SuppressWarnings("null") - @Test - public final void testSimpleHTTPBufReader() throws IOException { - String outString = null; - - SimpleHTTP yahoo = new SimpleHTTP("http://www.yahoo.com/"); - BufferedReader in = new BufferedReader(new InputStreamReader( - yahoo.getReader(null), "UTF-8")); - - String inputLine; - - while ((inputLine = in.readLine()) != null) - outString += inputLine + "\n"; - - assertTrue(outString.length() > 0); - } - - @SuppressWarnings("null") - @Test - public final void testSimpleHTTPReader() throws IOException { - String outString = null; - String inCh = new String(); - - SimpleHTTP yahoo = new SimpleHTTP("http://www.yahoo.com/"); - BufferedReader in = new BufferedReader(new InputStreamReader( - yahoo.getReader(null), "UTF-8")); - - while ((inCh = in.readLine()) != null) - outString += inCh; - - assertTrue(outString.length() > 0); - } - - @Test - public final void testSimpleHTTPGet() throws IOException { - SimpleHTTP yahoo = null; - String content = ""; - - try { - yahoo = new SimpleHTTP("http://www.yahoo.com/"); - content = yahoo.get(); - System.err.println(content); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (IOException e) { - System.err.println("Cannot close the connection to URL " - + yahoo.getUrl()); - e.printStackTrace(); - throw e; - } - - assertTrue(content.length() > 0); - } - - /** - * Get ClientLogin token from Google Blogger. - * - * @see {@link http - * ://code.google.com/apis/accounts/docs/AuthForInstalledApps.html} - * - * @return String ClientLogin token - * @throws IOException - */ - private String googleLogin(SimpleHTTP poster, Preferences prefs) - throws IOException { - String result = ""; - String logname = ""; - String passwd = ""; - - logname = prefs.get("login", null); - if (logname.length() == 0) { - throw new RuntimeException( - "Missing login in the system preferences"); - } - passwd = prefs.get("password", null); - - Map loginHeaders = new HashMap(); - loginHeaders.put("accountType", "HOSTED_OR_GOOGLE"); - loginHeaders.put("Email", logname); - loginHeaders.put("Passwd", passwd); - loginHeaders.put("service", "blogger"); - loginHeaders.put("source", "none-mcepl-simpleHTTPTest"); - - try { - result = poster.post(loginHeaders); - } catch (IOException e) { - System.err.println("Cannot run HTTP command to login into Google!"); - throw e; - } - - String[] tmpSplit; - String token = ""; - String[] splitted = result.split(System.getProperty("line.separator")); - - for (String spl : splitted) { - tmpSplit = spl.split("=", 2); - if (tmpSplit[0].equals("Auth")) { - token = tmpSplit[1]; - } - } - return token; - } - - @Test - public final void testSimpleHTTPPost() throws IOException, - AuthenticationException { - SimpleHTTP googleLogin = new SimpleHTTP(GoogleLoginURL); - Preferences prefs = Preferences.userRoot().node( - "eu.cepl.postBlog.PostBlog"); - - String token = googleLogin(googleLogin, prefs); - - if (token.length() == 0) { - throw new AuthenticationException("Not having Google token!"); - } - SimpleHTTP blogger = new SimpleHTTP(feedURL); - Map postHeaders = new HashMap(); - postHeaders.put("GData-Version", "2"); - postHeaders.put("Authorization", "GoogleLogin auth=" + token); - String response = blogger.get(postHeaders); - - System.err.println("response.length = " + response.length()); - - assertTrue(response.length() > 0); - } - -} \ No newline at end of file diff --git a/test/Wave.svg b/test/Wave.svg new file mode 100644 index 0000000..f3c66ec --- /dev/null +++ b/test/Wave.svg @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.11.4.GIT