FileDescriptorTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.io; 19 20import java.io.BufferedOutputStream; 21import java.io.File; 22import java.io.FileDescriptor; 23import java.io.FileInputStream; 24import java.io.FileOutputStream; 25import java.io.RandomAccessFile; 26 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetClass; 29import dalvik.annotation.TestTargetNew; 30 31@TestTargetClass(FileDescriptor.class) 32public class FileDescriptorTest extends junit.framework.TestCase { 33 34 private static String platformId = "JDK" 35 + System.getProperty("java.vm.version").replace('.', '-'); 36 37 FileOutputStream fos; 38 39 BufferedOutputStream os; 40 41 FileInputStream fis; 42 43 File f; 44 45 /** 46 * @tests java.io.FileDescriptor#FileDescriptor() 47 */ 48 @TestTargetNew( 49 level = TestLevel.COMPLETE, 50 method = "FileDescriptor", 51 args = {} 52 ) 53 public void test_Constructor() { 54 // Test for method java.io.FileDescriptor() 55 FileDescriptor fd = new FileDescriptor(); 56 assertTrue("Failed to create FileDescriptor", 57 fd instanceof FileDescriptor); 58 } 59 60 /** 61 * @tests java.io.FileDescriptor#sync() 62 */ 63 @TestTargetNew( 64 level = TestLevel.SUFFICIENT, 65 notes = "SyncFailedException not checked since it is only thrown" + 66 "by the native implementation of sync().", 67 method = "sync", 68 args = {} 69 ) 70 public void test_sync() throws Exception { 71 // Test for method void java.io.FileDescriptor.sync() 72 f = File.createTempFile("fd" + platformId, ".tst"); 73 f.delete(); 74 fos = new FileOutputStream(f.getPath()); 75 fos.write("Test String".getBytes()); 76 fis = new FileInputStream(f.getPath()); 77 FileDescriptor fd = fos.getFD(); 78 fd.sync(); 79 int length = "Test String".length(); 80 assertEquals("Bytes were not written after sync", length, fis 81 .available()); 82 83 // Regression test for Harmony-1494 84 fd = fis.getFD(); 85 fd.sync(); 86 assertEquals("Bytes were not written after sync", length, fis 87 .available()); 88 89 RandomAccessFile raf = new RandomAccessFile(f, "r"); 90 fd = raf.getFD(); 91 fd.sync(); 92 raf.close(); 93 } 94 95 /** 96 * @tests java.io.FileDescriptor#valid() 97 */ 98 @TestTargetNew( 99 level = TestLevel.COMPLETE, 100 method = "valid", 101 args = {} 102 ) 103 public void test_valid() { 104 // Test for method boolean java.io.FileDescriptor.valid() 105 try { 106 f = new File(System.getProperty("java.io.tmpdir"), "fd.tst"); 107 f.delete(); 108 os = new BufferedOutputStream(fos = new FileOutputStream(f 109 .getPath()), 4096); 110 FileDescriptor fd = fos.getFD(); 111 assertTrue("Valid fd returned false", fd.valid()); 112 os.close(); 113 assertTrue("Invalid fd returned true", !fd.valid()); 114 } catch (Exception e) { 115 fail("Exception during test : " + e.getMessage()); 116 } 117 118 } 119 120 /** 121 * Sets up the fixture, for example, open a network connection. This method 122 * is called before a test is executed. 123 */ 124 protected void setUp() { 125 } 126 127 /** 128 * Tears down the fixture, for example, close a network connection. This 129 * method is called after a test is executed. 130 */ 131 protected void tearDown() { 132 try { 133 os.close(); 134 } catch (Exception e) { 135 } 136 try { 137 fis.close(); 138 } catch (Exception e) { 139 } 140 try { 141 fos.close(); 142 } catch (Exception e) { 143 } 144 try { 145 f.delete(); 146 } catch (Exception e) { 147 } 148 } 149 150 protected void doneSuite() { 151 } 152} 153