I want to test parsing of data returned from server.
I thought I might create several test XML files and then feed them to the sax parser which uses my custom data handler (as in the application that I test).
But where should I put those test XMLs?
I tried with [project_root]/res/xml, but then I get the error:
android.content.res.Resources$NotFoundException: Resource ID #0x7f040000 type #0x1c is not valid
at android.content.res.Resources.loadXmlResourceParser(Resources.java:1870)
at android.content.res.Resources.getXml(Resources.java:779)
Does that mean that the xml is invalid, or that android couldn't find the XML file?
(I use the same XML that comes from the server - copied it from the firebug's net panel and pasted into the file).
Can I somehow read that XML as a text file, since getContext().getResources().getXml(R.xml.test_response1) returns XmlResourceParser instead of String (which I'd prefer)?
Put you xmls in MyAppTest/assets, for example MyAppTest/assets/my.xml, and then read it using
InputStream myxml = getInstrumentation().getContext().getAssets().open("my.xml");
then you can invoke the parser with this stream.
Simply put the file in your current package (ie into a resource package) and use the class loader...
private InputStream getTestImage(String pathToFile) throws IOException
{
final ClassLoader loader = getClass().getClassLoader();
assertNotNull(loader);
final InputStream resourceAsStream = loader.getResourceAsStream(pathToFile);
assertNotNull(resourceAsStream);
return resourceAsStream;
}
Related
I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);
To test one of my implementations I need a valid and correctly initialised File-Object that points to an image file.
I've tried the following:
placed an image into the androidTest/ressources-Folder named cat.jpeg
tried to create a file handle the following way: new File(InstrumentationRegistry.getContext().getClassLoader().getResource(“cat.jpg”).getPath())
when calling exists() on the initialized file it always returns false.
How do I have to setup the test / initialize the file to make it work?
One option is to put all the test resources in androidTest/assets. By fetching the AssetManager from the test package's context, you can get the assets as a list of filenames (AssetManager.list(String path)). For each asset, open an InputStream with AssetManager.open(String filename) and create a temporary file that you write the asset to.
I want to pass my xmls from my R.xml directory to Simple library for deserialization.
The library accepts Readers and InputSreams and such.
But from R.xml I get XMLPullParser. How can I get something like a Reader or InputStream instead?
if you save your xml fine inside res/raw instead of res/xml, you should be able to use openRawResource
getResources().openRawResource(R.raw.xml_name)
which returns an InputStream
When parsing an xml file in android, I'm doing like this:
try
{
InputStream is = ...
MyContentHandler ch = new MyContentHandler();
Xml.parse(is, Encoding.UTF_8, ch);
}
catch ...
The problem is that sometimes the file I'm trying to parse is not well-formed.
In my case, undeclared namespaces may be present.
The data I'm interested in is not inside those tags so I could simply ignore it, but I get an exception of unbound prefix not inside the content handler but in the parser itself; this means that if the exception occurs the entire parsing process is interrupted.
Is there a way of using the sax parser ignoring this kind of error (or namespaces at all)?
p.s. I want to avoid loading all the file in memory as a string and strip namespaces out of it, or having to rewrite the file.
I found the solution in another thread.
Instead of using Xml.parse you need to manually instantiate a sax parser through the SAXParserFactory and get a reader.
You can then set the reader features.
Among the available features, one disables namespaces and that does the trick.
Reference -> LINK
To sum up: in order to add easily unit tests for a SAX parser I would like to load XML from a file.
Now, I have my XML in a static string inside my unit test class, but it is not very convenient for large XML.
This is why I would like to add some XML files to my project and load them in my unit test. How can I do this?
This question is tagged as "Android" and I noticed that you mentioned an Activity, so I'm going to assume that you're trying to load an XML file within an Android application. If that is the case, put your XML file under /assets and call:
InputStream is = getAssets().open("input.xml")
from your Activity. From there, you can manipulate it into SAXBuilder. This will only work if you've set up your test to run on the emulator (or if you're just trying to debug outside of a unit test).
SAXBuilder has a constructor to read data from file:
Document build(java.io.File file)
This builds a document from the supplied filename.
http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html