use of java.net.URLConnection in project robovm by robovm.
the class ContentHandlerFactoryTest method test_createContentHandler.
@SideEffect("This test affects tests that are run after this one." + " The reason are side effects due to caching in URLConnection." + " Maybe this test needs to be run in isolation.")
public void test_createContentHandler() throws IOException {
TestContentHandlerFactory factory = new TestContentHandlerFactory();
if (isTestable) {
assertFalse(isCreateContentHandlerCalled);
URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
URLConnection.setContentHandlerFactory(factory);
URLConnection con = url.openConnection();
try {
con.getContent();
assertTrue(isCreateContentHandlerCalled);
assertTrue(isGetContentCalled);
} catch (Exception e) {
throw new RuntimeException(e);
}
isGetContentCalled = false;
try {
con.getContent(new Class[] {});
assertTrue(isGetContentCalled);
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
con.setContentHandlerFactory(factory);
fail("java.lang.Error was not thrown.");
} catch (java.lang.Error e) {
//expected
}
try {
con.setContentHandlerFactory(null);
fail("java.lang.Error was not thrown.");
} catch (java.lang.Error e) {
//expected
}
} else {
ContentHandler ch = factory.createContentHandler("text/plain");
URL url;
try {
url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
assertNotNull(ch.getContent(url.openConnection()));
} catch (MalformedURLException e) {
fail("MalformedURLException was thrown: " + e.getMessage());
} catch (IOException e) {
fail("IOException was thrown.");
}
}
}
use of java.net.URLConnection in project robovm by robovm.
the class URLConnectionTest method test_getRequestProperties_Exception.
/**
* {@link java.net.URLConnection#getRequestProperties()}
*/
public void test_getRequestProperties_Exception() throws IOException {
URL url = new URL("http", "test", 80, "index.html", new NewHandler());
URLConnection urlCon = url.openConnection();
urlCon.connect();
try {
urlCon.getRequestProperties();
fail("should throw IllegalStateException");
} catch (IllegalStateException e) {
// expected
}
}
use of java.net.URLConnection in project robovm by robovm.
the class URLConnectionTest method test_guessContentTypeFromName.
/**
* {@link java.net.URLConnection#guessContentTypeFromName(String)}
*/
public void test_guessContentTypeFromName() throws IOException {
URLConnection htmlFileCon = openHTMLFile();
String[] expected = new String[] { "text/html", "text/plain" };
String[] resources = new String[] { htmlFileCon.getURL().toString(), fileURL.toString() };
for (int i = 0; i < resources.length; i++) {
String mime = URLConnection.guessContentTypeFromName(resources[i]);
assertEquals("checking " + resources[i] + " expected " + expected[i] + "got " + expected[i], expected[i], mime);
}
// Try simple case
try {
URLConnection.guessContentTypeFromStream(null);
fail("should throw NullPointerException");
} catch (NullPointerException e) {
// expected
}
}
use of java.net.URLConnection in project robovm by robovm.
the class URLConnectionTest method openHTMLFile.
private URLConnection openHTMLFile() throws IOException {
String cts = System.getProperty("java.io.tmpdir");
File tmpDir = new File(cts);
Support_Resources.copyFile(tmpDir, null, "hyts_htmltest.html");
URL fUrl1 = new URL("file:/" + tmpDir.getPath() + "/hyts_htmltest.html");
URLConnection con1 = fUrl1.openConnection();
return con1;
}
use of java.net.URLConnection in project robovm by robovm.
the class URLConnectionTest method test_setIfModifiedSinceJ.
/**
* {@link java.net.URLConnection#setIfModifiedSince(long)}
*/
public void test_setIfModifiedSinceJ() throws IOException {
URL url = new URL("http://localhost:8080/");
URLConnection connection = url.openConnection();
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
cal.clear();
cal.set(2000, Calendar.MARCH, 5);
long sinceTime = cal.getTime().getTime();
connection.setIfModifiedSince(sinceTime);
assertEquals("Wrong date set", sinceTime, connection.getIfModifiedSince());
// content should be returned
uc2.setIfModifiedSince(sinceTime);
uc2.connect();
assertEquals(200, ((HttpURLConnection) uc2).getResponseCode());
try {
uc2.setIfModifiedSince(2);
fail("Exception expected");
} catch (IllegalStateException e) {
//ok
}
((HttpURLConnection) uc2).disconnect();
}
Aggregations