Set openejb javaagent in code? - eclipse

I'm currently writing integration tests for a Java EE application and use openejb/openjpa.
But as I'm using CMP I have to use a javaagent to enhance my classes. In maven I can configure my surefire plugin to do this enhancement, or better set the agent as vm parameter to the test.
But as I'm currently developing I like to run my tests quite often in eclipse. But there I don't want to set the agent all the time via
java -javaagent:openejb-javaagent-4.6.0.jar _\[other params...](other-params....html)
Does someone have a useful solution for this problem?

For testing in Eclipse purposes, I always create a variable that can be used in the VM arguments section of your Run Configuration. This way all you need to specify is something like ${agent} (or whatever you call it) rather than the full javaagent string.

Mhh but not sure, looking at my properties, my classes should be enhanced at runtime:
#Before
public void startupContainer() throws NamingException {
Properties p = new Properties();
p.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
p.put("openjpa.jdbc.DBDictionary", "hsql(SupportsSelectForUpdate=true)");
p.put("openejb.embedded.initialcontext.close", "destroy");
p.put("openjpa.RuntimeUnenhancedClasses", "supported");
p.put("openjpa.DynamicEnhancementAgent", "true");
p.put("javax.persistence.lock.timeout", "0");
p.put("openejb.log.factory", "slf4j");
ejbContainer = EJBContainer.createEJBContainer(p);
}

Related

Vertx multiple event loops

I'm a beginner with Vertx and I'm using this link as the starter code.
However, there is no main file and I don't see how the MainVerticle is deployed. I would like to set some deployment options for the MainVerticle but since there is no main file; how would I do that?
I recommend this link for you to improve your vert.x knowledge from zero to hero.You will be able to make a crud application using this repository.
https://github.com/saranglohar/vertx-crud-operations-demo/tree/master/src/main/java/com/vertx/student/crud
The two common ways to do that :
build a fat jar using the maven shade plugin or exec plugin and the io.vertx.core.Launcher class as main class, according to this documentation. Then you will be able to run your fat jar with a java -jar command.
My favorite : use the vertx command line (like you would do with nodejs for example).
I'd prefer the second solution in order to avoid the duplication of the vert.x core library in your filesystem which can have a real cost when you have to run bunch of vert.x microservices.
Afterwards to go further you can encapsulate that with a resilient services orchestrator like systemd or even docker+kubernetes & co.
You can use main() very easily:
public class Application {
public static void main(String args[]) {
Vertx vertx = Vertx.vertx()
vertx.deployVerticle(new MainVerticle());
}
}

How to make an application to work with any Java versions?

I have created a Java project application in eclipse and i am using the JRE System Library[JavaSE-1.7].
when i make an executable jar file of the application then test it on different computers which have different Java versions. The problem is that the jar file only works on some of the computers because of the java version.
my question is: is there away to make the application to check the computer see which java version it has and just use that version instead of the java version that was used to implement the project? in other words can you make an application which is not java version dependent?
any suggestion is of great help.
thanks
Thanks for all the suggestions and comments. But i think i'll just stick with the java 7 version and let the user know that they need java 7 to run it.
No, there's no way to check that, not in the way I think you mean.
In fact, on the one hand you have the Java version in which you compile the class (which you can choose), and on the other hand the Java version (of the JVM) in which your application runs (which you cannot choose).
Well, I do believe there's a way to check the Java version through some method call or similar, but I don't think it would help you here, I mean that I guess you can't do something like:
if(java-7) {
//do try-with-resources
} else {
// do usual try-statement
}
As #skiwi suggested, make sure your application compiles against an older Java version, and you should be fine with JVMs of that version and beyond.
I'm not sure what's causing your ClassNotFound error, but it could be that the JVM checks the required Java version needed and stops if it is above the available.
If you compile from the command line, there are switches to tell javac the source Java version and the target Java versions. I never had used them, so you better search for questions about them (i.e. javac source and target options). With those tools it will be the compiler that warns you against problems, without you having to explicitly check the version in your code.
There is a way to check the Java version, but you would need multiple executables to do it.
You would need a root executable, that would have a low-bar minimum Java version, simply so it can check the version of the systems, then it would launch an executable from there based on the system's Java version.
How you would come about this:
System.getProperty() allows you to, well, get system properties, along with a few other things, like versions of installed applications like Java.
You should create a separate JAR with one class:
public class Launcher {
public static void main(String[] args) {
String javaVersion = System.getProperty("java.version");
// Conditional to launch Jar file
if(correctVersion) { // if version is confirmed
Runtime.getRuntime().exec("java -jar whatever.jar");
}
}
}
The reason we need a root, lower bar executable, is because if the original executable can't run in the first place, how is it going to check the Java version?
Hope this works out for you. Sorry it took so long to get an answer.

Unable to run selenium webdriver script using TestNG

