1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.file.backends; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.junit.Assert.assertThrows; 20 21 import android.net.Uri; 22 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException; 23 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; 24 import java.io.File; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 28 /** Test {@link com.google.android.libraries.mobiledatadownload.file.backends.FileUriAdapter}. */ 29 @RunWith(GoogleRobolectricTestRunner.class) 30 public class FileUriAdapterTest { 31 @Test shouldGenerateFileFromUri()32 public void shouldGenerateFileFromUri() throws Exception { 33 File file = FileUriAdapter.instance().toFile(Uri.parse("file:///tmp/foo.txt")); 34 assertThat(file.getPath()).isEqualTo("/tmp/foo.txt"); 35 } 36 37 @Test ignoresFragmentAtCallersPeril()38 public void ignoresFragmentAtCallersPeril() throws Exception { 39 File file = FileUriAdapter.instance().toFile(Uri.parse("file:///tmp/foo.txt#fragment")); 40 assertThat(file.getPath()).isEqualTo("/tmp/foo.txt"); 41 } 42 43 @Test requiresFileScheme()44 public void requiresFileScheme() throws Exception { 45 assertThrows( 46 MalformedUriException.class, 47 () -> FileUriAdapter.instance().toFile(Uri.parse("xxx:///tmp/foo.txt"))); 48 } 49 50 @Test requiresEmptyQuery()51 public void requiresEmptyQuery() throws Exception { 52 assertThrows( 53 MalformedUriException.class, 54 () -> FileUriAdapter.instance().toFile(Uri.parse("file:///tmp/foo.txt?query"))); 55 } 56 57 @Test requiresEmptyAuthority()58 public void requiresEmptyAuthority() throws Exception { 59 assertThrows( 60 MalformedUriException.class, 61 () -> FileUriAdapter.instance().toFile(Uri.parse("file://authority/tmp/foo.txt"))); 62 } 63 } 64