Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.android.server.backup.utils;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.platform.test.annotations.Presubmit;
     22 import android.support.test.filters.SmallTest;
     23 import android.support.test.runner.AndroidJUnit4;
     24 import org.junit.Rule;
     25 import org.junit.Test;
     26 import org.junit.rules.TemporaryFolder;
     27 import org.junit.runner.RunWith;
     28 
     29 import java.io.DataInputStream;
     30 import java.io.DataOutputStream;
     31 import java.io.File;
     32 import java.io.IOException;
     33 
     34 @SmallTest
     35 @Presubmit
     36 @RunWith(AndroidJUnit4.class)
     37 public final class DataStreamFileCodecTest {
     38     @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
     39 
     40     @Test
     41     public void serialize_writesToTheFile() throws Exception {
     42         File unicornFile = mTemporaryFolder.newFile();
     43 
     44         DataStreamFileCodec<MythicalCreature> mythicalCreatureCodec = new DataStreamFileCodec<>(
     45                 unicornFile, new MythicalCreatureDataStreamCodec());
     46         MythicalCreature unicorn = new MythicalCreature(
     47                 10000, "Unicorn");
     48         mythicalCreatureCodec.serialize(unicorn);
     49 
     50         DataStreamFileCodec<MythicalCreature> newCodecWithSameFile = new DataStreamFileCodec<>(
     51                 unicornFile, new MythicalCreatureDataStreamCodec());
     52         MythicalCreature deserializedUnicorn = newCodecWithSameFile.deserialize();
     53 
     54         assertThat(deserializedUnicorn.averageLifespanInYears)
     55                 .isEqualTo(unicorn.averageLifespanInYears);
     56         assertThat(deserializedUnicorn.name).isEqualTo(unicorn.name);
     57     }
     58 
     59     private static class MythicalCreature {
     60         int averageLifespanInYears;
     61         String name;
     62 
     63         MythicalCreature(int averageLifespanInYears, String name) {
     64             this.averageLifespanInYears = averageLifespanInYears;
     65             this.name = name;
     66         }
     67     }
     68 
     69     private static class MythicalCreatureDataStreamCodec implements
     70             DataStreamCodec<MythicalCreature> {
     71         @Override
     72         public void serialize(MythicalCreature mythicalCreature,
     73                 DataOutputStream dataOutputStream) throws IOException {
     74             dataOutputStream.writeInt(mythicalCreature.averageLifespanInYears);
     75             dataOutputStream.writeUTF(mythicalCreature.name);
     76         }
     77 
     78         @Override
     79         public MythicalCreature deserialize(DataInputStream dataInputStream)
     80                 throws IOException {
     81             int years = dataInputStream.readInt();
     82             String name = dataInputStream.readUTF();
     83             return new MythicalCreature(years, name);
     84         }
     85     }
     86 }
     87