FileDocCategorySizeDatePackage
JavaIoRandomAccessFileTest.javaAPI DocAndroid 1.5 API5799Wed May 06 22:41:06 BST 2009tests.security.permissions

JavaIoRandomAccessFileTest

public class JavaIoRandomAccessFileTest extends TestCase

Fields Summary
SecurityManager
old
Constructors Summary
Methods Summary
protected voidsetUp()

        old = System.getSecurityManager();
        super.setUp();
    
protected voidtearDown()

        System.setSecurityManager(old);
        super.tearDown();
    
public voidtest_RandomAccessFile1()

        class TestSecurityManager extends SecurityManager {
            boolean called;
            String file;
            void reset(){
                called = false;
                file = null;
            }
            @Override
            public void checkRead(String file){
                called = true;
                this.file = file;
                super.checkRead(file);
            }
            @Override
            public void checkPermission(Permission p) {
                
            }
        }

        long id = new java.util.Date().getTime();
        String filename = "SecurityPermissionsTest_"+id;
        File f = File.createTempFile(filename, null);
        f.deleteOnExit();
        filename = f.getCanonicalPath();
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();
        new RandomAccessFile(filename, "r");
        assertTrue("RandomAccessFile(String,String) ctor must call checkRead on security manager", s.called);
        assertEquals("Argument of checkRead is not correct", filename, s.file);
        
        s.reset();
        new RandomAccessFile(f, "r");
        assertTrue("RandomAccessFile(File, String) ctor must call checkRead on security manager", s.called);
        assertEquals("Argument of checkRead is not correct", filename, s.file);
    
public voidtest_RandomAccessFile2()

        class TestSecurityManager extends SecurityManager {
            boolean checkReadCalled;
            boolean checkWriteCalled;
            String checkReadFile;
            String checkWriteFile;
            
            void reset(){
                checkReadCalled = false;
                checkWriteCalled = false;
                checkReadFile = null;
                checkWriteFile = null;
            }
            
            @Override
            public void checkRead(String file) {
                checkReadCalled = true;
                this.checkReadFile = file;
                super.checkRead(file);
            }
            @Override
            public void checkWrite(String file) {
                checkWriteCalled = true;
                this.checkWriteFile = file;
                super.checkWrite(file);
            }
            @Override
            public void checkPermission(Permission p) {
                
            }
        }

        long id = new java.util.Date().getTime();
        String filename = "SecurityPermissionsTest_"+id;
        File f = File.createTempFile(filename, null);
        f.deleteOnExit();
        filename = f.getCanonicalPath();
        
        TestSecurityManager s = new TestSecurityManager();
        System.setSecurityManager(s);
        
        s.reset();
        new RandomAccessFile(filename, "rw");
        assertTrue("RandomAccessFile(String,String) ctor must call checkRead on security manager", s.checkReadCalled);
        assertTrue("RandomAccessFile(String,String) ctor must call checkWrite on security manager", s.checkWriteCalled);
        assertEquals("Argument of checkRead is not correct", filename, s.checkReadFile);
        assertEquals("Argument of checkWrite is not correct", filename, s.checkWriteFile);