Reputation: 99
while executing in jmeter bean shell sampler we are getting the error as above mentioned.
Actually code was working fine in Eclipse but while implement in Jmeter it is not working. can any one help us to resolve this issue?
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.*;
import java.util.Set;
public class JsonComparator {
public static void main(String[] args) throws Exception {
JsonParser parser = new JsonParser();
try{
Gson g = new Gson();
JsonElement jsonElement1 = parser
.parse(new FileReader("E:\\InCites_UI\\trunk\\Tests\\Filters_ByPerson_People_PerfC.json"));
JsonElement jsonElement2 = parser
.parse(new FileReader("E:\\InCites_UI\\trunk\\Tests\\Filters_ByPerson_People_PerfA.json"));
System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1, jsonElement2));
if(!compareJson(jsonElement1, jsonElement2)){
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String,Object> firstMap = g.fromJson(jsonElement1, mapType);
Map<String, Object> secondMap = g.fromJson(jsonElement2, mapType);
System.out.println(JsonComparator.mapDifference(firstMap, secondMap));
}
else{
System.out.println("The Two JSON Are SAME!!!!!!!!!!!!!!!");
}
}catch(Exception e1){
e1.printStackTrace();
}
}
public static <K, V> Map<K, V> mapDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
Map<K, V> difference = new HashMap<K, V>();
difference.putAll(left);
difference.putAll(right);
difference.entrySet().removeAll(right.entrySet());
return difference;
}
public static boolean compareJson(JsonElement json1, JsonElement json2) {
boolean isEqual = true;
ArrayList<Object> ls1 = new ArrayList<Object>();
ArrayList<Object> ls2 = new ArrayList<Object>();
// Check whether both jsonElement are not null
if (json1 != null && json2 != null) {
// Check whether both jsonElement are objects
if (json1.isJsonObject() && json2.isJsonObject()) {
Set<Entry<String, JsonElement>> ens1 = ((JsonObject) json1).entrySet();
Set<Entry<String, JsonElement>> ens2 = ((JsonObject) json2).entrySet();
JsonObject json2obj = (JsonObject) json2;
if (ens1 != null && ens2 != null && (ens2.size() == ens1.size())) {
// Iterate JSON Elements with Key values
for (Entry<String, JsonElement> en : ens1) {
isEqual = isEqual && compareJson(en.getValue(),json2obj.get(en.getKey()));
}
} else {
return false;
}
}
// Check whether both jsonElement are arrays
else if (json1.isJsonArray() && json2.isJsonArray()) {
JsonArray jarr1 = json1.getAsJsonArray();
JsonArray jarr2 = json2.getAsJsonArray();
if (jarr1.size() != jarr2.size()) {
return false;
} else {
int i = 0;
// Iterate JSON Array to JSON Elements
for (JsonElement je : jarr1) {
isEqual = isEqual && compareJson(je, jarr2.get(i));
i++;
}
if (isEqual) {
Object[] o1 = ls1.toArray();
Object[] o2 = ls2.toArray();
isEqual = Arrays.deepEquals(o1, o2);
}
}
}
// Check whether both jsonElement are null
else if (json1.isJsonNull() && json2.isJsonNull()) {
return true;
}
// Check whether both jsonElement are primitives
else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) {
ls1.add(json1);
ls2.add(json2);
}
} else if (json1 == null && json2 == null) {
return true;
} else {
return false;
}
return isEqual;
}
}
we are looking for the help on this, If any one can able to use this code and try to work on Jmeter? we are not able implement in Jmeter.
Actually code was working fine in Eclipse but while implement in Jmeter it is not working.
Upvotes: 0
Views: 3454
Reputation: 167992
There are 2 Beanshell script troubleshooting techniques:
Put your code inside try block like:
try {
//your code here
}
catch (Throwable ex) {
log.error("Beanshell failure", ex);
throw ex;
}
taking this approach you will get the way more informative stacktrace in jmeter.log file
In general I would recommend switching to JSR223 Sampler and Groovy language. Groovy has built-in JSON support and performs much better comparing to Beanshell, see Groovy Is the New Black article for details.
Going forward don't post code as images, I don't think there will be enthusiasts who will dig into this.
Upvotes: 1