Search in sources :

Example 36 with StringReader

use of java.io.StringReader in project platform_frameworks_base by android.

the class FullBackupTest method testparseBackupSchemeFromXml_lotsOfIncludesAndExcludes.

public void testparseBackupSchemeFromXml_lotsOfIncludesAndExcludes() throws Exception {
    mXpp.setInput(new StringReader("<full-backup-content>" + "<exclude path=\"exclude1.txt\" domain=\"file\"/>" + "<include path=\"include1.txt\" domain=\"file\"/>" + "<exclude path=\"exclude2.txt\" domain=\"database\"/>" + "<include path=\"include2.txt\" domain=\"database\"/>" + "<exclude path=\"exclude3\" domain=\"sharedpref\"/>" + "<include path=\"include3\" domain=\"sharedpref\"/>" + "<exclude path=\"exclude4.xml\" domain=\"sharedpref\"/>" + "<include path=\"include4.xml\" domain=\"sharedpref\"/>" + "</full-backup-content>"));
    FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
    bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
    Set<String> fileDomainIncludes = includeMap.get(FullBackup.FILES_TREE_TOKEN);
    assertEquals("Didn't find expected file domain include.", 1, fileDomainIncludes.size());
    assertEquals("Invalid path parsed for <include/>", new File(mContext.getFilesDir(), "include1.txt").getCanonicalPath(), fileDomainIncludes.iterator().next());
    Set<String> databaseDomainIncludes = includeMap.get(FullBackup.DATABASE_TREE_TOKEN);
    // Three expected here because of "-journal" and "-wal" files
    assertEquals("Didn't find expected database domain include.", 3, databaseDomainIncludes.size());
    assertTrue("Invalid path parsed for <include/>", databaseDomainIncludes.contains(new File(mContext.getDatabasePath("foo").getParentFile(), "include2.txt").getCanonicalPath()));
    assertTrue("Invalid path parsed for <include/>", databaseDomainIncludes.contains(new File(mContext.getDatabasePath("foo").getParentFile(), "include2.txt-journal").getCanonicalPath()));
    assertTrue("Invalid path parsed for <include/>", databaseDomainIncludes.contains(new File(mContext.getDatabasePath("foo").getParentFile(), "include2.txt-wal").getCanonicalPath()));
    List<String> sharedPrefDomainIncludes = new ArrayList<String>(includeMap.get(FullBackup.SHAREDPREFS_TREE_TOKEN));
    Collections.sort(sharedPrefDomainIncludes);
    assertEquals("Didn't find expected sharedpref domain include.", 3, sharedPrefDomainIncludes.size());
    assertEquals("Invalid path parsed for <include/>", new File(mContext.getSharedPrefsFile("foo").getParentFile(), "include3").getCanonicalPath(), sharedPrefDomainIncludes.get(0));
    assertEquals("Invalid path parsed for <include/>", new File(mContext.getSharedPrefsFile("foo").getParentFile(), "include3.xml").getCanonicalPath(), sharedPrefDomainIncludes.get(1));
    assertEquals("Invalid path parsed for <include/>", new File(mContext.getSharedPrefsFile("foo").getParentFile(), "include4.xml").getCanonicalPath(), sharedPrefDomainIncludes.get(2));
    assertEquals("Unexpected number of <exclude/>s", 7, excludesSet.size());
    // Sets are annoying to iterate over b/c order isn't enforced - convert to an array and
    // sort lexicographically.
    List<String> arrayedSet = new ArrayList<String>(excludesSet);
    Collections.sort(arrayedSet);
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getDatabasePath("foo").getParentFile(), "exclude2.txt").getCanonicalPath(), arrayedSet.get(0));
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getDatabasePath("foo").getParentFile(), "exclude2.txt-journal").getCanonicalPath(), arrayedSet.get(1));
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getDatabasePath("foo").getParentFile(), "exclude2.txt-wal").getCanonicalPath(), arrayedSet.get(2));
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getFilesDir(), "exclude1.txt").getCanonicalPath(), arrayedSet.get(3));
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getSharedPrefsFile("foo").getParentFile(), "exclude3").getCanonicalPath(), arrayedSet.get(4));
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getSharedPrefsFile("foo").getParentFile(), "exclude3.xml").getCanonicalPath(), arrayedSet.get(5));
    assertEquals("Invalid path parsed for <exclude/>", new File(mContext.getSharedPrefsFile("foo").getParentFile(), "exclude4.xml").getCanonicalPath(), arrayedSet.get(6));
}
Also used : StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) File(java.io.File)

Example 37 with StringReader

use of java.io.StringReader in project platform_frameworks_base by android.

