Search in sources :

Example 1 with ImportExportException

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

the class ImportTest method testAnki2Mediadupes.

@Test
public void testAnki2Mediadupes() throws IOException, JSONException, ImportExportException {
    List<String> expected;
    List<String> actual;
    // add a note that references a sound
    Note n = testCol.newNote();
    n.setField(0, "[sound:foo.mp3]");
    long mid = n.model().getLong("id");
    testCol.addNote(n);
    // add that sound to the media folder
    FileOutputStream os;
    os = new FileOutputStream(new File(testCol.getMedia().dir(), "foo.mp3"), false);
    os.write("foo".getBytes());
    os.close();
    testCol.close();
    // it should be imported correctly into an empty deck
    Collection empty = Shared.getEmptyCol(InstrumentationRegistry.getInstrumentation().getTargetContext());
    Importer imp = new Anki2Importer(empty, testCol.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());
    // and importing again will not duplicate, as the file content matches
    empty.remCards(empty.getDb().queryLongList("select id from cards"));
    imp = new Anki2Importer(empty, testCol.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, testCol.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, testCol.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)

Example 2 with ImportExportException

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

the class ImportTest method testDupeIgnore.

/**
 * Custom tests for AnkiDroid.
 */
@Test
public void testDupeIgnore() throws IOException, ImportExportException {
    // create a new empty deck
    String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update1.apkg");
    AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
    imp.run();
    tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update3.apkg");
    imp = new AnkiPackageImporter(testCol, tmp);
    imp.run();
    // there is a dupe, but it was ignored
    assertEquals(1, imp.getDupes());
    assertEquals(0, imp.getAdded());
    assertEquals(0, imp.getUpdated());
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Test(org.junit.Test)

Example 3 with ImportExportException

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

the class ImportTest method testAnki2Updates.

@Test
public void testAnki2Updates() throws IOException, ImportExportException {
    // create a new empty deck
    String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update1.apkg");
    AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
    imp.run();
    assertEquals(0, imp.getDupes());
    assertEquals(1, imp.getAdded());
    assertEquals(0, imp.getUpdated());
    // importing again should be idempotent
    imp = new AnkiPackageImporter(testCol, tmp);
    imp.run();
    assertEquals(1, imp.getDupes());
    assertEquals(0, imp.getAdded());
    assertEquals(0, imp.getUpdated());
    // importing a newer note should update
    assertEquals(1, testCol.noteCount());
    assertTrue(testCol.getDb().queryString("select flds from notes").startsWith("hello"));
    tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update2.apkg");
    imp = new AnkiPackageImporter(testCol, tmp);
    imp.run();
    assertEquals(1, imp.getDupes());
    assertEquals(0, imp.getAdded());
    assertEquals(1, imp.getUpdated());
    assertTrue(testCol.getDb().queryString("select flds from notes").startsWith("goodbye"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Test(org.junit.Test)

Example 4 with ImportExportException

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

the class CollectionTask method doInBackgroundImportAdd.

private TaskData doInBackgroundImportAdd(TaskData param) {
    Timber.d("doInBackgroundImportAdd");
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    Collection col = getCol();
    String path = param.getString();
    AnkiPackageImporter imp = new AnkiPackageImporter(col, path);
    imp.setProgressCallback(new ProgressCallback(this, res));
    try {
        imp.run();
    } catch (ImportExportException e) {
        return new TaskData(e.getMessage(), true);
    }
    return new TaskData(new Object[] { imp });
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) ImportExportException(com.ichi2.anki.exception.ImportExportException) Collection(com.ichi2.libanki.Collection) Resources(android.content.res.Resources)

Example 5 with ImportExportException

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

the class CollectionTask method doInBackgroundExportApkg.

private TaskData doInBackgroundExportApkg(TaskData param) {
    Timber.d("doInBackgroundExportApkg");
    Object[] data = param.getObjArray();
    Collection col = (Collection) data[0];
    String apkgPath = (String) data[1];
    Long did = (Long) data[2];
    boolean includeSched = (Boolean) data[3];
    boolean includeMedia = (Boolean) data[4];
    boolean exportApkg = (Boolean) data[5];
    boolean exportCard = (Boolean) data[6];
    try {
        AnkiPackageExporter exporter = new AnkiPackageExporter(col);
        exporter.setIncludeSched(includeSched);
        exporter.setIncludeMedia(includeMedia);
        exporter.setExportCard(exportCard);
        exporter.setExportApkg(exportApkg);
        exporter.setDid(did);
        exporter.exportInto(apkgPath, mContext);
    } catch (FileNotFoundException e) {
        Timber.e(e, "FileNotFoundException in doInBackgroundExportApkg");
        return new TaskData(false);
    } catch (IOException e) {
        Timber.e(e, "IOException in doInBackgroundExportApkg");
        return new TaskData(false);
    } catch (JSONException e) {
        Timber.e(e, "JSOnException in doInBackgroundExportApkg");
        return new TaskData(false);
    } catch (ImportExportException e) {
        Timber.e(e, "ImportExportException in doInBackgroundExportApkg");
        return new TaskData(e.getMessage(), true);
    }
    return new TaskData(exportCard ? apkgPath.replace(".apkg", ".card") : apkgPath);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) JSONException(com.ichi2.utils.JSONException) IOException(java.io.IOException) AnkiPackageExporter(com.ichi2.libanki.AnkiPackageExporter) ImportExportException(com.ichi2.anki.exception.ImportExportException) Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject)

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