FileDocCategorySizeDatePackage
URLStreamHandlerFactoryTest.javaAPI DocAndroid 1.5 API4306Wed May 06 22:41:04 BST 2009tests.api.java.net

URLStreamHandlerFactoryTest

public class URLStreamHandlerFactoryTest extends TestCase

Fields Summary
URLStreamHandlerFactory
oldFactory
Field
factoryField
boolean
isTestable
boolean
isOpenConnectionCalled
boolean
isCreateURLStreamHandlerCalled
Constructors Summary
Methods Summary
public voidsetUp()

        Field [] fields = URL.class.getDeclaredFields();
        int counter = 0;
        for (Field field : fields) {
            if (URLStreamHandlerFactory.class.equals(field.getType())) {
                counter++;
                factoryField = field;
            }
        } 
        
        if(counter == 1) {
            
            isTestable = true;
    
            factoryField.setAccessible(true);
            try {
                oldFactory = (URLStreamHandlerFactory) 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_createURLStreamHandler()

        
        if(isTestable) {
            
            TestURLStreamHandlerFactory shf = new TestURLStreamHandlerFactory();
            assertFalse(isCreateURLStreamHandlerCalled);
            URL.setURLStreamHandlerFactory(shf);
            URL url = new URL("http://" + 
                    Support_Configuration.SpecialInetTestAddress);

            try {
                url.openConnection();
                assertTrue(isCreateURLStreamHandlerCalled);
                assertTrue(isOpenConnectionCalled);
            } catch (Exception e) {
                fail("Exception during test : " + e.getMessage());
            
            }
            
            try {
                URL.setURLStreamHandlerFactory(shf);
                fail("java.lang.Error was not thrown.");                
            } catch(java.lang.Error e) {
                //expected
            }
            
            try {
                URL.setURLStreamHandlerFactory(null);
                fail("java.lang.Error was not thrown.");                
            } catch(java.lang.Error e) {
                //expected
            }
            
        } else { 
            TestURLStreamHandlerFactory shf = new TestURLStreamHandlerFactory();
            URLStreamHandler sh = shf.createURLStreamHandler("");
            assertNotNull(sh.toString());
        }