Configure Ignite logger and appender using java - ignite

I want to configure the ignite log4j2 configuration programatically instead of xml.
How to configure ignite log4j2 configuration using java? Can anyone help?

Unfortunately built-it Ignite integration doesn't support pure programmatic way of configuration at the moment.
But in fact you're able to use slf4j logging facade backed by the log4j2 under the hood. In this case you'll have some sort of a full power over logging configuration.
In order to achieve that you should add log4j2 dependencies manually as well as ignite-slf4j dependency. In the meanwhile you need log4j2 slf4j binding to allow Ignite logging facilities transfer calls to the log4j2.
For instance if you use maven then your pom.xml should contain:
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-slf4j</artifactId>
<version>${ignite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
After that you'll be able to follow official insеructions to configure that stuff. For example you can try adding a class that extends org.apache.logging.log4j.core.config.ConfigurationFactory to your classpath. It could look somehow like that:
import java.net.URI;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Order;
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
#Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
#Order(50)
public class CustomConfigurationFactory extends ConfigurationFactory {
static Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) {
builder.setConfigurationName(name);
builder.setStatusLevel(Level.ERROR);
builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).
addAttribute("level", Level.DEBUG));
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").
addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY,
Filter.Result.NEUTRAL).addAttribute("marker", "FLOW"));
builder.add(appenderBuilder);
builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).
add(builder.newAppenderRef("Stdout")).
addAttribute("additivity", false));
builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout")));
return builder.build();
}
#Override
public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
return getConfiguration(loggerContext, source.toString(), null);
}
#Override
public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final URI configLocation) {
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder();
return createConfiguration(name, builder);
}
#Override
protected String[] getSupportedTypes() {
return new String[] {"*"};
}
}
It would be possible to make logging customization there.

Related

Log custom text to Cucumber JVM or Extent Reports

Please guide me in logging custom text which are there inside a method into Cucumber JVM (Masterthought) or Extent Reports.
I am using Cucumber Junit BDD Framework and have the below dependencies in my POM file -
<!-- Cucumber JVM Report -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>5.1.0</version>
</dependency>
<!-- Extent Report -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports-cucumber4-adapter</artifactId>
<version>1.2.1</version>
</dependency>
And my Runner file is as below -
#CucumberOptions(
tags = {"#SANITY,#REGRESSION,#E2E"},
features = "src/test/resources/cta-features/features/",
plugin = {
"json:target/cucumber-reports/cucumber.json", "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
"pretty"
},
glue = {
"e2e.steps",
"e2e.hooks"
})
#RunWith(Cucumber.class)
public class TestRunner {
#BeforeClass
public static void init() throws Exception {
}
#AfterClass
public static void generateReport() throws Exception {
File reportOutputDirectory = new File("target");
List<String> jsonFiles = new ArrayList<>();
jsonFiles.add("target/cucumber-reports/cucumber.json");
String buildNumber = "1";
String projectName = "Project";
Configuration configuration = new Configuration(reportOutputDirectory, projectName);
// optional configuration - check javadoc for details
configuration.addPresentationModes(PresentationMode.RUN_WITH_JENKINS);
// do not make scenario failed when step has status SKIPPED
configuration.setNotFailingStatuses(Collections.singleton(Status.SKIPPED));
configuration.setBuildNumber(buildNumber);
// addidtional metadata presented on main page
configuration.addClassifications("Platform", "Windows");
configuration.addClassifications("Browser", "Chrome");
configuration.addClassifications("Branch", "release/1.0");
ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration);
Reportable result = reportBuilder.generateReports();
// and here validate 'result' to decide what to do if report has failed
}
I am able to successfully generate report in both master thought and Extent which will contain Feature level statements and Step Level statements (I have used scenario.write for this). But my client wants one more level deeper where he can get info as to which link or button was clicked etc which i am currently publishing to my console using Log4j
Example :
Log.info("Random Button or Link was clicked and Operation is successful")
My clients wants this to be integrated into the report (In Either Masterthought or Extent). Is there a way to do this ?
Extent cucumber adapter provides addtestlog to log additional steps .
Please check below example -
ExtentCucumberAdapter.addAddTestStepLog(BTN.getText());
As per your Pom.xml file you are using adapter 4 if you cannot find method try to switch into cucumber adapter 6 and other essential dependency of cucumber 6. That will surely help you :)

