ContentHandlerFactoryTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1package org.apache.harmony.luni.tests.java.net; 2 3import dalvik.annotation.BrokenTest; 4import dalvik.annotation.TestTargets; 5import dalvik.annotation.TestLevel; 6import dalvik.annotation.TestTargetNew; 7import dalvik.annotation.TestTargetClass; 8 9import junit.framework.TestCase; 10 11import tests.support.Support_Configuration; 12 13import java.io.IOException; 14import java.lang.reflect.Field; 15import java.net.ContentHandler; 16import java.net.ContentHandlerFactory; 17import java.net.MalformedURLException; 18import java.net.URL; 19import java.net.URLConnection; 20 21@TestTargetClass(ContentHandlerFactory.class) 22public class ContentHandlerFactoryTest extends TestCase { 23 24 ContentHandlerFactory oldFactory = null; 25 Field factoryField = null; 26 27 boolean isTestable = false; 28 29 boolean isGetContentCalled = false; 30 boolean isCreateContentHandlerCalled = false; 31 32 @TestTargets ({ 33 @TestTargetNew( 34 level = TestLevel.COMPLETE, 35 notes = "", 36 method = "createContentHandler", 37 args = {java.lang.String.class} 38 ), 39 @TestTargetNew( 40 level = TestLevel.PARTIAL_COMPLETE, 41 notes = "Verifies positive case, and java.lang.Error.", 42 clazz = URLConnection.class, 43 method = "setContentHandlerFactory", 44 args = { ContentHandlerFactory.class } 45 ) 46 }) 47 @BrokenTest("This test affects tests that are run after this one." + 48 " The reason are side effects due to caching in URLConnection." + 49 " Maybe this test has to be marked NOT_FEASIBLE.") 50 public void test_createContentHandler() throws IOException { 51 52 TestContentHandlerFactory factory = new TestContentHandlerFactory(); 53 54 if(isTestable) { 55 56 assertFalse(isCreateContentHandlerCalled); 57 58 URL url = new URL("http://" + 59 Support_Configuration.SpecialInetTestAddress); 60 61 URLConnection.setContentHandlerFactory(factory); 62 63 URLConnection con = url.openConnection(); 64 65 try { 66 con.getContent(); 67 assertTrue(isCreateContentHandlerCalled); 68 assertTrue(isGetContentCalled); 69 } catch (Exception e) { 70 fail("Exception during test : " + e.getMessage()); 71 72 } 73 74 isGetContentCalled = false; 75 76 try { 77 con.getContent(new Class[] {}); 78 assertTrue(isGetContentCalled); 79 } catch (Exception e) { 80 fail("Exception during test : " + e.getMessage()); 81 82 } 83 84 try { 85 con.setContentHandlerFactory(factory); 86 fail("java.lang.Error was not thrown."); 87 } catch(java.lang.Error e) { 88 //expected 89 } 90 91 try { 92 con.setContentHandlerFactory(null); 93 fail("java.lang.Error was not thrown."); 94 } catch(java.lang.Error e) { 95 //expected 96 } 97 98 } else { 99 ContentHandler ch = factory.createContentHandler("text/plain"); 100 URL url; 101 try { 102 url = new URL("http://" + 103 Support_Configuration.SpecialInetTestAddress); 104 assertNotNull(ch.getContent(url.openConnection())); 105 } catch (MalformedURLException e) { 106 fail("MalformedURLException was thrown: " + e.getMessage()); 107 } catch (IOException e) { 108 fail("IOException was thrown."); 109 } 110 } 111 } 112 113 public void setUp() { 114 Field [] fields = URLConnection.class.getDeclaredFields(); 115 int counter = 0; 116 for (Field field : fields) { 117 if (ContentHandlerFactory.class.equals(field.getType())) { 118 counter++; 119 factoryField = field; 120 } 121 } 122 123 if(counter == 1) { 124 125 isTestable = true; 126 127 factoryField.setAccessible(true); 128 try { 129 oldFactory = (ContentHandlerFactory) factoryField.get(null); 130 } catch (IllegalArgumentException e) { 131 fail("IllegalArgumentException was thrown during setUp: " 132 + e.getMessage()); 133 } catch (IllegalAccessException e) { 134 fail("IllegalAccessException was thrown during setUp: " 135 + e.getMessage()); 136 } 137 } 138 } 139 140 public void tearDown() { 141 if(isTestable) { 142 try { 143 factoryField.set(null, oldFactory); 144 } catch (IllegalArgumentException e) { 145 fail("IllegalArgumentException was thrown during tearDown: " 146 + e.getMessage()); 147 } catch (IllegalAccessException e) { 148 fail("IllegalAccessException was thrown during tearDown: " 149 + e.getMessage()); 150 } 151 } 152 } 153 154 public class TestContentHandler extends ContentHandler { 155 156 public Object getContent(URLConnection u) { 157 isGetContentCalled = true; 158 return null; 159 } 160 } 161 162 public class TestContentHandlerFactory implements ContentHandlerFactory { 163 164 public ContentHandler createContentHandler(String mimetype) { 165 isCreateContentHandlerCalled = true; 166 return new TestContentHandler(); 167 } 168 } 169} 170