Search in sources :

Example 6 with ImportExportException

use of com.ichi2.anki.exception.ImportExportException in project AnkiChinaAndroid by ankichinateam.

the class ZipFile method exportFiltered.

private JSONObject exportFiltered(ZipFile z, String path, Context context) throws IOException, JSONException, ImportExportException {
    // export into the anki2 file
    String colfile = path.replace(".apkg", ".anki2");
    super.exportInto(colfile, context);
    z.write(colfile, CollectionHelper.COLLECTION_FILENAME);
    // and media
    prepareMedia();
    JSONObject media = _exportMedia(z, mMediaFiles, mCol.getMedia().dir());
    // tidy up intermediate files
    SQLiteDatabase.deleteDatabase(new File(colfile));
    SQLiteDatabase.deleteDatabase(new File(path.replace(".apkg", ".media.ad.db2")));
    String tempPath = path.replace(".apkg", ".media");
    File file = new File(tempPath);
    if (file.exists()) {
        String deleteCmd = "rm -r " + tempPath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException e) {
        }
    }
    return media;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) File(java.io.File)

Example 7 with ImportExportException

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

the class ImportTest method testAnki2Mediadupes.

@Test
public void testAnki2Mediadupes() throws IOException, JSONException, ImportExportException {
    // add a note that references a sound
    Note n = mTestCol.newNote();
    n.setField(0, "[sound:foo.mp3]");
    long mid = n.model().getLong("id");
    mTestCol.addNote(n);
    // add that sound to the media directory
    FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.mp3"), false);
    os.write("foo".getBytes());
    os.close();
    mTestCol.close();
    // it should be imported correctly into an empty deck
    Collection empty = getEmptyCol();
    Importer imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    List<String> expected = Collections.singletonList("foo.mp3");
    List<String> actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // and importing again will not duplicate, as the file content matches
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    expected = Collections.singletonList("foo.mp3");
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("foo.mp3"));
    // if the local file content is different, and import should trigger a rename
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"), false);
    os.write("bar".getBytes());
    os.close();
    imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("_"));
    // if the localized media file already exists, we rewrite the note and media
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    os = new FileOutputStream(new File(empty.getMedia().dir(), "foo.mp3"));
    os.write("bar".getBytes());
    os.close();
    imp = new Anki2Importer(empty, mTestCol.getPath());
    imp.run();
    expected = Arrays.asList("foo.mp3", String.format("foo_%s.mp3", mid));
    actual = Arrays.asList(new File(empty.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    n = empty.getNote(empty.getDb().queryLongScalar("select id from notes"));
    assertTrue(n.getFields()[0].contains("_"));
    empty.close();
}
Also used : Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Note(com.ichi2.libanki.Note) FileOutputStream(java.io.FileOutputStream) Collection(com.ichi2.libanki.Collection) File(java.io.File) Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Importer(com.ichi2.libanki.importer.Importer) AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) NoteImporter(com.ichi2.libanki.importer.NoteImporter) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 8 with ImportExportException

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

the class ImportTest method testApkg.

@Test
public void testApkg() throws IOException, ImportExportException {
    String apkg = Shared.getTestFilePath(getTestContext(), "media.apkg");
    Importer imp = new AnkiPackageImporter(mTestCol, apkg);
    List<String> expected = Collections.emptyList();
    List<String> actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(actual.size(), expected.size());
    imp.run();
    expected = Collections.singletonList("foo.wav");
    actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // import again should be idempotent in terms of media
    mTestCol.remCards(mTestCol.getDb().queryLongList("select id from cards"));
    imp = new AnkiPackageImporter(mTestCol, apkg);
    imp.run();
    expected = Collections.singletonList("foo.wav");
    actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
    actual.retainAll(expected);
    assertEquals(expected.size(), actual.size());
    // but if the local file has different data, it will rename
    mTestCol.remCards(mTestCol.getDb().queryLongList("select id from cards"));
    FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.wav"), false);
    os.write("xyz".getBytes());
    os.close();
    imp = new AnkiPackageImporter(mTestCol, apkg);
    imp.run();
    assertEquals(2, new File(mTestCol.getMedia().dir()).list().length);
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Importer(com.ichi2.libanki.importer.Importer) AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) NoteImporter(com.ichi2.libanki.importer.NoteImporter) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 9 with ImportExportException

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

the class AnkiPackageExporterTest method missingFileInDeckExportDoesSkipsFile.

@Test
public void missingFileInDeckExportDoesSkipsFile() throws IOException, ImportExportException {
    // Arrange
    File mediaFilePath = addTempFileToMediaAndNote();
    if (!mediaFilePath.delete()) {
        throw new IllegalStateException("need to delete temp file for test to pass");
    }
    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[] file_names = new File[2];
    int i = 0;
    for (String x : files) {
        File f = new File(unzipDirectory + "/" + x);
        file_names[i++] = f;
    }
    // Checking the unzipped files
    assertThat(files, containsInAnyOrder("collection.anki2", "media"));
    assertThat("Only two files should exist", files, hasSize(2));
    checkMediaExportStringIs(file_names, "{}");
}
Also used : File(java.io.File) UnzipFile(com.ichi2.utils.UnzipFile) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 10 with ImportExportException

use of com.ichi2.anki.exception.ImportExportException in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method testAnki2DiffmodelTemplates.

@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
    // different from the above as this one tests only the template text being
    // changed, not the number of cards/fields
    // import the first version of the model
    String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-1.apkg");
    AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // then the version with updated template
    tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-2.apkg");
    imp = new AnkiPackageImporter(testCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // collection should contain the note we imported
    assertEquals(1, testCol.noteCount());
    // the front template should contain the text added in the 2nd package
    Long tcid = testCol.findCards("").get(0);
    Note tnote = testCol.getCard(tcid).note();
    assertTrue(testCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Note(com.ichi2.libanki.Note) 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