IntelliJ + Corda: Error:(6, 30) java: package net.corda.testing.core does not exist

I'm a newbie using IntelliJ + Corda + Kotlin.
I just have installed IntelliJ IDEA, cloned the Corda Git Repository BootCamp (https://github.com/corda/bootcamp-cordapp), and the first thing that IntelliJ shows me are some Errors, claiming what I think are Kotlin packages and classes.
This are some of the Error messages:
Error:(6, 30) java: package net.corda.testing.core does not exist
Error:(12, 37) java: cannot find symbol
...symbol: class TestIdentity
...location: class test.java.java_bootcamp.StateTests
package test.java.java_bootcamp;
import net.corda.core.contracts.ContractState;
import net.corda.core.identity.CordaX500Name;
import net.corda.core.identity.Party;
import net.corda.testing.core.TestIdentity;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class StateTests {
private final Party alice = new TestIdentity(new CordaX500Name("Alice", "", "GB")).getParty();
private final Party bob = new TestIdentity(new CordaX500Name("Bob", "", "GB")).getParty();
#Test
public void tokenStateHasIssuerOwnerAndAmountParamsOfCorrectTypeInConstructor() {
new TokenState(alice, bob, 1);
}
#Test
public void tokenStateImplementsContractState() {
assert(new TokenState(alice, bob, 1) instanceof ContractState);
}
}
What am I doing wrong or missing?
BTW, I'm using a Mac
After a 4 hour pain, I discovered that I need to add two JAR's, that solved my problem (for now).
These were them, from Maven Repository:
<!-- https://mvnrepository.com/artifact/net.corda/corda-test-utils -->
<dependency>
<groupId>net.corda</groupId>
<artifactId>corda-test-utils</artifactId>
<version>3.3-corda</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.corda/corda-node-driver -->
<dependency>
<groupId>net.corda</groupId>
<artifactId>corda-node-driver</artifactId>
<version>3.3-corda</version>
</dependency>

Migrating from Selenium RC to Webdriver

I'm trying to migrate to webdriver, as it is faster. How do I do this?
Currently I'm using the setUp() command for selenium. Do I need to add any new jar files for the webdriver?
I 'd like to describe in common features approach I use on my project.
I'm using selenium webDriver. Working on java.
IDE: IDEA.
build manager: maven.
So I got POM.xml file.
So I simply add there depencdencies in POM xml:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency> <groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.24.1</version>
</dependency>
On building the project maven will get needed jars automatically.
In my project I got BaseSeleniumTest.java: with the following:
public class BaseSeleniumTest extends SeleneseTestBase {
static WebDriver driver;
#BeforeClass
public static void firefoxSetUp() throws MalformedURLException {
driver = new FirefoxDriver();
}
#Before
public void homePageRefresh() throws IOException {
driver.get("login.base.url");
}
#AfterClass
public static void closeFirefox(){
driver.quit();
}
...
}
And have some other classes inherinting from BaseSekeniumTest.java
where I describe pack of my selenium tests annotated by #Test
That's kind of structure on my project.
Hope this helps you.

New to Selenium - cannot access RemoteWebDriver error

I'm new to Selenium and I have tried to setup my first Selenium test using IntelliJ and Selenium 2. I have followed the tutorials on the official Selenium site but I get this error:
cannot access org.openqa.selenium.remote.RemoteWebDriver
class file for org.openqa.selenium.remote.RemoteWebDriver not found
This is the tutorial I am using - http://seleniumhq.org/docs/03_webdriver.html#chapter03-reference
This is the location for the Maven setup I followed - http://seleniumhq.org/docs/appendix_installing_java_driver_Sel20_via_maven.html#importing-maven-into-intellij-reference
Here is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MyAppHomePageTest {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
}
}
The error occurs when I try to create a FirefoxDriver instance. Do I need to have the Selenium server as I thought this is no longer required.
I have tried the same in Eclipse and received the same error.
Thank you
You should use this Maven dependency :
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.1.0</version>
</dependency>
According to this picture, you don't need a selenium-server dependency:
source : http://seleniumhq.org/download/maven.html

