Home | History | Annotate | Download | only in utils
      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 com.android.volley.Cache;
     20 import java.util.Random;
     21 
     22 public class CacheTestUtils {
     23 
     24     /**
     25      * Makes a random cache entry.
     26      *
     27      * @param data Data to use, or null to use random data
     28      * @param isExpired Whether the TTLs should be set such that this entry is expired
     29      * @param needsRefresh Whether the TTLs should be set such that this entry needs refresh
     30      */
     31     public static Cache.Entry makeRandomCacheEntry(
     32             byte[] data, boolean isExpired, boolean needsRefresh) {
     33         Random random = new Random();
     34         Cache.Entry entry = new Cache.Entry();
     35         if (data != null) {
     36             entry.data = data;
     37         } else {
     38             entry.data = new byte[random.nextInt(1024)];
     39         }
     40         entry.etag = String.valueOf(random.nextLong());
     41         entry.lastModified = random.nextLong();
     42         entry.ttl = isExpired ? 0 : Long.MAX_VALUE;
     43         entry.softTtl = needsRefresh ? 0 : Long.MAX_VALUE;
     44         return entry;
     45     }
     46 
     47     /**
     48      * Like {@link #makeRandomCacheEntry(byte[], boolean, boolean)} but defaults to an unexpired
     49      * entry.
     50      */
     51     public static Cache.Entry makeRandomCacheEntry(byte[] data) {
     52         return makeRandomCacheEntry(data, false, false);
     53     }
     54 }
     55