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 org.junit.Test; 25 import org.junit.runner.RunWith; 26 27 @RunWith(GoogleRobolectricTestRunner.class) 28 public class MemoryUriTest { 29 30 @Test builder_withoutKey_throwsException()31 public void builder_withoutKey_throwsException() throws Exception { 32 assertThrows(MalformedUriException.class, () -> MemoryUri.builder().build()); 33 } 34 35 @Test builder_setKey_setsSchemeSpecificPart()36 public void builder_setKey_setsSchemeSpecificPart() throws Exception { 37 Uri uri = MemoryUri.builder().setKey("foo").build(); 38 assertThat(uri.getSchemeSpecificPart()).isEqualTo("foo"); 39 assertThat(uri.toString()).isEqualTo("memory:foo"); 40 } 41 42 @Test builder_setKey_requiresNonEmptyValue()43 public void builder_setKey_requiresNonEmptyValue() throws Exception { 44 assertThrows(MalformedUriException.class, () -> MemoryUri.builder().setKey("").build()); 45 } 46 47 @Test builder_buildsOpaqueUri()48 public void builder_buildsOpaqueUri() throws Exception { 49 Uri uri = MemoryUri.builder().setKey("foo?withQuestionMark").build(); 50 assertThat(uri.getSchemeSpecificPart()).isEqualTo("foo?withQuestionMark"); 51 assertThat(uri.toString()).isEqualTo("memory:foo%3FwithQuestionMark"); 52 53 uri = MemoryUri.builder().setKey("foo/withForwardSlash").build(); 54 assertThat(uri.getSchemeSpecificPart()).isEqualTo("foo/withForwardSlash"); 55 assertThat(uri.toString()).isEqualTo("memory:foo%2FwithForwardSlash"); 56 57 uri = MemoryUri.builder().setKey("foo#withPound").build(); 58 assertThat(uri.getSchemeSpecificPart()).isEqualTo("foo#withPound"); 59 assertThat(uri.toString()).isEqualTo("memory:foo%23withPound"); 60 } 61 } 62