How to configure environment variable for particular cucumber feature file in eclipse? - cucumber

I am using Cucumber in Java with JUnit and building the project with Maven. I want to use environment variable in the project that should access my entire project. Is there any way to do it?

Your problem title says for a particular cucumber feature file and description of your question says in your whole projects the environment variable should be accessible.
Can you confirm what you want for a particular feature file or for whole project?
In case if you want your environment variables to be accessed in whole project with static value than you can do following things:
Create Single ton class and getter and setter methods to access those environment variables as below
public class GlobalClass {
private static GlobalClass ourInstance;
private HashMap<String, Object> sessionState = new HashMap<>();
public static GlobalClass getInstance() {
if (ourInstance == null) {
return ourInstance = new ThisRun();
}
return ourInstance;
}
public void add(String key, Object value) {
sessionState.put(key, value);
}
public String getAsString(String key) {
return sessionState.get(key).toString();
}
}
so you can add the key and values in this single ton class and can access anywhere in the project

Related

FusedLocationProviderClient, This method should only be accessed from tests or within package private scope

I have a class in my project
public class LocateMe {
public void locateMe(Activity activity) {
mLocationClient = new FusedLocationProviderClient(activity);
.....
that uses FusedLocationProviderClient but I keep getting the warning "This method should only be accessed from tests or within package private scope" about the line new FusedLocationProviderClient(activity). I tried to tweak and change things without success.
Use the following way to initialize it instead:
mLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
or use whatever context you got.

Can I register Orchard resources in an HtmlHelper?

I am aware that this violates the MVC principle and best practices.
I have a whole bunch of custom Angular components that each take a whole bunch of optional parameters and each require a different remote stylesheet and javascript file. I would like to render these with an HtmlHelper without having to manually include the right resources everywhere I use them.
I was hoping that this would do the trick but it doesn't
public static class HtmlExtensions
{
private static IResourceManager _resourceManager;
// Executed in the Activated method of an OrchardShellEvents implementation
public static void SetResourceManager(IResourceManager resourceManager)
{
_resourceManager = resourceManager;
}
public static MvcHtmlString Angular(this HtmlHelper helper, CustomAngularComponent component)
{
// Require the resources
var _styleRegister = new ResourceRegister(helper.ViewDataContainer, _resourceManager, "Style");
var _scriptRegister = new ResourceRegister(helper.ViewDataContainer, _resourceManager, "Script");
_styleRegister.Require(component.StyleSheet).AtHead();
if (!string.IsNullOrEmpty(component.Script))
{
_scriptRegister.Require(component.Script).AtFoot();
}
// Create tag
var tag = new TagBuilder(component.Tag);
tag.MergeAttributes(component.Parameters);
return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}
}
I could use an arbitrary Shape as a helper, something like:
#Display(New.Angular(Model: new CustomAngularComponent(...)))
but a helper with strongly typed parameters feels a lot better.
This comment on another Orchard question tipped me off. As it turns out, simply injecting Work<IResourceManager> and using it directly was enough to make my setup work. Please note that this does not conform to best MVC practices, by doing this I sacrifice maintainability in favor of readability.
public static class HtmlExtensions
{
private static Work<IResourceManager> _resourceManager;
// Executed in the Activated method of an OrchardShellEvents implementation
public static void SetResourceManager(Work<IResourceManager> resourceManager)
{
_resourceManager = resourceManager;
}
public static MvcHtmlString Angular(this HtmlHelper helper, CustomAngularComponent component)
{
// Require the resources
_resourceManager.Value.Require("stylesheet", component.StyleSheet).AtHead();
if (!string.IsNullOrEmpty(component.Script))
{
_resourceManager.Value.Require("script", component.Script).AtFoot();
}
// Create tag
var tag = new TagBuilder(component.Tag);
tag.MergeAttributes(component.Parameters);
return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}
}

How to pass variable values between steps in Cucumber Java?

I have a variable and I want to pass this variable across all the steps.
Anyone can suggest with an code snippet example please on how to pass a variable value between the steps please.
Any help will be highly appreciated.
In Cucumber for Java (cucumber-jvm) the intended way of sharing data between steps is to use a dependency integration (DI) container - several of which have been integrated with Cucumber.
The method in which you use DI varies slightly from container to container, but here's an example using PicoContainer:
// MySharedData.java
public class MySharedData {
public String stringData;
}
// SomeStepDefs.java
public class SomeStepDefs {
private MySharedData sharedData;
public SomeStepDefs(MySharedData sharedData) {
this.sharedData = sharedData;
}
// StepDefs omitted
}
// MoreStepDefs.java
public class MoreStepDefs {
private MySharedData sharedData;
public MoreStepDefs(MySharedData sharedData) {
this.sharedData = sharedData;
}
// StepDefs omitted
}
The DI container will ensure that a single instance of MySharedData is created for each scenario and is passed to every step definition class that requires it. The benefit of this approach is that Cucumber ensures that no shared state leaks between scenarios, because the injected dependency is created afresh for each scenario.
The example above uses constructor injection (so the injected dependency is specified by a constructor parameter) but other DI containers also support other injection mechanisms, such as Spring's #Autowired.
To get Cucumber to use DI you'll need to choose one (and only one) of the DI integrations and include it on your classpath (or in your POM). The choice is between:
PicoContainer (cucumber-picocontainer.jar)
Guice (cucumber-guice.jar)
Weld (cucumber-weld.jar)
Spring (cucumber-spring.jar)
OpenEJB (cucumber-openejb.jar)
You'll also need to install the selected DI container itself, because the Cucumber jars only provide the integration between Cucumber and the DI container.
private static String myName = null;
#Given("I have a cucumber step")
public void i_have_a_cucumber_step() throws Throwable {
myName = "Stackoverflow"
}
#Given("^I have (\\d+) (.*) in my basket$")
public void i_have_in_my_basket(int number, String veg) throws Throwable {
System.out.println(myName));
}

How to force the order of Installer Execution

I have been building a new .NET solu­tion with Cas­tle per­form­ing my DI.
Its now at the stage where i would like to con­trol the order in which my installers run. I have built indi­vid­ual classes which implement IWind­sorIn­staller to han­dle my core types — eg IRepos­i­tory, IMap­per and ISer­vice to name a few.
I see that its suggested i implement my own Installer­Fac­tory (guessing i just override Select) in this class.
Then use this new factory in my call to:
FromAssembly.InDirectory(new AssemblyFilter("bin loca­tion"));
My ques­tion — when over­rid­ing the save method — what is the best way to force the order of my installers.
I know its already solved but I couldn't find any example on how to actually implement the InstallerFactory so here's a solution if anyone is googling for it.
How to use:
[InstallerPriority(0)]
public class ImportantInstallerToRunFirst : IWindsorInstaller
{
public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
// do registrations
}
}
Just add the InstallerPriority attribute with a priority to your "install-order-sensitive" classes. Installers will be sorted by ascending. Installers without priority will default to 100.
How to implement:
public class WindsorBootstrap : InstallerFactory
{
public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes)
{
var retval = installerTypes.OrderBy(x => this.GetPriority(x));
return retval;
}
private int GetPriority(Type type)
{
var attribute = type.GetCustomAttributes(typeof(InstallerPriorityAttribute), false).FirstOrDefault() as InstallerPriorityAttribute;
return attribute != null ? attribute.Priority : InstallerPriorityAttribute.DefaultPriority;
}
}
[AttributeUsage(AttributeTargets.Class)]
public sealed class InstallerPriorityAttribute : Attribute
{
public const int DefaultPriority = 100;
public int Priority { get; private set; }
public InstallerPriorityAttribute(int priority)
{
this.Priority = priority;
}
}
When starting application, global.asax etc:
container.Install(FromAssembly.This(new WindsorBootstrap()));
You can call your installers in the order they need to be instantiated in Global.asax.cs or e.g. in a Bootstrapper class, which is called from Global.asax.cs.
IWindsorContainer container = new WindsorContainer()
.Install(
new LoggerInstaller() // No dependencies
, new PersistenceInstaller() // --""--
, new RepositoriesInstaller() // Depends on Persistence
, new ServicesInstaller() // Depends on Repositories
, new ControllersInstaller() // Depends on Services
);
They are instantiated in this order, and you can add a breakpoint after and check the container for "Potentially misconfigured components".
If there are any, check their Status->details, if not, it's the correct order.
This solution is quick and easy, the documentation mentions using a InstallerFactory Class for tighter control over your installers so if you have a ton of installers the other solution may fit better. (Using code as convention should not require tons of installers?)
http://docs.castleproject.org/Windsor.Installers.ashx#codeInstallerFactorycode_class_4
In the end i had to use InstallerFactory and implement the ordering rules as suggested previously by returning the IEnumerable<Type> with my specific order

