MimeTypeFilterTest.java revision 29395192292de3a4aa2609b8bc6c5ad59f5369bf
1/*
2 * Copyright (C) 2013 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
17package android.support.v4.content;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNull;
22import static org.junit.Assert.assertTrue;
23
24import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26import android.test.MoreAsserts;
27
28import junit.framework.Assert;
29
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33/**
34 * Tests for {@link MimeTypeFilter}
35 */
36@SmallTest
37@RunWith(AndroidJUnit4.class)
38public class MimeTypeFilterTest {
39    @Test
40    public void matchesBasic() throws Exception {
41        assertTrue(MimeTypeFilter.matches("image/jpeg", "*/*"));
42        assertTrue(MimeTypeFilter.matches("image/jpeg", "image/*"));
43        assertTrue(MimeTypeFilter.matches("image/jpeg", "image/jpeg"));
44        assertTrue(MimeTypeFilter.matches("image/jpeg", "*/jpeg"));
45
46        // These matchers are case *sensitive*.
47        assertFalse(MimeTypeFilter.matches("ImAgE/JpEg", "iMaGe/*"));
48        assertFalse(MimeTypeFilter.matches("IMAGE/JPEG", "image/jpeg"));
49        assertFalse(MimeTypeFilter.matches("image/jpeg", "IMAGE/JPEG"));
50
51        assertFalse(MimeTypeFilter.matches("image/jpeg", "image/png"));
52        assertFalse(MimeTypeFilter.matches("image/jpeg", "video/jpeg"));
53
54        assertFalse(MimeTypeFilter.matches((String) null, "*/*"));
55        assertFalse(MimeTypeFilter.matches((String) null, "image/"));
56        assertFalse(MimeTypeFilter.matches((String) null, "image/jpeg"));
57
58        // Null and invalid MIME types.
59        assertFalse(MimeTypeFilter.matches((String) null, "*/*"));
60        assertFalse(MimeTypeFilter.matches("", "*/*"));
61        assertFalse(MimeTypeFilter.matches("image/", "*/*"));
62        assertFalse(MimeTypeFilter.matches("*/", "*/*"));
63    }
64
65    @Test
66    public void matchesManyFilters() throws Exception {
67        assertEquals("*/*", MimeTypeFilter.matches("image/jpeg", new String[] {"*/*"}));
68        assertEquals("image/*", MimeTypeFilter.matches("image/jpeg", new String[] {"image/*"}));
69        assertEquals("image/jpeg", MimeTypeFilter.matches(
70                "image/jpeg", new String[] {"image/jpeg"}));
71
72        assertEquals("*/*", MimeTypeFilter.matches(
73                "image/jpeg", new String[] {"not/matching", "*/*"}));
74        assertEquals("image/*", MimeTypeFilter.matches(
75                "image/jpeg", new String[] {"image/*", "image/jpeg"}));
76        assertEquals("image/jpeg", MimeTypeFilter.matches(
77                "image/jpeg", new String[] {"image/jpeg", "image/png"}));
78
79        assertNull(MimeTypeFilter.matches(
80                "ImAgE/JpEg", new String[] {"iMaGe/*", "image/*"}));
81        assertEquals("*/jpeg", MimeTypeFilter.matches(
82                "image/jpeg", new String[] {"*/png", "*/jpeg"}));
83
84        assertNull(MimeTypeFilter.matches("image/jpeg", new String[] {}));
85
86        assertNull(MimeTypeFilter.matches("image/jpeg", new String[] {"image/png", "video/jpeg"}));
87        assertNull(MimeTypeFilter.matches("image/jpeg", new String[] {"video/jpeg", "image/png"}));
88
89        assertNull(MimeTypeFilter.matches(null, new String[] {"*/*"}));
90        assertNull(MimeTypeFilter.matches(null, new String[] {"image/"}));
91        assertNull(MimeTypeFilter.matches(null, new String[] {"image/jpeg"}));
92
93        // Null and invalid MIME types.
94        assertNull(MimeTypeFilter.matches((String) null, new String[] { "*/*" }));
95        assertNull(MimeTypeFilter.matches("", new String[] { "*/*" }));
96        assertNull(MimeTypeFilter.matches("image/", new String[] { "*/*" }));
97        assertNull(MimeTypeFilter.matches("*/", new String[] { "*/*" }));
98    }
99
100    @Test
101    public void matchesManyMimeTypes() throws Exception {
102        MoreAsserts.assertEquals(new String[] {"image/jpeg", "image/png"},
103                MimeTypeFilter.matchesMany(new String[] {"image/jpeg", "image/png"}, "image/*"));
104        MoreAsserts.assertEquals(new String[] {"image/png"},
105                MimeTypeFilter.matchesMany(new String[] {"image/jpeg", "image/png"}, "image/png"));
106        MoreAsserts.assertEquals(new String[] {},
107                MimeTypeFilter.matchesMany(new String[] {"image/jpeg", "image/png"}, "*/JpEg"));
108
109        MoreAsserts.assertEquals(new String[] {},
110                MimeTypeFilter.matchesMany(new String[] {"*/", "image/"}, "*/*"));
111        MoreAsserts.assertEquals(new String[] {},
112                MimeTypeFilter.matchesMany(new String[] {}, "*/*"));
113    }
114
115    @Test
116    public void illegalFilters() throws Exception {
117        try {
118            MimeTypeFilter.matches("image/jpeg", "");
119            Assert.fail("Illegal filter, should throw.");
120        } catch (IllegalArgumentException e) {
121            // Expected.
122        }
123
124        try {
125            MimeTypeFilter.matches("image/jpeg", "*");
126            Assert.fail("Illegal filter, should throw.");
127        } catch (IllegalArgumentException e) {
128            // Expected.
129        }
130
131        try {
132            MimeTypeFilter.matches("image/jpeg", "*/");
133            Assert.fail("Illegal filter, should throw.");
134        } catch (IllegalArgumentException e) {
135            // Expected.
136        }
137
138        try {
139            MimeTypeFilter.matches("image/jpeg", "/*");
140            Assert.fail("Illegal filter, should throw.");
141        } catch (IllegalArgumentException e) {
142            // Expected.
143        }
144
145        try {
146            MimeTypeFilter.matches("image/jpeg", "*/*/*");
147            Assert.fail("Illegal filter, should throw.");
148        } catch (IllegalArgumentException e) {
149            // Expected.
150        }
151
152        try {
153            MimeTypeFilter.matches(new String[] { "image/jpeg" }, "");
154            Assert.fail("Illegal filter, should throw.");
155        } catch (IllegalArgumentException e) {
156            // Expected.
157        }
158
159        try {
160            MimeTypeFilter.matches(new String[] { "image/jpeg" }, "*");
161            Assert.fail("Illegal filter, should throw.");
162        } catch (IllegalArgumentException e) {
163            // Expected.
164        }
165
166        try {
167            MimeTypeFilter.matches(new String[] { "image/jpeg" }, "*/");
168            Assert.fail("Illegal filter, should throw.");
169        } catch (IllegalArgumentException e) {
170            // Expected.
171        }
172
173        try {
174            MimeTypeFilter.matches(new String[] { "image/jpeg" }, "/*");
175            Assert.fail("Illegal filter, should throw.");
176        } catch (IllegalArgumentException e) {
177            // Expected.
178        }
179
180        try {
181            MimeTypeFilter.matches(new String[] { "image/jpeg" }, "*/*/*");
182            Assert.fail("Illegal filter, should throw.");
183        } catch (IllegalArgumentException e) {
184            // Expected.
185        }
186
187        try {
188            MimeTypeFilter.matches("image/jpeg", new String[] { "" });
189            Assert.fail("Illegal filter, should throw.");
190        } catch (IllegalArgumentException e) {
191            // Expected.
192        }
193
194        try {
195            MimeTypeFilter.matches("image/jpeg", new String[] { "*" });
196            Assert.fail("Illegal filter, should throw.");
197        } catch (IllegalArgumentException e) {
198            // Expected.
199        }
200
201        try {
202            MimeTypeFilter.matches("image/jpeg", new String[] { "*/" });
203            Assert.fail("Illegal filter, should throw.");
204        } catch (IllegalArgumentException e) {
205            // Expected.
206        }
207
208        try {
209            MimeTypeFilter.matches("image/jpeg", new String[] { "/*" });
210            Assert.fail("Illegal filter, should throw.");
211        } catch (IllegalArgumentException e) {
212            // Expected.
213        }
214
215        try {
216            MimeTypeFilter.matches("image/jpeg", new String[] { "*/*/*" });
217            Assert.fail("Illegal filter, should throw.");
218        } catch (IllegalArgumentException e) {
219            // Expected.
220        }
221
222        try {
223            MimeTypeFilter.matchesMany(new String[] { "image/jpeg" }, "");
224            Assert.fail("Illegal filter, should throw.");
225        } catch (IllegalArgumentException e) {
226            // Expected.
227        }
228
229        try {
230            MimeTypeFilter.matchesMany(new String[] { "image/jpeg" }, "*");
231            Assert.fail("Illegal filter, should throw.");
232        } catch (IllegalArgumentException e) {
233            // Expected.
234        }
235
236        try {
237            MimeTypeFilter.matchesMany(new String[] { "image/jpeg" }, "*/");
238            Assert.fail("Illegal filter, should throw.");
239        } catch (IllegalArgumentException e) {
240            // Expected.
241        }
242
243        try {
244            MimeTypeFilter.matchesMany(new String[] { "image/jpeg" }, "/*");
245            Assert.fail("Illegal filter, should throw.");
246        } catch (IllegalArgumentException e) {
247            // Expected.
248        }
249
250        try {
251            MimeTypeFilter.matchesMany(new String[] { "image/jpeg" }, "*/*/*");
252            Assert.fail("Illegal filter, should throw.");
253        } catch (IllegalArgumentException e) {
254            // Expected.
255        }
256    }
257}
258