Load file from resource - android

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

Related

Accessing assets of main application inside package

i'm currently trying to develop a package for a Flutter App, with Kotlin. My issue is that I need to provide the package with a config file, which should only be defined inside the main App. Since the config differs for the Dev and Prod environment, the app should pass through the path of the File via the Method Channel. The problem is that the package isn't able to access the assets folder of the calling application.
Path: "assets/config.json" (the root being the main application)
Steps I already tried:
Creating the file inside the res/raw & accessing the config file through a ressource id -> Kotlin gives me an "Unresolved reference" error, unless I create the file inside the packages res/raw
Instead of passing through the path, I tried passing through the content of the config & writing it into an empty temporary file. The code in Kotlin like this:
val config = File(applicationContext.filesDir,"config.json")
config.writeText(configContent)
-> This works, but it seems like a weird solution to the problem.
please let me know if I need to provide further information & thank you in advance!
edit:
The Java Method that is called during initialisation:
public static void createMultipleAccountPublicClientApplication(#NonNull final Context context,
#NonNull final File configFile,
#NonNull final IMultipleAccountApplicationCreatedListener listener)
Flutter assets aren't files - they are packaged up and only available through the rootBundle. So, if you want to make a file from a text asset, someone has to load the asset and write it to a file.
As your plugin user will be in charge of the asset, they will have to do the first part (and will end up with a String). The question arises of who should do the writing.
You could make the plugin user use path_provider to find the temporary directory and write it there and then pass you the file path. Eventually, down in the Java, you new File(theTempFilePath). Or they could pass the string to the Dart half of your plugin and you create the temp file in the same way.
It's probably more convenient if they pass your plugin the string, you pass that to the native side and have the native side create a temporary file and write the string there. (BTW, I assume we are talking about this config file: https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-configuration#how-to-use-a-configuration-file )
See this answer for creating temporary files: Creating temporary files in Android
Note that there's actually no reason that your plugin user then needs to use an asset. They could, instead, just hard code the string in their code if the configuration never really changes.
There's an argument that as this is a JSON configuration file, you may not want to bother your user with the details of this JSON configuration file. You may want to default it in your Dart code (why not hard code it as a string, as above, if it never really changes) and then provide some methods to override particular values like the client id and the redirect uri, which may be the only things that users ever change in practice. So rather than making them supply a complete JSON file, they just give you those two strings and you plonk them into your default JSON. Maybe a version 2 feature :)

Add XML Serialized object to APK so it is copied into the app's private files directory

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);

Android Instrumentation Test get resource file handle

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.

Read xml file in Android/Mono

I'm trying to read an xml file which I plan to include as part of my mobile application. I've created a folder called "XML" and added a file called "test.xml". The files build action is set to content. When I run the app it crashes on trying to find the file. Here is the code I'm using:
_path = "XML/test.xml";
doc = new XmlDocument();
doc.Load(_path);
Here is the error I get:
System.IO.DirectoryNotFoundException has been thrown.
Could not find a part of the path "/XML/test.xml"
Do I need to register the directory/folder somehow? Thanks for any advice, I'm sure I've just missed something simple...
Do you mean that you'd created XML directory inside of your project?
If you want to include XML to your APK - one of the best ways will be to create /res/xml/file.xml and read it inside application with Resources class.
Please have a look at this thread - maybe you'll find it helpful. Open XML file from res/xml in Android
Good luck

Using specific resources for android testing

I'm trying to test an android project. I'm using intellij IDEA.
So far everything goes perfect. My problem arises when I try to use a different xml resource file for the tests (with specific data for testing) instead of using the XML resource from the tested project.
I have the following directory structure:
Project
|
|---- assets
|---- res
|-------|---- values
|-------|---- xml
|---- src
|---- tests
---------|----res
---------|------|-----xml
---------|-----src
I've assumed that the XML stored in the Project/tests/res/xml would automatically replace the one stored in Project/res/xml when running under the context of the test. It is not happening so, the tests keep on taking the Project/res/xml file instead of the test xml.
Do I have to make anything additional? Is there a way to somewhat mock this?
Thanks!
Certainly you can place your XML files in the resource or assets folder of your test project and access them by:
InputStream myxml = getInstrumentation().getContext().getAssets().open("my_document.xml");

Categories

Resources