use of dalvik.annotation.SideEffect 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 dalvik.annotation.SideEffect in project robovm by robovm.
the class OldURLClassLoaderTest method test_findResourceLjava_lang_String.
@SideEffect("Support_TestWebServer requires isolation.")
public void test_findResourceLjava_lang_String() throws Exception {
File tmp = File.createTempFile("test", ".txt");
Support_TestWebServer server = new Support_TestWebServer();
try {
int port = server.initServer(tmp.getAbsolutePath(), "text/html");
URL[] urls = { new URL("http://localhost:" + port + "/") };
ucl = new URLClassLoader(urls);
URL res = ucl.findResource("test1");
assertNotNull("Failed to locate resource", res);
StringBuffer sb = getResContent(res);
assertEquals("Returned incorrect resource", new String(Support_TestWebData.test1), sb.toString());
} finally {
server.close();
}
}
use of dalvik.annotation.SideEffect in project robovm by robovm.
the class HttpURLConnectionTest method testProxyAuthorization.
@SideEffect("Suffers from side effect of other, currently unknown test")
public void testProxyAuthorization() throws Exception {
// Set up test Authenticator
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
try {
MockProxyServer proxy = new MockProxyServer("ProxyServer");
URL url = new URL("http://remotehost:55555/requested.data");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxy.port())));
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
proxy.start();
connection.connect();
assertEquals("unexpected response code", 200, connection.getResponseCode());
proxy.join();
assertTrue("Connection did not send proxy authorization request", proxy.acceptedAuthorizedRequest);
} finally {
// remove previously set authenticator
Authenticator.setDefault(null);
}
}
use of dalvik.annotation.SideEffect in project robovm by robovm.
the class SoftReferenceTest method test_get_SoftReference.
@SideEffect("Causes OutOfMemoryError to test finalization")
public void test_get_SoftReference() {
class TestObject {
public boolean finalized;
public TestObject() {
finalized = false;
}
protected void finalize() {
finalized = true;
}
}
final ReferenceQueue rq = new ReferenceQueue();
class TestThread extends Thread {
public void run() {
Object testObj = new TestObject();
r = new SoftReference(testObj, rq);
}
}
Reference ref;
try {
TestThread t = new TestThread();
t.start();
t.join();
Vector<StringBuffer> v = new Vector<StringBuffer>();
try {
while (true) {
v.add(new StringBuffer(10000));
}
} catch (OutOfMemoryError ofme) {
v = null;
}
} catch (InterruptedException e) {
fail("InterruptedException : " + e.getMessage());
}
assertNull("get() should return null " + "if OutOfMemoryError is thrown.", r.get());
try {
TestThread t = new TestThread();
t.start();
t.join();
FinalizationTester.induceFinalization();
ref = rq.poll();
assertNotNull("Object not garbage collected.", ref);
assertNull("Object is not null.", ref.get());
assertNotNull("Object could not be reclaimed.", r.get());
} catch (Exception e) {
fail("Exception : " + e.getMessage());
}
}
use of dalvik.annotation.SideEffect in project robovm by robovm.
the class X509CertificateTest method testVerifyPublicKey.
/**
* @throws SignatureException
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws CertificateException
* {@link Certificate#verify(PublicKey)}
*/
@SideEffect("Destroys MD5 provider, hurts succeeding tests")
public void testVerifyPublicKey() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, CertificateException {
// Preconditions
assertNotNull(javaxCert.getPublicKey());
assertNotNull(javaxSSCert.getPublicKey());
// here not self signed:
try {
javaxCert.verify(javaxCert.getPublicKey());
} catch (SignatureException e) {
// ok
}
PublicKey k = javaxCert.getPublicKey();
MyModifiablePublicKey changedEncoding = new MyModifiablePublicKey(k);
changedEncoding.setEncoding(new byte[javaxCert.getEncoded().length - 1]);
try {
javaxCert.verify(tbt_cert.getPublicKey());
} catch (InvalidKeyException e) {
// ok
}
try {
javaxCert.verify(null);
} catch (Exception e) {
// ok
}
try {
javaxCert.verify(changedEncoding);
fail("Exception expected");
} catch (Exception e) {
// ok
}
// following test doesn't work because the algorithm is derived from
// somewhere else.
// MyModifiablePublicKey changedAlgo = new MyModifiablePublicKey(k);
// changedAlgo.setAlgorithm("MD5withBla");
// try {
// javaxCert.verify(changedAlgo);
// fail("Exception expected");
// } catch (SignatureException e) {
// // ok
// }
// Security.removeProvider(mySSProvider.getName());
// try {
// javaxSSCert.verify(javaxSSCert.getPublicKey());
// } catch (NoSuchProviderException e) {
// // ok
// }
// Security.addProvider(mySSProvider);
// must always evaluate true for self signed
// javaxSSCert.verify(javaxSSCert.getPublicKey());
}
Aggregations