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.samples; 17 18 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.createFile; 19 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.readFile; 20 import static com.google.common.truth.Truth.assertThat; 21 import static java.nio.charset.StandardCharsets.UTF_8; 22 23 import android.net.Uri; 24 import android.text.TextUtils; 25 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 26 import com.google.android.libraries.mobiledatadownload.file.backends.FileUriAdapter; 27 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend; 28 import com.google.android.libraries.mobiledatadownload.file.common.Sizable; 29 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri; 30 import com.google.android.libraries.mobiledatadownload.file.openers.ReadStreamOpener; 31 import com.google.android.libraries.mobiledatadownload.file.transforms.CompressTransform; 32 import com.google.common.collect.ImmutableList; 33 import com.google.common.collect.Lists; 34 import com.google.common.io.BaseEncoding; 35 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; 36 import java.io.BufferedReader; 37 import java.io.File; 38 import java.io.FileInputStream; 39 import java.io.InputStream; 40 import java.io.InputStreamReader; 41 import java.util.List; 42 import org.junit.Before; 43 import org.junit.Rule; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 47 @RunWith(GoogleRobolectricTestRunner.class) 48 public final class SamplesTest { 49 SynchronousFileStorage storage; 50 ByteCountingMonitor byteCounter; 51 52 @Rule public TemporaryUri tmpUri = new TemporaryUri(); 53 54 @Before setupFileStorage()55 public void setupFileStorage() { 56 byteCounter = new ByteCountingMonitor(); 57 storage = 58 new SynchronousFileStorage( 59 ImmutableList.of(new JavaFileBackend(), new Base64Backend()), 60 ImmutableList.of( 61 new Base64Transform(), new CapitalizationTransform(), new CompressTransform()), 62 ImmutableList.of(byteCounter)); 63 } 64 65 @Test capitalizeAndBase64ShouldRoundTrip()66 public void capitalizeAndBase64ShouldRoundTrip() throws Exception { 67 Uri uri = tmpUri.newUri().buildUpon().encodedFragment("transform=capitalize+base64").build(); 68 createFile(storage, uri, "SOME ALL CAPS TEXT"); 69 assertThat(readFile(storage, uri)).isEqualTo("SOME ALL CAPS TEXT"); 70 71 // Check raw file and manually decoded result. 72 List<String> path = Lists.newArrayList(uri.getPathSegments()); 73 String filename = path.get(path.size() - 1); 74 filename = filename.toLowerCase(); // capitalize 75 filename = BaseEncoding.base64().encode(filename.getBytes(UTF_8)); // base64 76 path.set(path.size() - 1, filename); 77 Uri encodedUri = uri.buildUpon().path(TextUtils.join("/", path)).build(); 78 File encodedFile = FileUriAdapter.instance().toFile(encodedUri); 79 BufferedReader rawReader = 80 new BufferedReader(new InputStreamReader(new FileInputStream(encodedFile), UTF_8)); 81 String encodedText = rawReader.readLine(); 82 assertThat(encodedText).isEqualTo("c29tZSBhbGwgY2FwcyB0ZXh0"); 83 String lowercaseText = new String(BaseEncoding.base64().decode(encodedText), UTF_8); 84 assertThat(lowercaseText).isEqualTo("some all caps text"); 85 rawReader.close(); 86 87 assertThat(byteCounter.stats()) 88 .isEqualTo(new long[] {encodedText.length(), encodedText.length()}); 89 } 90 91 @Test capitalizeAndBase64AndCompressShouldRoundTrip()92 public void capitalizeAndBase64AndCompressShouldRoundTrip() throws Exception { 93 Uri uri = 94 tmpUri.newUri().buildUpon().encodedFragment("transform=capitalize+base64+compress").build(); 95 createFile(storage, uri, "SOME ALL CAPS TEXT"); 96 assertThat(readFile(storage, uri)).isEqualTo("SOME ALL CAPS TEXT"); 97 assertThat(byteCounter.stats()).isEqualTo(new long[] {32, 32}); 98 } 99 100 @Test base64BackendShouldRoundTrip()101 public void base64BackendShouldRoundTrip() throws Exception { 102 Uri uri = tmpUri.newUri().buildUpon().scheme("base64").build(); 103 createFile(storage, uri, "SOME ALL CAPS TEXT"); 104 assertThat(readFile(storage, uri)).isEqualTo("SOME ALL CAPS TEXT"); 105 assertThat(byteCounter.stats()) 106 .isEqualTo(new long[] {"SOME ALL CAPS TEXT".length(), "SOME ALL CAPS TEXT".length()}); 107 } 108 109 @Test capitalizationTransform_shouldBeSizable()110 public void capitalizationTransform_shouldBeSizable() throws Exception { 111 Uri uri = tmpUri.newUri().buildUpon().encodedFragment("transform=capitalize").build(); 112 String text = "SOME ALL CAPS TEXT"; 113 createFile(storage, uri, text); 114 try (InputStream in = storage.open(uri, ReadStreamOpener.create())) { 115 assertThat(in).isInstanceOf(Sizable.class); 116 assertThat(((Sizable) in).size()).isEqualTo(text.length()); 117 } 118 } 119 } 120