Accessing Files In webapp Folder During Unit Testing - junit

I've seen several variations of this question on StackOverflow, but none describe my scenario exactly, or offer a solution that works for me.
I'm building a Java EE web game. I'm using JUnit to do unit testing during the build process. In my webapp folder, I have assets that users access via HTTP GET requests. In my source code, I have corresponding Java classes that implement the logic of those assets.
For example, I have a class called CookingPot, which implements the logic for a cooking pot in my game. In the webapp folder, I have a files that provide media and content for cooking pots to the user, for example:
webapp/assets/items/CookingPot.svg (an image of the cooking pot)
webapp/assets/items/CookingPot.html (an HTML description of the cooking pot)
During unit testing, I'd like to make sure that for every game item (in this example, the CookingPot item), there is an SVG and HTML file in the correct place in the webapp folder.
How can I implement this test? The JUnit tests are executed in the Maven build environment, which does not provide a Tomcat server to test against, so accessing them via HTTP won't work. I've tried:
URL svg = getClass().getResource("/webapp/assets/items/CookingPot.svg");
URL svg = getClass().getResource("webapp/assets/items/CookingPot.svg");
But those return null.
I have a feeling that there is something simple I'm missing here, but I don't know what it is. Surely there's got to be a way to test that these files exist during the build process?
Thanks in advance for any help!

The src/main/webapp folder is not part of the classpath (per default). Hence you can't access them with the code you provide in your question. If your files would be part of src/main/resources you could access them with .getResource("") from your Unit test without further configuration.
Modifying the Surefire plugin and including the webapp folder can solve your issue using your current setup (like mentioned here: Unit Test in Maven requires access to the src/main/webapp Directory):
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>src/main/webapp</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>

Related

urls to html files generated by applications in openshift environment

