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();
}
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());
}
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"));
}
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 });
}
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);
}
Aggregations