1 /******************************************************************************* 2 * Copyright (c) 2005, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.core.tests.filesystem; 15 16 import java.io.*; 17 import java.util.Arrays; 18 import org.eclipse.core.filesystem.EFS; 19 import org.eclipse.core.filesystem.IFileStore; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.tests.internal.filesystem.ram.MemoryFileStore; 23 import org.eclipse.core.tests.internal.filesystem.ram.MemoryTree; 24 25 /** 26 * Tests the file caching provided by FileStore.toLocalFile. 27 */ 28 public class FileCacheTest extends FileSystemTest { 29 30 /** 31 * Overrides generic method from Assert to perform proper array equality test. 32 */ assertEquals(String message, byte[] expected, byte[] actual)33 public void assertEquals(String message, byte[] expected, byte[] actual) { 34 if (expected.length != actual.length) { 35 fail(message + " arrays of different length"); 36 } 37 assertEquals(message + " different length", expected.length, actual.length); 38 for (int i = 0; i < actual.length; i++) { 39 if (expected[i] != actual[i]) { 40 fail(message + " arrays differ at position " + i + "; expected: " + expected[i] + " but was: " + actual[i]); 41 } 42 } 43 } 44 45 /** 46 * Overrides generic method from Assert to perform proper array equality test. 47 */ assertNotSame(String message, byte[] expected, byte[] actual)48 public void assertNotSame(String message, byte[] expected, byte[] actual) { 49 if (expected.length != actual.length) { 50 return; 51 } 52 for (int i = 0; i < actual.length; i++) { 53 if (expected[i] != actual[i]) { 54 return; 55 } 56 } 57 fail(message + " arrays should be different, but they are not: " + Arrays.toString(expected)); 58 } 59 60 /** 61 * Returns the byte[] contents of the given file. 62 */ getBytes(File cachedFile)63 private byte[] getBytes(File cachedFile) { 64 FileInputStream in = null; 65 ByteArrayOutputStream out = null; 66 try { 67 in = new FileInputStream(cachedFile); 68 out = new ByteArrayOutputStream(); 69 transferData(in, out); 70 in.close(); 71 out.close(); 72 return out.toByteArray(); 73 } catch (IOException e) { 74 fail("Exception in FileCacheTest.getBytes", e); 75 } 76 return new byte[0]; 77 } 78 79 @Override setUp()80 protected void setUp() throws Exception { 81 super.setUp(); 82 MemoryTree.TREE.deleteAll(); 83 } 84 85 @Override tearDown()86 protected void tearDown() throws Exception { 87 super.tearDown(); 88 MemoryTree.TREE.deleteAll(); 89 } 90 testCacheFile()91 public void testCacheFile() { 92 try { 93 IFileStore store = new MemoryFileStore(new Path("testCacheFile")); 94 OutputStream out = store.openOutputStream(EFS.NONE, getMonitor()); 95 byte[] contents = "test".getBytes(); 96 out.write(contents); 97 out.close(); 98 File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor()); 99 assertTrue("1.0", cachedFile.exists()); 100 assertTrue("1.1", !cachedFile.isDirectory()); 101 assertEquals("1.2", contents, getBytes(cachedFile)); 102 103 //write out new file contents 104 byte[] newContents = "newContents".getBytes(); 105 out = store.openOutputStream(EFS.NONE, getMonitor()); 106 out.write(newContents); 107 out.close(); 108 109 //old cache will be out of date 110 assertNotSame("2.0", newContents, getBytes(cachedFile)); 111 112 //fetching the cache again should return up to date file 113 cachedFile = store.toLocalFile(EFS.CACHE, getMonitor()); 114 assertTrue("3.0", cachedFile.exists()); 115 assertTrue("3.1", !cachedFile.isDirectory()); 116 assertEquals("3.2", newContents, getBytes(cachedFile)); 117 118 } catch (IOException | CoreException e) { 119 fail("1.99", e); 120 } 121 } 122 testCacheFolder()123 public void testCacheFolder() { 124 try { 125 IFileStore store = new MemoryFileStore(new Path("testCacheFolder")); 126 store.mkdir(EFS.NONE, getMonitor()); 127 File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor()); 128 assertTrue("1.0", cachedFile.exists()); 129 assertTrue("1.1", cachedFile.isDirectory()); 130 } catch (CoreException e) { 131 fail("1.99", e); 132 } 133 } 134 135 /** 136 * Tests invoking the toLocalFile method without the CACHE option flag. 137 */ testNoCacheFlag()138 public void testNoCacheFlag() { 139 try { 140 IFileStore store = new MemoryFileStore(new Path("testNoCacheFlag")); 141 store.mkdir(EFS.NONE, getMonitor()); 142 File cachedFile = store.toLocalFile(EFS.NONE, getMonitor()); 143 assertNull("1.0", cachedFile); 144 } catch (CoreException e) { 145 fail("4.99", e); 146 } 147 } 148 149 /** 150 * Tests caching a non-existing file 151 */ testNonExisting()152 public void testNonExisting() { 153 try { 154 IFileStore store = new MemoryFileStore(new Path("testNonExisting")); 155 File cachedFile = store.toLocalFile(EFS.CACHE, getMonitor()); 156 assertTrue("1.0", !cachedFile.exists()); 157 } catch (CoreException e) { 158 fail("4.99", e); 159 } 160 } 161 } 162