I have integrated eclipse & TestNG & could able to run one sample Java program which provides the output folder. But while included web driver script it could not run using TestNG, but able to run the same using Java program(two choices it ask while right click on the script to run).
Please le me know where the issue might be.
Thanks,
Amal
As #Chetan said in the comments - you need to attach the #Test annotation to your method.
your method would look something like...
#Test
public void myTest() {
WebDriver driver = new...
}
Also when you import, import TestNG's Test annotation. not jUnit's.

java.lang.exception no runnable methods junit

I have a suite, inside which I have added the test class.
I am using surefire to run my JUnits.
My test class ends with test and the methods has #test annotations to it.
How can this problem be resolved?
Here are various suggestions for this incomplete question (for those unfortunate enough to be brought here by google looking for an answer to this common issue):
if using Junit4.x, just use annotations (#Test); don't create a test suite: see this question for details.
original question said "#Test" annotation is being used, which should prevent the error. But it can still happen if there are other errors that happen earlier, and junit can hide the original problem with this message. E.g., see if there are problems with Spring configuration (unset #Required attributes), misconfigured mock objects, etc.
to avoid other frequent issues that also may generate this error (such as running classes suffixed by "*Test" that do not have any #Test methods), try updating to the surefire plugin 2.7 (currently #2.8.1) and junit 4.7+ (currently #4.8.1) to avoid this issue (i'm using maven3, btw; perhaps do a "mvn clean" before "mvn test" to be safe)
long shot: upgrade to at least ant 1.7+ (currently 1.8+) to avoid junit 4 test suite issues
You are using the correct version of JUnit at least 4.X to be able to use annotations for that? (Maven?)

Automatically add properties when running JUnit in Eclipse

In order to run my unit tests on my Eclipse, I need to set some properties for the VM.
Thus, when I first run my JUnit test, I go in "Open Run Dialog", then in my JUnit configuration for this test, I go in "Arguments" tab and put everything I need in the "VM arguments" text area.
Is there a way to automatically add a set of properties when I run my JUnit, so I will be able to only right-click on the test class, and click on "Run as > Junit Test" to run a test?
Technical information:
Eclipse 3.3.2, JUnit 4, Java 5
Edit, regarding response from Aaron Digulla:
These properties are used in Spring configuration files*. Thus, I can't use the idea given by Aaron, as Spring will be initialized before the test is run.
In addition to that, I just need to know if I can achieve that in a easy way in Eclipse. Thus the solution must not have any impact on the compilation of the application outside Eclipse, as my application will finally be compiled (and tested) by Maven2.
* few "unit" tests indeed need my Spring configuration to be run. Ok, I know that it is not real unit tests ;o)
Edit 2: In fact, I was indeed starting the Spring configuration by a test unit. Thus, before starting Spring, I check the System properties, and if my properties are not set, then I give them the required value...
However, I am a little disappointed that Eclipse can't do that for me automatically...
You could try this - go to
Window->Preferences->Java->Installed JREs
ans select the JVM in use, than put a "Default VM" prameter like
-DrunningInEclipse
Than you can check from within your TestCase:
System.getProperty("runningInEclipse") != null
My solution is to create an abstract test base class for all tests in a project which extends TestCase. It has to be abstract so the automatic unit test finder will not consider it.
In static code block of this class, I set all properties I need. This ensures that the code runs once and only once and that it runs before any test in my project.
[EDIT] You say that Spring is initialized before the tests run. This is a bug in your project: It must be the tests who initialize Spring. Otherwise, you will always run into the problem that you have to test something outside of your control.
Therefore, I suggest to move the Spring init code into a place where you can call it at the point in time when the environment is ready.
Alternatively, check that the environment is correctly set up in setUp() and throw an error if a property is missing. This way, you will at least know why the tests would fail later. But I still prefer to have total control when which subsystem comes to life. Anything else just begs for disaster.
When i want to set some properties entries for my junit test i implement the following
protected void setUp() throws Exception {
super.setUp();
System.setProperty("Property1", "value1");
System.setProperty("Property2", "value2");
}
The properties are set before the test methode is called
EDIT:
You also can read the properties from a file and at thes to the System properties
I never understood why the launch configurations have a way to define environment variables but the only way of adding a system property seems to be to add vm arguments.
The way I've worked around this is to have tests (or an abstract tests base class) test for the presence of required properties, if they aren't there then I load them from a .properties file on the classpath.
This works as I can still override them or specify them from ANT or Maven but can also 'right click' -> Run As -> Junit Test the individual test files.
edit: here is an example of getting Spring to optionally load a properties file in the same manner as described above:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="database.properties"/>
<property name="ignoreResourceNotFound" value="true" />
<property name="systemPropertiesMode">
<util:constant static-field="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</property>
</bean>
Agreed used method in this way in one of my junit tests and it worked
#BeforeClass
public static void setupProperties() {
System.setProperty("catalina.base", "C:\\sam-tomcat-7.0.42");
}