Home | History | Annotate | Download | only in result
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.result;
     17 
     18 import junit.framework.TestCase;
     19 
     20 import java.io.ByteArrayInputStream;
     21 import java.io.File;
     22 import java.io.InputStream;
     23 
     24 /**
     25  * Unit tests for the {@link SnapshotInputStreamSource} class
     26  */
     27 public class SnapshotInputStreamSourceTest extends TestCase {
     28     private static final String FILE_CONTENTS = "These are file contents!";
     29     private InputStream mInputStream = null;
     30 
     31     @Override
     32     public void setUp() throws Exception {
     33         mInputStream = new ByteArrayInputStream(FILE_CONTENTS.getBytes());
     34     }
     35 
     36     /**
     37      * Ensure that the {@link SnapshotInputStreamSource#close()} method cleans up the backing file
     38      * as expected
     39      */
     40     @SuppressWarnings("serial")
     41     public void testCancel() {
     42         final String deletedMsg = "File was deleted";
     43         final File fakeFile = new File("noexist") {
     44             @Override
     45             public boolean delete() {
     46                 throw new RuntimeException(deletedMsg);
     47             }
     48         };
     49 
     50         InputStreamSource source =
     51                 new SnapshotInputStreamSource("SnapUnitTest", mInputStream) {
     52                     @Override
     53                     File createBackingFile(String name, InputStream stream) {
     54                         return fakeFile;
     55                     }
     56                 };
     57 
     58         try {
     59             source.close();
     60             fail("Fake file was not deleted");
     61         } catch (RuntimeException e) {
     62             if (!deletedMsg.equals(e.getMessage())) {
     63                 // This is not the droi^WRuntimeException you're searching for
     64                 throw e;
     65             }
     66         }
     67     }
     68 }
     69 
     70