aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/ant/types/resources/LazyResourceCollectionTest.java
blob: f91077a6e38cd053003d457c20a60c788621ef4c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.types.resources;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;


import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class LazyResourceCollectionTest {

    private class StringResourceCollection implements ResourceCollection {
        List resources = Arrays.<Resource>asList();

        List createdIterators = new ArrayList();

        public int size() {
            return resources.size();
        }

        public Iterator<Resource> iterator() {
            StringResourceIterator it = new StringResourceIterator();
            createdIterators.add(it);
            return it;
        }

        public boolean isFilesystemOnly() {
            return false;
        }
    }

    private class StringResourceIterator implements Iterator {
        int cursor = 0;

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public Object next() {
            if (cursor < 3) {
                cursor++;
                return new StringResource("r" + cursor);
            }
            return null;
        }

        public boolean hasNext() {
            return cursor < 3;
        }
    }

    @Test
    public void testLazyLoading() throws Exception {
        StringResourceCollection collectionTest = new StringResourceCollection();
        LazyResourceCollectionWrapper lazyCollection = new LazyResourceCollectionWrapper();
        lazyCollection.add(collectionTest);

        Iterator<Resource> it = lazyCollection.iterator();
        assertOneCreatedIterator(collectionTest);
        StringResourceIterator stringResourceIterator = (StringResourceIterator) collectionTest.createdIterators
                .get(0);
        assertEquals("A resource was loaded without iterating", 1,
                stringResourceIterator.cursor);

        StringResource r = (StringResource) it.next();
        assertOneCreatedIterator(collectionTest);
        assertEquals("r1", r.getValue());
        assertEquals("Iterating once load more than 1 resource", 2,
                stringResourceIterator.cursor);

        r = (StringResource) it.next();
        assertOneCreatedIterator(collectionTest);
        assertEquals("r2", r.getValue());
        assertEquals("Iterating twice load more than 2 resources", 3,
                stringResourceIterator.cursor);

        r = (StringResource) it.next();
        assertOneCreatedIterator(collectionTest);
        assertEquals("r3", r.getValue());
        assertEquals("Iterating 3 times load more than 3 resources", 3,
                stringResourceIterator.cursor);

        try {
            it.next();
            fail("NoSuchElementException should have been raised");
        } catch (NoSuchElementException e) {
            // ok
        }
    }

    private void assertOneCreatedIterator(
            StringResourceCollection testCollection) {
        assertEquals("More than one iterator has been created", 1,
                testCollection.createdIterators.size());
    }

    @Test
    public void testCaching() throws Exception {
        StringResourceCollection collectionTest = new StringResourceCollection();
        LazyResourceCollectionWrapper lazyCollection = new LazyResourceCollectionWrapper();
        lazyCollection.add(collectionTest);

        assertTrue(lazyCollection.isCache());
        Iterator<Resource> it1 = lazyCollection.iterator();
        assertOneCreatedIterator(collectionTest);
        Iterator<Resource> it2 = lazyCollection.iterator();
        assertOneCreatedIterator(collectionTest);

        StringResourceIterator stringResourceIterator = (StringResourceIterator) collectionTest.createdIterators
                .get(0);
        assertEquals("A resource was loaded without iterating", 1,
                stringResourceIterator.cursor);

        StringResource r = (StringResource) it1.next();
        assertEquals("r1", r.getValue());
        assertEquals("Iterating once load more than 1 resource", 2,
                stringResourceIterator.cursor);

        r = (StringResource) it2.next();
        assertEquals("r1", r.getValue());
        assertEquals(
                "The second iterator did not lookup in the cache for a resource",
                2, stringResourceIterator.cursor);

        r = (StringResource) it2.next();
        assertEquals("r2", r.getValue());
        assertEquals("Iterating twice load more than 2 resources", 3,
                stringResourceIterator.cursor);

        r = (StringResource) it1.next();
        assertEquals("r2", r.getValue());
        assertEquals(
                "The first iterator did not lookup in the cache for a resource",
                3, stringResourceIterator.cursor);

        r = (StringResource) it2.next();
        assertEquals("r3", r.getValue());
        assertEquals("Iterating 3 times load more than 3 resources", 3,
                stringResourceIterator.cursor);

        r = (StringResource) it1.next();
        assertEquals("r3", r.getValue());
        assertEquals(
                "The first iterator did not lookup in the cache for a resource",
                3, stringResourceIterator.cursor);

        try {
            it1.next();
            fail("NoSuchElementException should have been raised");
        } catch (NoSuchElementException e) {
            // ok
        }

        try {
            it2.next();
            fail("NoSuchElementException should have been raised");
        } catch (NoSuchElementException e) {
            // ok
        }
    }
}