aboutsummaryrefslogtreecommitdiff
path: root/library/src/test/java/com/bumptech/glide/load/engine/cache/DiskLruCacheWrapperTest.java
blob: 8dcdbeefc94e7be7c728bf5cf26de617edf25f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.bumptech.glide.load.engine.cache;

import com.bumptech.glide.load.Key;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Arrays;

import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
public class DiskLruCacheWrapperTest {
    private File dir;
    private DiskLruCacheWrapper cache;
    private byte[] data;
    private StringKey key;

    @Before
    public void setUp() {
        dir = Robolectric.application.getCacheDir();
        cache = new DiskLruCacheWrapper(dir, 10 * 1024 * 1024);
        key = new StringKey("test");
        data = new byte[] { 1, 2, 3, 4, 5, 6 };
    }

    @Test
    public void testCanInsertAndGet() {
        cache.put(key, new DiskCache.Writer() {
            @Override
            public boolean write(OutputStream os) {
                try {
                    os.write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return true;
            }
        });

        byte[] received = isToBytes(cache.get(key), data.length);

        assertTrue(Arrays.equals(data, received));
    }

    @Test
    public void testDoesNotCommitIfWriterReturnsFalse() {
        cache.put(key, new DiskCache.Writer() {
            @Override
            public boolean write(OutputStream os) {
                return false;
            }
        });

        assertNull(cache.get(key));
    }

    @Test
    public void testDoesNotCommitIfWriterWritesButReturnsFalse() {
        cache.put(key, new DiskCache.Writer() {
            @Override
            public boolean write(OutputStream os) {
                try {
                    os.write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });

        assertNull(cache.get(key));
    }

    private static byte[] isToBytes(InputStream is, int length) {
        byte[] result = new byte[length];
        try {
            is.read(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    private static class StringKey implements Key {
        private final String key;

        public StringKey(String key) {
            this.key = key;
        }

        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) throws UnsupportedEncodingException {
            messageDigest.update(key.getBytes());
        }
    }
}