JMeter Beanshell share class definitions - java

is there a way to share class definitions between scripts created purely in jmeter? For instance if I had the following structure:
Thread1
-BSSampler
-BSSample2
How can I create a class in BSSampler and use that same class definition in BSSample2 explicitly? Or would I have to push the class definition out to a file and use
${__beanShell(source("filename.bsh"))}
to share the same class definitions? Right now it's saying it doesn't recognize the class definition because it's a different namespace.

You can declare your classes and functions in a bsh file that you reference in user.properties through:
beanshell.server.file=../extras/startup.bsh
You can have a look at this file in extras/startup.bsh

Related

Access Java class defined in a file with many classes from Kotlin

I have a package which contains a public Kotlin class and a Java file with many package-private top-level classes like so:
com.example.mypackage
- KotlinClass.kt
- JavaClasses.java
-- Class1
-- Class2
-- ...
If I try to access any class from JavaClasses in KotlinClass I get Unresolved reference error. Is it possible to access these classes?
I cannot change JavaClasses.java because it's generated.
It should be possible to access these classes with the setup that you described, the only time I get a an Unresolved reference error is when there are errors within the java file. Perhaps because JavaClasses.java is generated it doesn't have the correct package declaration at the top of the file, which in your case should be package com.example.mypackage;

Load dynamic library java that inherit interface

I want to load dynamic library where classes inherit from an interface/abstract class on my core project, so I can load my classes at runtime and use it. How can i do that ?
Example:
Core: ITrigger (interface)
Library: {MyTriggerOne extends ITrigger} {MyTriggerTwo extends ITrigger}
If you want to load a class/library dynamically use Class.forName('class name') method to load.
I had the same requirement and I used the library Reflections.
Very simple code snippet:
public Set<Class<? extends ITrigger>> getITriggerClasses() {
final Reflections reflections = new Reflections("package.where.to.find.implementations");
return reflections.getSubTypesOf(ITrigger.class);
}
Then you can use the method Class::newInstance to create the ITrigger(s).
This is a very simple example, there are several options to initialize the Reflections class (not only with one package name).
Java's SPI(Service Provider Interface) libraries allow you to load classes dynamically based on the interfaces they implement, that can be done with the help of META-INF/services.
You can create a interface like
package com.test.dynamic;
public interface ITrigger {
String getData();
String setData();
}
you can use the ServiceLoader class to load the interface like below code
ServiceLoader<ITrigger> loader = ServiceLoader.load(ITrigger.class);
then you can perform all the operation on it.
If you have some other implementing classes on your classpath, they register themselves in META-INF/services.
you need to create a file in META-INF/services in your classpath with the following properties
The name of the file is the fully qualified class name of the
interface, in this case, it's com.test.dynamic.ITrigger
The file contains a newline-separated list of implementations, so
for the example implementation, it would contain one line:
com.test.dynamic.impl.SomeITriggerImplementation class.

How to make my root jaxb generated class extend my own class

I want my xjc generated root class extend one of my class A, which is nothing but a wrapper around the root class. Can i do that without modifying the xsd file (how to force schema compiled classes to extend specific class outside schema)
You can define a super-root class (see http://jaxb.java.net/nonav/2.0.2/docs/vendorCustomizations.html). Every generated class (in that xjc run) will be extending that root class.
The required global binding can be declared in an external binding file instead of inside the xsd (see http://java.sun.com/webservices/docs/1.4/tutorial/doc/JAXBUsing4.html#wp148515). Then you can pass it to the xjc generator together with the xsd.

WSImports specify package name for a class

I have following scenario, I have two packages say com.ws.a and com.ws.b.
Both the packages having web-services classes. com.ws.a contain class TestServoceA which is having method which returns class TestA.
I want to pass this generated class to TestServiceB which is in com.ws.b.
But wsimport tool generate two different classes for both packages like:
com.ws.a.TestA and
com.ws.b.TestA
I want to generate only one class com.ws.a.TestA as a proxy and use it in both namespaces.
How can I do this? If custom binding can help me please give me example.
Thanks.
wsimport will use JAXB for data binding, follow this documentation to specify custom bindings:
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html

Defining classes in Java files

I have found one error in my Java program:
The public type abc class must be defined in its own class
How can I resolve this error? I am using Eclipse. I am new to Java programming.
Each source file must contain only one public class. A class named ClassName should be in a file named ClassName.java, and only that class should be defined there.
Exceptions to this are anonymous and inner classes, but understanding you are a beginner to Java, that is an advanced topic. For now, keep one class per file.
Answering your addition: it is OK to inherit classes and that's totally fine. This does not matter, each class should still have its own file.
Public top-level classes (i.e. public classes which aren't nested within other classes) have to be defined in a file which matches the classname. So the code for class "Foo" must live in "Foo.java".
From the language specification, section 7.6:
When packages are stored in a file system (ยง7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared public (and therefore is potentially accessible from code in other packages).
This rule, which doesn't have to be followed by compilers, is pretty much universally adhered to.
Ok, maybe an example will help.
In file MySuperClass.java:
public class MySuperClass {
// whatever goes here
}
public class MySubClass1 extends MySuperClass {
// compile error: public class MySubClass1 should be in MySubClass1.java
}
class MySubClass2 extends MySuperClass {
// no problem (non-public class does not have to be in a file of the same name)
}
In file MySubClass3.java:
public class MySubClass3 extends MySuperClass {
// no problem (public class in file of the same name)
}
Does that make things clearer?
A public class with the name of "abc" must be in a file called abc.java
You can create a new class an a existing file if it's private, but you should not do this.
Create one file per class.
Eclipse does that for you, if you create a new class.
For programming Java, you have to understand the construct of classes, packages and files. Even if Eclipse helps you, you have to know it for yourself. So start reading Java books or tutorials!

Categories

Resources