Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2016 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 
     17 package com.android.tools.build.apkzlib.utils;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.assertTrue;
     23 
     24 import com.google.common.base.Charsets;
     25 import com.google.common.io.Files;
     26 import java.io.File;
     27 import org.junit.Rule;
     28 import org.junit.Test;
     29 import org.junit.rules.TemporaryFolder;
     30 
     31 public class CachedFileContentsTest {
     32     @Rule
     33     public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
     34 
     35     @Test
     36     public void createFileAndCheckWithNoChanges() throws Exception {
     37         File f = mTemporaryFolder.newFile("test");
     38         Files.write("abc", f, Charsets.US_ASCII);
     39 
     40         Object cache = new Object();
     41 
     42         CachedFileContents<Object> cachedFile = new CachedFileContents<>(f);
     43         cachedFile.closed(cache);
     44 
     45         assertTrue(cachedFile.isValid());
     46         assertSame(cache, cachedFile.getCache());
     47     }
     48 
     49     @Test
     50     public void createFileAndCheckChanges() throws Exception {
     51         File f = mTemporaryFolder.newFile("test");
     52         Files.write("abc", f, Charsets.US_ASCII);
     53 
     54         Object cache = new Object();
     55 
     56         CachedFileContents<Object> cachedFile = new CachedFileContents<>(f);
     57         cachedFile.closed(cache);
     58 
     59         Files.write("def", f, Charsets.US_ASCII);
     60 
     61         assertFalse(cachedFile.isValid());
     62         assertNull(cachedFile.getCache());
     63     }
     64 
     65     @Test
     66     public void createFileUpdateAndCheckChanges() throws Exception {
     67         File f = mTemporaryFolder.newFile("test");
     68         Files.write("abc", f, Charsets.US_ASCII);
     69 
     70         Object cache = new Object();
     71 
     72         CachedFileContents<Object> cachedFile = new CachedFileContents<>(f);
     73         cachedFile.closed(cache);
     74 
     75         Files.write("def", f, Charsets.US_ASCII);
     76         cachedFile.closed(cache);
     77 
     78         assertTrue(cachedFile.isValid());
     79         assertSame(cache, cachedFile.getCache());
     80     }
     81 
     82     @Test
     83     public void immediateChangesDetected() throws Exception {
     84         File f = mTemporaryFolder.newFile("foo");
     85         Files.write("bar", f, Charsets.US_ASCII);
     86 
     87         CachedFileContents<Object> cachedFile = new CachedFileContents<>(f);
     88         cachedFile.closed(null);
     89 
     90         Files.write("xpto", f, Charsets.US_ASCII);
     91         assertFalse(cachedFile.isValid());
     92     }
     93 
     94     @Test
     95     public void immediateChangesDetectedEvenWithHackedTs() throws Exception {
     96         File f = mTemporaryFolder.newFile("foo");
     97         Files.write("bar", f, Charsets.US_ASCII);
     98 
     99         CachedFileContents<Object> cachedFile = new CachedFileContents<>(f);
    100         cachedFile.closed(null);
    101         long lastTs = f.lastModified();
    102 
    103         Files.write("xpto", f, Charsets.US_ASCII);
    104         f.setLastModified(lastTs);
    105         assertFalse(cachedFile.isValid());
    106     }
    107 
    108     @Test
    109     public void immediateChangesWithNoContentChangeNotDetected() throws Exception {
    110         File f = mTemporaryFolder.newFile("foo");
    111         Files.write("bar", f, Charsets.US_ASCII);
    112 
    113         CachedFileContents<Object> cachedFile = new CachedFileContents<>(f);
    114         cachedFile.closed(null);
    115         long lastTs = f.lastModified();
    116 
    117         Files.write("bar", f, Charsets.US_ASCII);
    118         f.setLastModified(lastTs);
    119         assertTrue(cachedFile.isValid());
    120     }
    121 }
    122