the class FullBackupTest method testDoubleSlashInPath_isIgnored.

public void testDoubleSlashInPath_isIgnored() throws Exception {
    mXpp.setInput(new StringReader("<full-backup-content>" + // Invalid use of "//"
    "<exclude path=\"//hello.txt\" domain=\"file\"/>" + "</full-backup-content>"));
    FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
    bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
    assertEquals("Didn't throw away invalid path containing \"//\".", 0, excludesSet.size());
}
Also used : StringReader(java.io.StringReader)

Example 38 with StringReader

use of java.io.StringReader in project platform_frameworks_base by android.

the class FullBackupTest method testParseBackupSchemeFromXml_invalidXmlFails.

public void testParseBackupSchemeFromXml_invalidXmlFails() throws Exception {
    // Invalid root tag.
    mXpp.setInput(new StringReader("<full-weird-tag>" + "<exclude path=\"invalidRootTag.txt\" domain=\"file\"/>" + "</ffull-weird-tag>"));
    try {
        FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
        bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
        fail("Invalid root xml tag should throw an XmlPullParserException");
    } catch (XmlPullParserException expected) {
    }
    // Invalid exclude tag.
    mXpp.setInput(new StringReader("<full-backup-content>" + "<excluded path=\"invalidExcludeTag.txt\" domain=\"file\"/>" + "</full-backup-conten>t"));
    try {
        FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
        bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
        fail("Misspelled xml exclude tag should throw an XmlPullParserException");
    } catch (XmlPullParserException expected) {
    }
    // Just for good measure - invalid include tag.
    mXpp.setInput(new StringReader("<full-backup-content>" + "<yinclude path=\"invalidIncludeTag.txt\" domain=\"file\"/>" + "</full-backup-conten>t"));
    try {
        FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
        bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
        fail("Misspelled xml exclude tag should throw an XmlPullParserException");
    } catch (XmlPullParserException expected) {
    }
}
Also used : StringReader(java.io.StringReader) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 39 with StringReader

use of java.io.StringReader in project platform_frameworks_base by android.

the class FullBackupTest method testDoubleDotInPath_isIgnored.

public void testDoubleDotInPath_isIgnored() throws Exception {
    mXpp.setInput(new StringReader("<full-backup-content>" + // Invalid use of ".." dir.
    "<include path=\"..\" domain=\"file\"/>" + "</full-backup-content>"));
    FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
    bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
    assertEquals("Didn't throw away invalid \"..\" path.", 0, includeMap.size());
    Set<String> fileDomainIncludes = includeMap.get(FullBackup.FILES_TREE_TOKEN);
    assertNull("Didn't throw away invalid \"..\" path.", fileDomainIncludes);
}
Also used : StringReader(java.io.StringReader)

Example 40 with StringReader

use of java.io.StringReader in project platform_frameworks_base by android.

the class FullBackupTest method testInvalidPath_doesNotBackup.

public void testInvalidPath_doesNotBackup() throws Exception {
    mXpp.setInput(new StringReader("<full-backup-content>" + // Invalid use of ".." dir.
    "<exclude path=\"..\" domain=\"file\"/>" + "</full-backup-content>"));
    FullBackup.BackupScheme bs = FullBackup.getBackupSchemeForTest(mContext);
    bs.parseBackupSchemeFromXmlLocked(mXpp, excludesSet, includeMap);
    assertEquals("Didn't throw away invalid \"..\" path.", 0, includeMap.size());
    Set<String> fileDomainIncludes = includeMap.get(FullBackup.FILES_TREE_TOKEN);
    assertNull("Didn't throw away invalid \"..\" path.", fileDomainIncludes);
}
Also used : StringReader(java.io.StringReader)

Aggregations

StringReader (java.io.StringReader)4150 Test (org.junit.Test)1003 IOException (java.io.IOException)589 Reader (java.io.Reader)445 InputSource (org.xml.sax.InputSource)408 BufferedReader (java.io.BufferedReader)342 TokenStream (org.apache.lucene.analysis.TokenStream)302 ArrayList (java.util.ArrayList)273 StringWriter (java.io.StringWriter)251 Tokenizer (org.apache.lucene.analysis.Tokenizer)241 Document (org.w3c.dom.Document)232 JSONReader (com.alibaba.fastjson.JSONReader)195 DocumentBuilder (javax.xml.parsers.DocumentBuilder)180 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)157 Map (java.util.Map)144 HashMap (java.util.HashMap)136 Element (org.w3c.dom.Element)134 StreamSource (javax.xml.transform.stream.StreamSource)132 ParserResult (org.jabref.logic.importer.ParserResult)130 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)120