My (java) application (in openshift environment) generates/updates a few html files for each user (based on changes of user data) on daily basis. Since these are application-generated files, they are stored in a directory under ~/jbosseap/ (e.g. ~/jbosseap/htmls/xxxxxx.html). what should be the urls to these html files, by which they can be accessed on internet?
Just an update on how have I solved the problem I presented above. First of all, it is good to rephrase the issue for clarity:
My objective: I have an application which creates/updates a set of html pages regularly. These pages are stored (by the application) in a directory on the file system. For the purpose of this writing, let's say the fullpath to the directory is "/opt/mydata/*".
Users of the app need to access these html pages from internet. Therefore, urls to these pages need to be provided: e.g., http://mycompany.com/static/sample.html for /opt/mydata/sample.html.
My environment: my app is a J2EE application, developed with Springframework 3.1, running in Openshift's jbosseap environment
My solution: add the following line to the (servlet's) context configuration:
<mvc:resources mapping="/static/**" location="file:/opt/mydata/"/>
The trick, I think, is "file:" in the clause to allow inclusion of resources
on a file system external to the application.
It has worked for me. Hope it helps anyone who have wondered too.
Credit Thanks to Eugen Paraschiv, whose post led me to the answer.

How do I configure a Web Application project for working with html pages without .Net code?

We have a few html pages in one of our solutions that are meant to be extremely simple, client side only, pure html+javascript pages that access our web api. The api itself is in a web application project in the same solution.
We are now using a web site project to contain those files, but it is getting harder and harder to manage that project, since it's information is placed on the solution, and most of it's aspects cannot be controlled like they can on a msbuild project file.
I'd like to migrate those html files to a web application project, but I'm struggling to make it as basic as possible. For instance, I do not want to generate any dlls on the project. It should be in the solution just to provide access to the files and to enable us to control what goes to the _PublishedWebsites folder on the build by setting the build action on the files. We need this because there are some miscellaneous files in the project that should not be published.
I tried creating an empty web application and removing most things from it, by editing the csproj file. I managed to delete all references and the whole Properties special folder (along with the AssemblyInfo.cs file), but when I run the build command, I still see a dll created along with the obj and bin folders. Then, I tried faking the build target on the csproj file, like this:
<Target Name="Build" />
Now when the project is built, no dll/pdb is created, but the obj and bin folders are still there. Next, I tried setting the outputpath property to the current directory, like this:
<OutputPath>.</OutputPath>
But even then, the obj folder is still created.
EDIT:
I just found another common msbuild property that controls where the files inside the obj folder are placed. After placing this in my csproj file:
<IntermediateOutputPath>.</IntermediateOutputPath>
I now get no folders generated on build, which is nice.
There is a small problem now though (and I'm not sure how and where exactly this process happens) when I open the solution or reload the project in Visual Studio. Even though the project is not being built at this time, some files are still generated:
I feel the current approach is enough for my requirements, yet I'd really like to know if there is a more elegant way to achieve that. Thus, the question holds: Is there a way to make the web application project work as if there was no code file in it, effectively disabling output generation (bin and obj folders, and the dll/xml/pdb outputs)?

How can I use a console application with my website in Visual Studio?

Please read the following in the Visual Studio 2012 context:
I have two projects--one is a website (File --> New Website) and another is a console application (File --> New Project --> Windows --> Console Application). I am the author of the former.
The standalone app fakes the input by hardcoding it, runs through some code, and creates an output. It uses dlls from a local installation of software that I have installed on my machine to generate this output.
I read on MSDN that I cannot add a console app to a website solution in a useful manner. So, if I compile the console app to output a dll instead of an exe, can I reference that dll in my website? How can I do this exactly? I would need to pass the input value from the website to the dll, and return meaningful results from the dll. Is this possible?
Yes, you describe a feasible way to solve this. You need to create a class library project, add source code from console application to it, except the the class that has static Main method and modify (add to) that source code such that there is a class that you will be able instantiate from the code in your web application after you add the class library assembly to the web application as a reference. This class will have a method with appropriate parameters, that you will call. All this assuming that the task that console application code performs is fast and will not create noticeable delay in the web application response. If the task takes a long time, you will either have to run it in a background thread or move it outside the web application - the latter is significantly more involved.

Configuration with MBUnit

I want to use MBUnit to write unit tests for my application, however I will require some configuration to be set up.
My understanding is that any App.config files I add to the unit test DLL will not be loaded. I've looked in the wiki and can't see a way, is there a mechanism for loading my configuration?
I have looked into this more by myself, I found this article.
You can do this by creating a config file as you would a regular exe (DllName.dll.config), MBUnit automatically loads this as the appropriate config file.

How to configure Netbeans code entry point when you use mod-rewriting

I am developing a website in PHP and I am using mod-rewrite rules. I want to use the Netbeans Run Configuration (under project properties) to set code entry points that looks like http://project/news or http://project/user/12
It seems Netbeans have a problem with this and needs an entry point to a physical file like http://project/user.php?id=12
Has anyone found a good way to work around this?
I see your question is a bit old, but since it has no answer, I will give you one.
What I did to solve the problem, was to give netbeans what it wants in terms of a valid physical file, but provide my controller (index.php in this case) with the 'data' to act correctly. I pass this data using a query parameter. Using your example of project being the web site domain and user/12 as the URL, use the following in the NetBeans Run Configuration and arguments boxes. netbeans does not need the ? as it inserts that automatically, see the complete url below the input boxes
Project URL: http://project
Index File: index.php *(put your controller name here)*
Arguments: url=user/12
http://project/index.php?url=user/12
Then in your controller (index.php in this example), test for the url query param and if it exists parse it instead of the actual Server Request, as you would do normally.
I also do not want the above URL to be publically accessible. So, by using an IS_DEVELOPER define, which is true only for configured developer IP addresses, I can control who has access that special url.
If you are trying to debug specific pages, alternatively, you can set the NetBeans run configuration to:
http://project/
and debug your project, but you must run through your home page once and since the debugger is now active, just navigate to http://project/user/12 in your browser and NetBeans will debug at that entry point. I found passing through my home page every time a pain, so I use the technique above.
Hopefully that provides enough insight to work with your project. It has worked good for me and if you need more detail, just ask.
EDIT: Also, one can make the Run Configuration Project URL the complete url http://project/user/12 and leave the Index File and Arguments blank and that works too without any special code in controller. (tested in NetBeans 7.1). I think I will start using this method.