How to conditionalize GUI tests using Netbeans/Maven vs maven on command line invocation

I'd like to have a single project pom but have my GUI tests always run when I'm invoking JUnit on Netbeans, but have them conditional (on an environment variable?) when building on the command line (usually for production build - on a headless machine, but sometimes just for build speed).
I don't mind instrumenting my JUnit tests for this, as I already have to set up my GUI test infrastructure, but how do I conditionalize my pom!
Netbeans 6.5 with Maven plugin.
Any ideas how I can accomplish this?
Ilane
The Surefire plugin can specify which tests to run.
You could specify different sets to run for different profiles, and select your profile using -P{profilename}.
One solution would be to use a profile (that could be indeed activated by an environment variable) and to exclude GUI tests from the Maven Surefire Plugin configuration using excludes in this profile, based on a naming convention. Something like this:
<profile>
<id>headless</id>
<activation>
<property>
<name>env.FOO</name>
<value>foo</value>
</property>
</activation>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<excludes>
<exclude>**/*$*</exclude><!-- to exclude inner classes -->
<exclude>**/gui/**</exclude>
</excludes>
</configuration>
</plugin>
...
</plugins>
</build>
</profile>
Another option would be to use TestNG and to leverage the excludeGroups parameter.
To achieve the desired behavior in my Maven project on Netbeans, I set up a profile defining an environment variable in my project's pom and modified the Test file action in my project's properties to activate my new profile. In this way I could check the enviroment variable in my tests. (This could have been done similarly with a system property.)
To keep from having to add a check to each and every GUI test, however, I found that I could add a JUnit filter, which would automatically ignore the UI tests in the case where I didn't want to run them and put the number of skipped tests in the test result line.
Here what my profile looks like:
<profile>
<id>test-gui</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
<excludes>
<exclude>**/TestSuite.java</exclude>
</excludes>
<environmentVariables>
<GUI_TEST>true</GUI_TEST>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Here's a description of how to update the Netbeans Action:
(Right click on project in Projects window)->Properies
In Categories box, select 'Actions'
In Actions box, select 'Test file'
In Activate Profiles textfield, enter 'test-gui'
Click 'OK' button to save.
To add the JUnit filter (and the documentation was sparse, so I may not have done this in the most effective manner), I subclassed the default test runner, TestClassRunner and had it call my Filter, which checks the test name and the environment variable. To get JUnit to call my class, I annotated the test class.
public class GUITestClassRunner extends TestClassRunner {
public GUITestClassRunner(Class klazz) throws InitializationError {
super(klazz);
}
public GUITestClassRunner(Class klazz, Runner runner)
throws InitializationError, NoTestsRemainException {
super(klazz, runner);
}
#Override
public void run(RunNotifier notifier) {
if (getDescription().testCount() > 0) {
try {
filter(new GUIFilter(notifier));
} catch (NoTestsRemainException ex) {
Description description = getDescription();
notifier.fireTestStarted(description);
notifier.fireTestIgnored(description);
notifier.fireTestFinished(description);
return;
}
}
super.run(notifier);
}
class GUIFilter extends Filter {
private boolean isGUI = false;
private RunNotifier notifier;
public GUIFilter(RunNotifier notifier) {
this.notifier = notifier;
isGUI = UI.isGUITestEnvironment();
}
#Override
public boolean shouldRun(Description desc) {
if (!isGUI && UI.isGUITest(desc.getDisplayName())) {
Description description = getDescription();
notifier.fireTestStarted(description);
notifier.fireTestIgnored(description);
notifier.fireTestFinished(description);
return false;
}
return true;
}
#Override
public String describe() {
return "all tests except GUI tests if headless";
}
}
}
To call this runner, the test class needs to be annotated with:
#RunWith(GUITestClassRunner.class)
public class MyJUnitTestClass
{
....
#Test
public void testAlpha() {...}
#Test
public void testBeta_UI() {...}
}
So, now, from Netbeans, I just run my unit test class and the GUI tests automatically run. I can run from the command line with no environment variables set or GUI_TEST set to false, and my GUI tests are skipped, or I can run from the command line with GUI_TEST set to true, or use mvn -Ptest-gui and all my GUI tests run.
My sincerest thanks to Brian and Pascal for pointing me in the right direction.
Ilane