aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/LazyInitialilzedListTest.java
blob: d1298383a564c45e72208d4407b31447d1779f8e (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
package cgeo.geocaching;

import cgeo.geocaching.utils.LazyInitializedList;

import android.test.AndroidTestCase;

import java.util.LinkedList;
import java.util.List;

public class LazyInitialilzedListTest extends AndroidTestCase {

    private static final int MAKE_NULL = -1;
    private static final int MAKE_EXCEPTION = -2;

    private static class MyList extends LazyInitializedList<Integer> {

        private int counter;

        MyList(int counter) {
            this.counter = counter;
        }

        @Override
        public List<Integer> call() {
            if (counter == MAKE_NULL) {
                return null;
            }
            if (counter == MAKE_EXCEPTION) {
                throw new RuntimeException("exception in call()");
            }
            final List<Integer> result = new LinkedList<Integer>();
            for (int i = 0; i < counter; i++) {
                result.add(counter);
            }
            counter += 1;
            return result;
        }

        int getCounter() {
            return counter;
        }

    }

    public static void testCallOnce() {
        final MyList l = new MyList(0);
        assertEquals("call() must not be called prematurely", 0, l.getCounter());
        l.size();
        assertEquals("call() must be called when needed", 1, l.getCounter());
        l.size();
        assertEquals("call() must be called only once", 1, l.getCounter());
    }

    public static void testSize() {
        final MyList l = new MyList(3);
        assertEquals("completed size must be identical to call() result", 3, l.size());
    }

    public static void testValue() {
        final MyList l = new MyList(1);
        assertEquals("value must be identical to call() result", Integer.valueOf(1), l.get(0));
    }

    public static void testNull() {
        final MyList l = new MyList(MAKE_NULL);
        assertEquals("null returned by call() must create an empty list", 0, l.size());
    }

    public static void testException() {
        final MyList l = new MyList(MAKE_EXCEPTION);
        assertEquals("exception in call() must create an empty list", 0, l.size());
    }

}