Back to Repositories

Testing AtomicFile Operations in SmartTube ExoPlayer Implementation

This test suite validates the AtomicFile functionality in ExoPlayer’s core library, ensuring atomic file operations work correctly for safe file writing and reading. The tests verify file creation, deletion, and data integrity during read/write operations.

Test Coverage Overview

The test suite provides comprehensive coverage of AtomicFile operations including:

  • File deletion verification
  • Atomic write operations with data integrity checks
  • Read operation validation
  • Edge cases for interrupted writes

Implementation Analysis

The testing approach uses JUnit4 with AndroidJUnit4 runner for Android-specific file operations. Tests implement setUp/tearDown patterns for clean test environments and utilize Truth assertions for clear verification statements.

The implementation focuses on isolated atomic operations with proper resource cleanup.

Technical Details

Testing tools and configuration:

  • JUnit4 testing framework
  • AndroidJUnit4 test runner
  • Truth assertion library
  • Temporary file system operations
  • ApplicationProvider for context access

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation using setUp/tearDown methods
  • Resource cleanup after test execution
  • Clear test method naming conventions
  • Focused test cases with single responsibility
  • Effective use of assertion libraries

yuliskov/smarttube

exoplayer-amzn-2.10.6/library/core/src/test/java/com/google/android/exoplayer2/util/AtomicFileTest.java

            
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.exoplayer2.util;

import static com.google.common.truth.Truth.assertThat;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Tests {@link AtomicFile}. */
@RunWith(AndroidJUnit4.class)
public final class AtomicFileTest {

  private File tempFolder;
  private File file;
  private AtomicFile atomicFile;

  @Before
  public void setUp() throws Exception {
    tempFolder =
        Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
    file = new File(tempFolder, "atomicFile");
    atomicFile = new AtomicFile(file);
  }

  @After
  public void tearDown() throws Exception {
    Util.recursiveDelete(tempFolder);
  }

  @Test
  public void testDelete() throws Exception {
    assertThat(file.createNewFile()).isTrue();
    atomicFile.delete();
    assertThat(file.exists()).isFalse();
  }

  @Test
  public void testWriteRead() throws Exception {
    OutputStream output = atomicFile.startWrite();
    output.write(5);
    atomicFile.endWrite(output);
    output.close();

    assertRead();

    output = atomicFile.startWrite();
    output.write(5);
    output.write(6);
    output.close();

    assertRead();

    output = atomicFile.startWrite();
    output.write(6);

    assertRead();
    output.close();

    output = atomicFile.startWrite();

    assertRead();
    output.close();
  }

  private void assertRead() throws IOException {
    InputStream input = atomicFile.openRead();
    assertThat(input.read()).isEqualTo(5);
    assertThat(input.read()).isEqualTo(-1);
    input.close();
  }

}