<commit_before><commit_msg>Add test that verifies that IOUtils.readAsset() closes the stream. (#898)<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.utils

import android.content.Context
import android.content.res.AssetManager
import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.*
import java.io.ByteArrayInputStream

class IOUtilsTest {
    /**
     * Verify that IOUtils.readAsset() closes the AssetInputStream.
     */
    @Test
    fun testReadAssetClosesStream() {
        // Setup

        val stream = spy(ByteArrayInputStream("Hello".toByteArray()))

        val assetManager = mock(AssetManager::class.java)
        `when`(assetManager.open(ArgumentMatchers.anyString())).thenReturn(stream)

        val context = mock(Context::class.java)
        `when`(context.assets).thenReturn(assetManager)

        // Now use our mock classes to call IOUtils.close()

        IOUtils.readAsset(context, "test.txt")

        // Verify that the stream was opened and closed

        verify(assetManager).open("test.txt")
        verify(stream).close()
    }
}
{'fraction_non_alphanumeric': 0.1088646967340591, 'fraction_numerical': 0.005443234836702955, 'mean_word_length': 3.3043478260869565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2365632', 'n_tokens_mistral': 374, 'n_tokens_neox': 366, 'n_words': 101}