Search in sources :

Example 21 with ImportExportException

use of com.ichi2.anki.exception.ImportExportException in project Anki-Android by ankidroid.

the class AnkiPackageExporterTest method fileInExportIsCopied.

@Test
public void fileInExportIsCopied() throws IOException, ImportExportException {
    // Arrange
    File tempFileInCollection = addTempFileToMediaAndNote();
    AnkiPackageExporter exporter = getExporterForDeckWithMedia();
    File temp = CreateTempDir.tempDir("/AnkiDroid-missingFileInExportDoesNotThrowException-export");
    File exportedFile = new File(temp.getAbsolutePath() + "/export.apkg");
    // Exporting
    exporter.exportInto(exportedFile.getAbsolutePath(), getTargetContext());
    // Unzipping the export.apkg file
    UnzipFile.unzip(exportedFile, temp.getAbsolutePath() + "/unzipped");
    String unzipDirectory = temp.getAbsolutePath() + "/unzipped";
    // Storing paths of unzipped files in a list
    List<String> files = Arrays.asList(new File(unzipDirectory).list());
    File[] fileNames = new File[3];
    int i = 0;
    for (String x : files) {
        File f = new File(unzipDirectory + "/" + x);
        fileNames[i++] = f;
    }
    // Checking the unzipped files
    assertThat(files, containsInAnyOrder("collection.anki2", "media", "0"));
    assertThat("Three files should exist", files, hasSize(3));
    // {"0":"filename.txt"}
    String expected = String.format("{\"0\":\"%s\"}", tempFileInCollection.getName());
    checkMediaExportStringIs(fileNames, expected);
}
Also used : File(java.io.File) UnzipFile(com.ichi2.utils.UnzipFile) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 File (java.io.File)13 AnkiPackageImporter (com.ichi2.libanki.importer.AnkiPackageImporter)11 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)5 Collection (com.ichi2.libanki.Collection)5 JSONObject (com.ichi2.utils.JSONObject)5 RobolectricTest (com.ichi2.anki.RobolectricTest)4 Note (com.ichi2.libanki.Note)4 Anki2Importer (com.ichi2.libanki.importer.Anki2Importer)4 Importer (com.ichi2.libanki.importer.Importer)4 NoteImporter (com.ichi2.libanki.importer.NoteImporter)4 TextImporter (com.ichi2.libanki.importer.TextImporter)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 ImportExportException (com.ichi2.anki.exception.ImportExportException)2 JSONArray (com.ichi2.utils.JSONArray)2 UnzipFile (com.ichi2.utils.UnzipFile)2 FileNotFoundException (java.io.FileNotFoundException)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2