I tried to instantiate a class with other class A using setter and fetch variable to class B using getter, but it return null. I understand, since I created new instance of class, thats why it is null. What can be other approach?
public class ContextBrowser {
String browser;
public String getBrowser() {
return browser;
}
public void setBrowser(String browser) {
this.browser = browser;
}
}
public class SetBrowser{
public void setCurrentBrowser(String browser){
ContextBrowser contextBrowser = new ContextBrowser();
contextBrowser.setBrowser(browser);
}
}
public class getBrowser{
public String readBrowser(){
ContextBrowser contextBrowser = new ContextBrowser();
return contextBrowser.getBrowser()
}
}
public class ContextBrowser {
/*
Singleton class
*/
private String browser;
private static ContextBrowser contextBrowserInstance;
// private constructor
private ContextBrowser() {}
public static ContextBrowser getContextBrowserInstance() {
if(contextBrowserInstance == null) {
contextBrowserInstance = new ContextBrowser();
}
return contextBrowserInstance;
}
public String getBrowser() {
return browser;
}
public void setBrowser(String browser) {
this.browser = browser;
}
}
public class SetBrowser{
public void setCurrentBrowser(String browser){
ContextBrowser contextBrowser = ContextBrowser.getContextBrowserInstance();
contextBrowser.setBrowser(browser);
}
}
public class GetBrowser{
public String readBrowser(){
ContextBrowser contextBrowser = ContextBrowser.getContextBrowserInstance();
return contextBrowser.getBrowser();
}
}
Improved code with thread safe class:
public class ContextBrowser {
private String browser;
private ContextBrowser() {}
public static synchronized ContextBrowser getInstance(){
return instance.get();
}
public String getBrowser() {
return browser;
}
public void setBrowser(String browser) {
this.browser = browser;
}
private static ThreadLocal<ContextBrowser> instance = new ThreadLocal() {
#Override
protected ContextBrowser initialValue() {
return new ContextBrowser();
}
};
}
Related
I am trying to convert my class to support builder in order to prettify my code, this is the code I am using and I try to define my method called addSMTPIntegration to use builder.
this is my class:
public class IntegrationsPage extends SettingsTab {
private static final By newIntegrationBth = Locators.findBy("settings_page_integrations_page_add_new_button");
private IntegrationsTable integrationsTable;
private SmtpIntegrationForm smtpIntegrationForm;
private ConfirmPopup confirmPopup;
public IntegrationsPage(DriverWrapper driver){
super(driver, "integrations",newIntegrationBth);
integrationsTable = new IntegrationsTable(driver);
smtpIntegrationForm = new SmtpIntegrationForm(driver);
confirmPopup = new ConfirmPopup(driver);
}
public void addSMTPIntegration(String name, String server, String port, String fromAddress, boolean mode, String userName, String password){
clickNewIntegrationButton();
smtpIntegrationForm.chooseIntegration(IntegrationType.SMTP);
smtpIntegrationForm.setIntegrationName(name);
smtpIntegrationForm.setIntegrationServer(server);
smtpIntegrationForm.setIntegrationPort(port);
smtpIntegrationForm.setIntegrationFromAddress(fromAddress);
smtpIntegrationForm.setIntegrationAuth(mode);
smtpIntegrationForm.setIntegrationUserName(userName);
smtpIntegrationForm.setIntegrationPassword(password);
smtpIntegrationForm.clickSaveButton();
LOG.i("SMTP configuration passed successfully");
}
private void clickNewIntegrationButton(){
clickButton(newIntegrationBth);
}
public IntegrationsRow waitIntegrationRowTable(String configurationName) {
return integrationsTable.waitRowDisplay(configurationName);
}
public boolean deleteIntegration(String integrationName) {
integrationsTable.findRow(integrationName).clickRow();
integrationsTable.delete(integrationName);
confirmPopup.clickYes();
return integrationsTable.findRow(integrationName) == null;
}
}
I am trying to define my addSMTPIntegration method to support builder in a manner that I would be able to build it in the following way:
smtpIntegrationForm.chooseIntegration(IntegrationType.SMTP).setIntegrationName(name).setIntegrationServer(server).... etc
This is my smtpIntegrationForm class:
public class SmtpIntegrationForm extends IntegrationCommonSection {
private static final By integrationServerBy = Locators.findBy("settings_page_integrations_page_integration_server_name_txt");
private static final By integrationPortBy = Locators.findBy("settings_page_integrations_page_integration_port_txt");
private static final By integrationFromAddressBy = Locators.findBy("settings_page_integrations_page_integration_from_address_txt");
SmtpIntegrationForm(DriverWrapper driver){
super(driver);
}
void setIntegrationServer(String server){
setText(integrationServerBy, server);
}
void setIntegrationPort(String port){
setText(integrationPortBy, port);
}
void setIntegrationFromAddress(String address){
setText(integrationFromAddressBy, address);
}
void chooseIntegration(IntegrationType integrationType){
clickButton(By.cssSelector("li[class~='qa_" + integrationType.value + "']"));
}
and this is IntegrationCommonSection class:
class IntegrationCommonSection extends PageElement {
private static final By integrationNameBy = Locators.findBy("settings_page_integrations_page_integration_name_txt");
private static final By integrationAuthBy = Locators.findBy("settings_page_integrations_page_integration_auth_bth");
private static final By integrationUserNameBy = Locators.findBy("settings_page_integrations_page_integration_username_txt");
private static final By integrationPasswordBy = Locators.findBy("settings_page_integrations_page_integration_password_txt");
private static final By integrationSaveBthBy = Locators.findBy("settings_page_integrations_page_integration_save_bth");
private static final By integrationTestBthBy = Locators.findBy("settings_page_integrations_page_integration_test_bth");
IntegrationCommonSection(DriverWrapper driver){
super(driver);
}
void setIntegrationName(String name){
clearAndSetCharacters(integrationNameBy, name);
}
void setIntegrationAuth(boolean mode){ //true - with auth, false - no auth
if(!isCheckBoxEnabled(integrationAuthBy) && mode) {
clickButton(integrationAuthBy);
}
}
void setIntegrationUserName(String userName){
setText(integrationUserNameBy, userName);
}
void setIntegrationPassword(String password){
setText(integrationPasswordBy, password);
}
void clickSaveButton(){
clickButton(integrationSaveBthBy);
}
void clickTestButton(){
clickButton(integrationTestBthBy);
}
}
just return "this" :
SmtpIntegrationForm setIntegrationServer(String server){
setText(integrationServerBy, server);
return this;
}
In order to do that, you need to modify the methods of SmtpIntegrationForm with the return of this object so that you can construct the statements in a builder pattern. There is nothing you can do in addSMTPIntegration() method to achieve this.
chooseIntegration(), setIntegrationName(), setIntegrationServer(), etc. methods inside SmtpIntegrationForm should have return type of SmtpIntegrationForm and the last statement in these methods should be return this; in order for you to achieve this.
Make the changes as:
public class SmtpIntegrationForm extends IntegrationCommonSection {
private static final By integrationServerBy = Locators.findBy("settings_page_integrations_page_integration_server_name_txt");
private static final By integrationPortBy = Locators.findBy("settings_page_integrations_page_integration_port_txt");
private static final By integrationFromAddressBy = Locators.findBy("settings_page_integrations_page_integration_from_address_txt");
SmtpIntegrationForm(DriverWrapper driver){
super(driver);
}
SmtpIntegrationForm setIntegrationServer(String server){
setText(integrationServerBy, server);
return this;
}
SmtpIntegrationForm setIntegrationPort(String port){
setText(integrationPortBy, port);
return this;
}
SmtpIntegrationForm setIntegrationFromAddress(String address){
setText(integrationFromAddressBy, address);
return this;
}
SmtpIntegrationForm chooseIntegration(IntegrationType integrationType){
clickButton(By.cssSelector("li[class~='qa_" + integrationType.value + "']"));
return this;
}
}
I have implemented a history mechanism for my mvp4g project. When I traverse through the pages, I can see the url also getting changed. But on reload of any page other than home page, always home page gets displayed instead of the desired page?
This is my implementation:
#History(type = HistoryConverterType.SIMPLE)
public class CustomHistoryConverter implements HistoryConverter<AppEventBus> {
private CustomEventBus eventBus;
#Override
public void convertFromToken(String historyName, String param, CustomEventBus eventBus) {
this.eventBus = eventBus;
eventBus.dispatch(historyName, param);
}
public String convertToToken(String eventName, String name) {
return name;
}
public String convertToToken(String eventName) {
return eventName;
}
public String convertToToken(String eventName, String name, String type) {
return name;
}
public boolean isCrawlable() {
return false;
}
}
and event bus related code :
#Events(startPresenter=PageOnePresenter.class,historyOnStart=true)
public interface CustomEventBus extends EventBusWithLookup {
#Start
#Event(handlers = PageOnePresenter.class)
void start();
#InitHistory
#Event(handlers = PageOnePresenter.class)
void init();
#Event(handlers = PageTwoPresenter.class, name = "page2", historyConverter = CustomHistoryConverter.class)
void getPageTwo();
#Event(handlers = PageThreePresenter.class, name = "page3", historyConverter=CustomHistoryConverter.class)
void getPageThree();
#Event(handlers=PageOnePresenter.class, name = "page1", historyConverter=CustomHistoryConverter.class)
void getPageOne();
#Event(handlers=PageOnePresenter.class)
void setPageTwo(HistoryPageTwoView view);
#Event(handlers=PageOnePresenter.class)
void setPageThree(HistoryPageThreeView view);
}
The HistoryConverter needs to be improved.
In fact, that the event has no parameter, you should return an empty string. Update the HistoryConverter that it looks like that:
#History(type = HistoryConverterType.SIMPLE)
public class CustomHistoryConverter implements HistoryConverter<AppEventBus> {
private CustomEventBus eventBus;
#Override
public void convertFromToken(String historyName, String param, CustomEventBus eventBus) {
this.eventBus = eventBus;
// TODO handle the param in cases where you have more than one parameter
eventBus.dispatch(historyName, param);
}
public String convertToToken(String eventName, String name) {
return name;
}
public String convertToToken(String eventName) {
return "";
}
public String convertToToken(String eventName, String name, String type) {
return name - "-!-" type;
}
public boolean isCrawlable() {
return false;
}
}
Hope that helps.
I have the following class:
public class RefactorMe {
private static List<Event<Apple>> mAppleEventList = new ArrayList<Event<Apple>>();
private static List<Event<Banana>> mBananaEventList = new ArrayList<Event<Banana>>();
private static List<Event<Orange>> mOrangeEventList = new ArrayList<Event<Orange>>();
public static List<Event<Apple>> getAppleList() {
return mAppleEventList;
}
public static List<Event<Banana>> getBananaEventList() {
return mBananaEventList;
}
public static List<Event<Orange> getOrangeList() {
return mOrangeEventList;
}
public static void addAppleEvent(Event<Apple> pEvent) {
mAppleEventList.add(pEvent);
}
public static void addBananaEvent(Event<Banana> pEvent) {
mBananaEventList.add(pEvent);
}
public static void addOrangeEvent(Event<Orange> pEvent) {
mOrangeEventList.add(pEvent);
}
}
I tried refactoring it using the Visitor pattern but could not get it to work because of the generics.. Is there a better way to do this?
Following on #user902383 by using the Map here is a solution for you in Java 7:
public class RefactorMe {
class Event<K> {
public K getNewObject() {
return null;
}
}
private static Map<Class<?>, List<Event<?>>> eventLists = new HashMap<>();
public static <E> List<Event<E>> getEventList(Class<E> clazz) {
return (List) eventLists.get(clazz);
}
public static <E extends Event<E>> void addEvent(Event<E> pEvent) {
Class<E> key = (Class<E>) pEvent.getNewObject().getClass();
List<Event<?>> events = eventLists.get(key);
if (events == null) {
events = new ArrayList<>();
eventLists.put(key, events);
}
events.add(pEvent);
}
}
I have a POJO that looks more or less like this:
public class Action {
private String eventId;
private List<ActionArgument> arguments;
//action to perform when this action is done
private List<Action> onCompleteActions;
public Action() {
}
public Action(String eventId, List<ActionArgument> arguments, List<Action> onCompleteActions) {
this.eventId = eventId;
this.arguments = arguments;
this.onCompleteActions = onCompleteActions;
}
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public List<ActionArgument> getArguments() {
return arguments;
}
public void setArguments(List<ActionArgument> arguments) {
this.arguments = arguments;
}
public List<Action> getOnCompleteActions() {
return onCompleteActions;
}
public void setOnCompleteAction(List<Action> onCompleteActions) {
this.onCompleteActions = onCompleteActions;
}
}
and I have an extending class that looks like this:
public class UserDefinedAction extends Action {
//for reordering actions with the default actions
private String doBefore;
private String doAfter;
private String doAfterComplete;
public String getDoBefore() {
return doBefore;
}
public void setDoBefore(String doBefore) {
this.doBefore = doBefore;
}
public String getDoAfter() {
return doAfter;
}
public void setDoAfter(String doAfter) {
this.doAfter = doAfter;
}
public String getDoAfterComplete() {
return doAfterComplete;
}
public void setDoAfterComplete(String doAfterComplete) {
this.doAfterComplete = doAfterComplete;
}
}
Elsewhere I have a service I would like to do this:
...
UserDefinedAction udAction = new UserDefinedAction();
udAction.setOnCompleteAction(new ArrayList<UserDefinedAction>());
I thought this should work because a UserDefinedAction IS an Action because its extending it right?
List<UserDefinedAction> is not a subclass of List<Action> even if UserDefinedAction extends Action. In order you can pass a List<UserDefinedAction> to your service, change the UserDefinedAction#setOnCompleteAction method to receive a List<? extends Action>, now you can pass a new ArrayList<UserDefinedAction>().
More info:
Generics: Wildcards
Your UserDefinedAction may be an Action, but a List<Subclass> is not a List<Superclass>. As you have defined it, your setOnCompleteAction method must take a List<Action>, so it cannot accept a List<UserDefinedAction>.
I want to make a unit test suite of the same object with same variable but different values. However if the object get the same name (created by this.setName("testlaunch"); (we must have the name of a method tested by JUnit), it runs only one test.
If i don't write this.setName("testlaunch"); it complains saying junit.framework.AssertionFailedError: TestCase.fName cannot be null.
I don't know what to do...
public class LanceurRegleGestion extends TestSuite
{
public static Test suite()
{
Class maClasse = null;
TestSuite suite = new TestSuite();
String filtre = ".*.xml";
// on compile le pattern pour l'expression réguliere
Pattern p = Pattern.compile(filtre);
String path = "D:/Documents/workspace/Solipsisme/src/ReglesGestion/XML/";
// on liste les fichiers du repertoire
String [] u = new File(path).list();
// on parcours la liste de fichier
System.out.println("Initialisation");
for (int i=0; i
et le code de l'objet serialisé
public class Application extends TestCase {
private String nomappli;
private String id2_1;
private String id3_1;
private String id4_1;
private String id2_2;
private String id3_2;
private String id4_2;
private String id5_2;
private String id6_2;
private String id7_2;
private String id8_2;
private String id9_2;
private String id2_3;
private String id3_3;
private String id4_3;
private String id2_4;
private String id3_4;
private String id4_4;
private String id2_5;
private String id3_5;
private String id4_5;
private String id5_5;
private String id6_5;
private String id7_5;
private static Selenium selenium;
public Application(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5)
{
this.setName("testlaunch");
this.nomappli = nomappli;
this.id2_1 = id2_1;
this.id3_1 = id3_1;
this.id4_1 = id4_1;
this.id2_2 = id2_2;
this.id3_2 = id3_2;
this.id4_2 = id4_2;
this.id5_2 = id5_2;
this.id6_2 = id6_2;
this.id7_2 = id7_2;
this.id8_2 = id8_2;
this.id9_2 = id9_2;
this.id2_3 = id2_3;
this.id3_3 = id3_3;
this.id4_3 = id4_3;
this.id2_4 = id2_4;
this.id3_4 = id3_4;
this.id4_4 = id4_4;
this.id2_5 = id2_5;
this.id3_5 = id3_5;
this.id4_5 = id4_5;
this.id5_5 = id5_5;
this.id6_5 = id6_5;
this.id7_5 = id7_5;
}
public Application(){
}
public String toString()
{
return getNomappli();
}
public void setNomappli(String nomappli)
{
this.nomappli = nomappli;
}
public String getNomappli()
{
return this.nomappli;
}
public void setId2_1(String id2_1)
{
this.id2_1 = id2_1;
}
public String getId2_1()
{
return this.id2_1;
}
public void setId3_1(String id3_1)
{
this.id3_1 = id3_1;
}
public String getId3_1()
{
return this.id3_1;
}
public void setId4_1(String id4_1)
{
this.id4_1 = id4_1;
}
public String getId4_1()
{
return this.id4_1;
}
public void setId2_2(String id2_2)
{
this.id2_2 = id2_2;
}
public String getId2_2()
{
return this.id2_2;
}
public void setId3_2(String id3_2)
{
this.id3_2 = id3_2;
}
public String getId3_2()
{
return this.id3_2;
}
public void setId4_2(String id4_2)
{
this.id4_2 = id4_2;
}
public String getId4_2()
{
return this.id4_2;
}
public void setId5_2(String id5_2)
{
this.id5_2 = id5_2;
}
public String getId5_2()
{
return this.id5_2;
}
public void setId6_2(String id6_2)
{
this.id6_2 = id6_2;
}
public String getId6_2()
{
return this.id6_2;
}
public void setId7_2(String id7_2)
{
this.id7_2 = id7_2;
}
public String getId7_2()
{
return this.id7_2;
}
public void setId8_2(String id8_2)
{
this.id8_2 = id8_2;
}
public String getId8_2()
{
return this.id8_2;
}
public void setId9_2(String id9_2)
{
this.id9_2 = id9_2;
}
public String getId9_2()
{
return this.id9_2;
}
public void setId2_3(String id2_3)
{
this.id2_3 = id2_3;
}
public String getId2_3()
{
return this.id2_3;
}
public void setId3_3(String id3_3)
{
this.id3_3 = id3_3;
}
public String getId3_3()
{
return this.id3_3;
}
public void setId4_3(String id4_3)
{
this.id4_3 = id4_3;
}
public String getId4_3()
{
return this.id4_3;
}
public void setId2_4(String id2_4)
{
this.id2_4 = id2_4;
}
public String getId2_4()
{
return this.id2_4;
}
public void setId3_4(String id3_4)
{
this.id3_4 = id3_4;
}
public String getId3_4()
{
return this.id3_4;
}
public void setId4_4(String id4_4)
{
this.id4_4 = id4_4;
}
public String getId4_4()
{
return this.id4_4;
}
public void setId2_5(String id2_5)
{
this.id2_5 = id2_5;
}
public String getId2_5()
{
return this.id2_5;
}
public void setId3_5( String id3_5)
{
this.id3_5 = id3_5;
}
public String getId3_5()
{
return this.id3_5;
}
public void setId4_5(String id4_5)
{
this.id4_5 = id4_5;
}
public String getId4_5()
{
return this.id4_5;
}
public void setId5_5(String id5_5)
{
this.id5_5 = id5_5;
}
public String getId5_5()
{
return this.id5_5;
}
public void setId6_5(String id6_5)
{
this.id6_5 = id6_5;
}
public String getId6_5()
{
return this.id6_5;
}
public void setId7_5(String id7_5)
{
this.id7_5 = id7_5;
}
public String getId7_5()
{
return this.id7_5;
}
public void setSelenium(Selenium selenium)
{
this.selenium = selenium;
}
public Selenium getSelenium()
{
return this.selenium;
}
public final static void login()
{
selenium.open("apj/ident");
selenium.type("username", "hsuzumiya-cp");
selenium.type("password", "1");
selenium.click("enterButton");
selenium.waitForPageToLoad("9999999");
}
public void testlaunch()
{
generique(this.nomappli,this.id2_1,this.id3_1,this.id4_1,this.id2_2,this.id3_2,this.id4_2,this.id5_2,this.id6_2,this.id7_2,this.id8_2,this.id9_2,this.id2_3,this.id3_3,this.id4_3,this.id2_4,this.id3_4,this.id4_4,this.id2_5,this.id3_5,this.id4_5,this.id5_5,this.id6_5,this.id7_5);
}
public void setUp() throws Exception
{
System.out.println("Initialisation");
selenium = new DefaultSelenium("127.0.0.1",4444,"*iexplore", "http://hsuzumiya/");
selenium.start();
selenium.setTimeout("90000");
selenium.setSpeed("500");
login();
}
public void generique(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,
String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,
String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5
)
{
System.out.println(nomappli);
selenium.click("valider");
selenium.waitForPageToLoad("30000");
selenium.click("validertout");
}
public final void tearDown() throws Exception
{
System.out.println("Killing session");
selenium.stop();
}
}
Being new to junit, I stumbled upon this question hoping to solve a problem I had getting the same message. Through further research, I found that it is necessary to pass the name of the test function you want to invoke through addTest to the constructor of the test case class. A simple (and useless, other than illustration) example follows:
JunitTestCases.java
import junit.framework.TestCase;
public class JunitTestCases extends TestCase {
public JunitTestCases(String fnName) {
super(fnName);
}
public void testA() {
assertTrue("assertTrue failed", true);
}
}
JunitTestSuite.java:
import junit.framework.*;
public class JunitTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JunitTestCases("testA"));
return suite;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
When I compiled with:
javac -cp .:path/to/junit-X.X.X.jar JunitTestSuite.java
and ran with
java -cp .:path/to/junit-X.X.X.jar JunitTestSuite
this worked with no errors, with junit giving me an OK message.