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 17 package com.android.volley.utils; 18 19 import static org.hamcrest.Matchers.equalTo; 20 import static org.hamcrest.Matchers.is; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertThat; 23 24 import com.android.volley.Cache; 25 import java.util.Random; 26 27 public class CacheTestUtils { 28 29 /** 30 * Makes a random cache entry. 31 * 32 * @param data Data to use, or null to use random data 33 * @param isExpired Whether the TTLs should be set such that this entry is expired 34 * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh 35 */ makeRandomCacheEntry( byte[] data, boolean isExpired, boolean needsRefresh)36 public static Cache.Entry makeRandomCacheEntry( 37 byte[] data, boolean isExpired, boolean needsRefresh) { 38 Random random = new Random(); 39 Cache.Entry entry = new Cache.Entry(); 40 if (data != null) { 41 entry.data = data; 42 } else { 43 entry.data = new byte[random.nextInt(1024)]; 44 } 45 entry.etag = String.valueOf(random.nextLong()); 46 entry.lastModified = random.nextLong(); 47 entry.ttl = isExpired ? 0 : Long.MAX_VALUE; 48 entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE; 49 return entry; 50 } 51 52 /** 53 * Like {@link #makeRandomCacheEntry(byte[], boolean, boolean)} but defaults to an unexpired 54 * entry. 55 */ makeRandomCacheEntry(byte[] data)56 public static Cache.Entry makeRandomCacheEntry(byte[] data) { 57 return makeRandomCacheEntry(data, false, false); 58 } 59 assertThatEntriesAreEqual(Cache.Entry actual, Cache.Entry expected)60 public static void assertThatEntriesAreEqual(Cache.Entry actual, Cache.Entry expected) { 61 assertNotNull(actual); 62 assertThat(actual.data, is(equalTo(expected.data))); 63 assertThat(actual.etag, is(equalTo(expected.etag))); 64 assertThat(actual.lastModified, is(equalTo(expected.lastModified))); 65 assertThat(actual.responseHeaders, is(equalTo(expected.responseHeaders))); 66 assertThat(actual.serverDate, is(equalTo(expected.serverDate))); 67 assertThat(actual.softTtl, is(equalTo(expected.softTtl))); 68 assertThat(actual.ttl, is(equalTo(expected.ttl))); 69 } 70 randomData(int length)71 public static Cache.Entry randomData(int length) { 72 Cache.Entry entry = new Cache.Entry(); 73 byte[] data = new byte[length]; 74 new Random(42).nextBytes(data); // explicit seed for reproducible results 75 entry.data = data; 76 return entry; 77 } 78 getEntrySizeOnDisk(String key)79 public static int getEntrySizeOnDisk(String key) { 80 // Header size is: 81 // 4 bytes for magic int 82 // 8 + len(key) bytes for key (long length) 83 // 8 bytes for etag (long length + 0 characters) 84 // 32 bytes for serverDate, lastModified, ttl, and softTtl longs 85 // 4 bytes for length of header list int 86 // == 56 + len(key) bytes total. 87 return 56 + key.length(); 88 } 89 } 90