Home | History | Annotate | Download | only in res
      1 package com.xtremelabs.robolectric.res;
      2 
      3 import com.xtremelabs.robolectric.R;
      4 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
      5 import org.junit.Before;
      6 import org.junit.Test;
      7 import org.junit.runner.RunWith;
      8 
      9 import java.io.BufferedReader;
     10 import java.io.IOException;
     11 import java.io.InputStream;
     12 import java.io.InputStreamReader;
     13 import java.io.Reader;
     14 import java.io.StringWriter;
     15 import java.io.Writer;
     16 
     17 import static com.xtremelabs.robolectric.util.TestUtil.resourceFile;
     18 import static org.junit.Assert.assertEquals;
     19 
     20 @RunWith(WithTestDefaultsRunner.class)
     21 public class RawResourceLoaderTest {
     22 
     23     private RawResourceLoader rawResourceLoader;
     24 
     25     @Before public void setUp() throws Exception {
     26         ResourceExtractor resourceExtractor = new ResourceExtractor();
     27         resourceExtractor.addLocalRClass(R.class);
     28         rawResourceLoader = new RawResourceLoader(resourceExtractor, resourceFile("res"));
     29     }
     30 
     31     @Test
     32     public void shouldReturnRawResourcesWithExtensions() throws Exception {
     33         InputStream is = rawResourceLoader.getValue(R.raw.raw_resource);
     34         assertEquals("raw txt file contents", readString(is));
     35     }
     36 
     37     @Test
     38     public void shouldReturnRawResourcesWithoutExtensions() throws Exception {
     39         InputStream is = rawResourceLoader.getValue(R.raw.raw_no_ext);
     40         assertEquals("no ext file contents", readString(is));
     41     }
     42 
     43     private static String readString(InputStream is) throws IOException {
     44         Writer writer = new StringWriter();
     45         char[] buffer = new char[1024];
     46         try {
     47             Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
     48             int n;
     49             while ((n = reader.read(buffer)) != -1) {
     50                 writer.write(buffer, 0, n);
     51             }
     52         } finally {
     53             is.close();
     54         }
     55         return writer.toString();
     56     }
     57 }
     58