FileDocCategorySizeDatePackage
ContentHandlerFactoryTest.javaAPI DocAndroid 1.5 API5681Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.tests.java.net

ContentHandlerFactoryTest

public class ContentHandlerFactoryTest extends TestCase

Fields Summary
ContentHandlerFactory
oldFactory
Field
factoryField
boolean
isTestable
boolean
isGetContentCalled
boolean
isCreateContentHandlerCalled
Constructors Summary
Methods Summary
public voidsetUp()

        Field [] fields = URLConnection.class.getDeclaredFields();
        int counter = 0;
        for (Field field : fields) {
            if (ContentHandlerFactory.class.equals(field.getType())) {
                counter++;
                factoryField = field;
            }
        } 
        
        if(counter == 1) {
            
            isTestable = true;
    
            factoryField.setAccessible(true);
            try {
                oldFactory = (ContentHandlerFactory) factoryField.get(null);
            } catch (IllegalArgumentException e) {
                fail("IllegalArgumentException was thrown during setUp: " 
                        + e.getMessage());
            } catch (IllegalAccessException e) {
                fail("IllegalAccessException was thrown during setUp: "
                        + e.getMessage());
            }        
        }        
    
public voidtearDown()

        if(isTestable) {
            try {
                factoryField.set(null, oldFactory);
            } catch (IllegalArgumentException e) {
                fail("IllegalArgumentException was thrown during tearDown: " 
                        + e.getMessage());
            } catch (IllegalAccessException e) {
                fail("IllegalAccessException was thrown during tearDown: "
                        + e.getMessage());
            }
        }        
    
public voidtest_createContentHandler()

        
        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) {
                fail("Exception during test : " + e.getMessage());
            
            }
            
            isGetContentCalled = false;
            
            try {
                con.getContent(new Class[] {});
                assertTrue(isGetContentCalled);
            } catch (Exception e) {
                fail("Exception during test : " + e.getMessage());
            
            }
            
            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.");
            }
        }