Tapestry 4: Asset Cache Control?

I use Tapestry 4, and whenever we push a release that changes any assets (image, style sheet, JS library), we get problems because users still have the old version of the asset in their browser cache. I'd like to set up some easy way to allow caching, but force a new asset download when we update the application. Simply disallowing caching entirely for assets is not an acceptable solution.
I couldn't see any existing mechanism for doing this, but I was figuring that there might be some way to tell Tapestry to add the build number to the URL, something like this:
http://www.test.com/path/to/the/asset/asset.jpg?12345
That way, every new build would make it look like a different asset to the end user.
Does Tapestry provide an easy way to solve the cache problem that I'm not aware of? If not, how would one go about modifying the URL generated by Tapestry? And how would the code responsible for doing that get the build number? (I could get the build number into a Spring bean, for example, but how would the new URL building mechanism get at it?)
After stewing about this problem for a long time, I eventually solved it myself. This solution assumes you have the tapestry-spring library in your project.
In my case, I have a Spring bean that contains some of my application's global properties:
package myapp;
public class AppProperties {
private String build;
public String getBuild() {
return build;
}
public void setBuild(String build) {
this.build = build;
}
// other properties
}
Declare this bean in your Spring configuration:
<bean id="appProperties" class="myapp.AppProperties">
<property name="build" value="#BUILD_NUMBER#"/>
</bean>
You can set up your Ant build script to replace #BUILD_NUMBER# with the actual number (see the Copy task in the Ant manual for details).
Now create a class that will wrap IAssets and tack the build number onto the URL:
package myapp;
import java.io.InputStream;
import org.apache.hivemind.Location;
import org.apache.hivemind.Resource;
import org.apache.tapestry.IAsset;
public class BuildAwareAssetWrapper implements IAsset {
private IAsset wrapped;
private String build;
public BuildAwareAssetWrapper(IAsset wrapped, String build) {
this.wrapped = wrapped;
this.build = build;
}
public String buildURL() {
return addParam(wrapped.buildURL(), "build", build);
}
public InputStream getResourceAsStream() {
return wrapped.getResourceAsStream();
}
public Resource getResourceLocation() {
return wrapped.getResourceLocation();
}
public Location getLocation() {
return wrapped.getLocation();
}
private static String addParam(String url, String name, String value) {
if (url == null) url = "";
char sep = url.contains("?") ? '&' : '?';
return url + sep + name + '=' + value;
}
}
Next, we need to make Tapestry wrap all assets with our wrapper. The AssetSourceImpl class is responsible for providing IAsset instances to Tapestry. We'll extend this class and override the findAsset() method so that we can wrap the created assets with the wrapper class:
package myapp;
import java.util.Locale;
import org.apache.hivemind.Location;
import org.apache.hivemind.Resource;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.asset.AssetSourceImpl;
public class BuildAwareAssetSourceImpl extends AssetSourceImpl {
private AppProperties props;
#Override
public IAsset findAsset(Resource base, String path, Locale locale, Location location) {
IAsset asset = super.findAsset(base, path, locale, location);
return new BuildAwareAssetWrapper(asset, props.getBuild());
}
public void setAppProperties(AppProperties props) {
this.props = props;
}
}
Notice that the implementation has a setter which can accept our Spring bean. The last step is to get Tapestry to use BuildAwareAssetSourceImpl to create assets instead of AssetSourceImpl. We do this by overriding the corresponding service point in hivemodule.xml:
<!-- Custom asset source -->
<implementation service-id="tapestry.asset.AssetSource">
<invoke-factory service-id="hivemind.BuilderFactory" model="singleton">
<construct class="myapp.BuildAwareAssetSourceImpl">
<set-object property="appProperties" value="spring:appProperties"/>
<set-configuration property="contributions" configuration-id="tapestry.asset.AssetFactories"/>
<set-service property="lookupAssetFactory" service-id="tapestry.asset.LookupAssetFactory"/>
<set-service property="defaultAssetFactory" service-id="tapestry.asset.DefaultAssetFactory"/>
</construct>
</invoke-factory>
</implementation>
That's it. If you run your application and view the source for any page that uses an asset, you will see that the URL will have the new build parameter on it.

Resources