Skip to content

Commit

Permalink
Improved ejb30 tests
Browse files Browse the repository at this point in the history
- it is hard to find someting when the code swallows exceptions and doesn't log
- fixed equals - it ignored result

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent e2592cd commit d2ba362
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ private Object distinctMethodInterceptor(InvocationContext ctx)
}

protected boolean isAICountOK() {
System.out.println("isAICountOK\n distinctAICount=baseAICount?[" + distinctAICount + "=" + baseAICount + "],"
+ " distinctAICount=baseLevel2AICount?[" + distinctAICount + "=" + baseLevel2AICount + "],"
+ " aroundInvokeList=base,level2,distinct?[" + aroundInvokeList + "]");
return (distinctAICount == baseAICount)
&& (distinctAICount == baseLevel2AICount)
&& checkForCorrectSequence(aroundInvokeList);
}

protected boolean isPostConstructCallCounOK() {
System.out.println("isPostConstructCallCounOK\n distinctPCCount=basePCCount?[" + distinctPCCount + "=" + basePCCount + "],"
+ " distinctPCCount=baseLevel2PCCount?[" + distinctPCCount + "=" + baseLevel2PCCount + "],"
+ " postConstructList=base,level2,distinct?[" + postConstructList + "]");
return (distinctPCCount == basePCCount)
&& (distinctPCCount == baseLevel2PCCount)
&& checkForCorrectSequence(postConstructList);
Expand All @@ -66,17 +72,10 @@ protected boolean isPostConstructCallCounOK() {
private boolean checkForCorrectSequence(List<String> list) {
boolean result = list.size() == 3;
if (result) {
BASE_INTERCEPTOR_NAME.equals(list.get(0));
LEVEL2_INTERCEPTOR_NAME.equals(list.get(1));
DISTINCT_INTERCEPTOR_NAME.equals(list.get(2));
return BASE_INTERCEPTOR_NAME.equals(list.get(0)) && LEVEL2_INTERCEPTOR_NAME.equals(list.get(1))
&& DISTINCT_INTERCEPTOR_NAME.equals(list.get(2));
}

for(String str : list) {
System.out.println("**DISTINCT_INTERCEPTOR_TEST**: " + str);
}
System.out.println("**DISTINCT_INTERCEPTOR_TEST**: " + result);

return result;
return false;
}

String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ Object myOwnAroundInvoke(InvocationContext ctx)
private boolean checkPostConstructSequence() {
boolean result = postConstructList.size() == 2;
if (result) {
"DummyBaseEJB".equals(postConstructList.get(0));
"DummyEJB".equals(postConstructList.get(1));
return "DummyBaseEJB".equals(postConstructList.get(0))
&& "DummyEJB".equals(postConstructList.get(1));
}

return result;
return false;
}

private boolean checkAroundInvokeSequence() {
boolean result = aroundInvokeList.size() == 3;
if (result) {
"DummyBaseEJB".equals(aroundInvokeList.get(0));
"DummyLevel2EJB".equals(aroundInvokeList.get(1));
"DummyEJB".equals(aroundInvokeList.get(2));
return "DummyBaseEJB".equals(aroundInvokeList.get(0))
&& "DummyLevel2EJB".equals(aroundInvokeList.get(1))
&& "DummyEJB".equals(aroundInvokeList.get(2));
}

return result;
return false;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import jakarta.interceptor.AroundInvoke;
import jakarta.annotation.PostConstruct;

public class OverridingMethodsInterceptor
extends BaseLevel2Interceptor {
public class OverridingMethodsInterceptor extends BaseLevel2Interceptor {

protected static final String OVERRIDING_INTERCEPTOR_NAME = "OverridingInterceptor";

Expand All @@ -33,36 +32,39 @@ public class OverridingMethodsInterceptor


@PostConstruct
protected void overridablePostConstructMethod(InvocationContext ctx)
throws RuntimeException {
protected void overridablePostConstructMethod(InvocationContext ctx) throws RuntimeException {
postConstructList.add(OVERRIDING_INTERCEPTOR_NAME);
pcCount++;
try {
ctx.proceed();
} catch(RuntimeException e) {
} catch (RuntimeException e) {
throw e;
} catch(Exception e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}


@AroundInvoke
protected Object overridableAroundInvokeMethod(InvocationContext ctx)
throws Throwable
{
protected Object overridableAroundInvokeMethod(InvocationContext ctx) throws Throwable {
aroundInvokeList.add(OVERRIDING_INTERCEPTOR_NAME);
aiCount++;
return ctx.proceed();
}

protected boolean isAICountOK() {
System.out.println("isAICountOK\n baseLevel2AICount=0?[" + baseLevel2AICount + "=0],"
+ " aiCount=baseAICount?[" + aiCount + "=" + baseAICount + "],"
+ " aroundInvokeList=base,overriding?[" + aroundInvokeList + "]");
return (0 == baseLevel2AICount)
&& (aiCount == baseAICount)
&& checkForCorrectSequence(aroundInvokeList);
}

protected boolean isPostConstructCallCounOK() {
System.out.println("isPostConstructCallCounOK\n baseLevel2PCCount=0?[" + baseLevel2PCCount + "=0],"
+ " pcCount=basePCCount?[" + pcCount + "=" + basePCCount + "],"
+ " postConstructList=base,overriding?[" + postConstructList + "]");
return (pcCount == basePCCount)
&& (baseLevel2PCCount == 0)
&& checkForCorrectSequence(postConstructList);
Expand All @@ -71,16 +73,9 @@ protected boolean isPostConstructCallCounOK() {
private boolean checkForCorrectSequence(List<String> list) {
boolean result = list.size() == 2;
if (result) {
BASE_INTERCEPTOR_NAME.equals(list.get(0));
OVERRIDING_INTERCEPTOR_NAME.equals(list.get(1));
return BASE_INTERCEPTOR_NAME.equals(list.get(0)) && OVERRIDING_INTERCEPTOR_NAME.equals(list.get(1));
}
for(String str : list) {
System.out.println("**OVERRIDING_INTERCEPTOR_TEST**: " + str);
}
System.out.println("**OVERRIDING_INTERCEPTOR_TEST**: " + result);


return result;
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

import javax.naming.*;
import jakarta.ejb.EJB;
import com.sun.s1asdev.ejb.ejb30.interceptors.session.*;
Expand Down Expand Up @@ -195,12 +197,12 @@ private void doTest7_8() {
private void doTest9() {
boolean checkSetParamsStatus = true;
Map<String, Boolean> map = sful.checkSetParams();
for (String testName : map.keySet()) {
System.out.println("Test[" + testName + "]: " + map.get(testName));
checkSetParamsStatus = checkSetParamsStatus && map.get(testName);
System.err.println("checkParams, received map: " + map);
for (Entry<String, Boolean> test : map.entrySet()) {
System.out.println("Test[" + test.getKey() + "]: " + test.getValue());
checkSetParamsStatus = checkSetParamsStatus && test.getValue();
}
stat.addStatus("local test9" ,
(checkSetParamsStatus == true) ? stat.PASS : stat.FAIL);
stat.addStatus("local test9", checkSetParamsStatus == true ? stat.PASS : stat.FAIL);
}

private void doTest10() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2002, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -17,6 +18,9 @@
package com.sun.s1asdev.ejb.ejb30.interceptors.session;

import java.lang.reflect.Method;
import java.lang.StringBuilder;
import java.util.stream.Collectors;
import java.util.Arrays;

import jakarta.interceptor.InvocationContext;
import jakarta.interceptor.AroundInvoke;
Expand All @@ -30,9 +34,8 @@ public Object interceptCall(InvocationContext ctx)
Method method = ctx.getMethod();
String methodName = method.getName();
Object[] params = ctx.getParameters();
System.out.println("**++**Args: " + toString(params));
if (methodName.equals("addIntInt")) {
System.out.println("**++**Args[0]: " + params[0].getClass().getName());
System.out.println("**++**Args[1]: " + params[1].getClass().getName());
params[0] = new Integer(10);
params[1] = new Integer(20);
} else if (methodName.equals("setInt")) {
Expand All @@ -45,7 +48,8 @@ public Object interceptCall(InvocationContext ctx)
} else if (methodName.equals("setFoo")) {
params[0] = new SubFoo();
} else if (methodName.equals("setBar")) {
params[0] = new SubFoo(); //Should fail
// Should fail
params[0] = new SubFoo();
} else if (methodName.equals("emptyArgs")) {
params = new Object[1];
params[0] = new Long(45);
Expand All @@ -70,4 +74,13 @@ public Object interceptCall(InvocationContext ctx)
return retVal;
}

private String toString(Object[] objects) {
if (objects == null) {
return null;
}

return "[\n" + Arrays.stream(objects).map(o -> o == null ? "null" : (o.toString() + ": " + o.getClass()))
.collect(Collectors.joining("\n")) + "\n]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public Map<String, Boolean> checkSetParams() {
} catch (MyBadException ejbEx) {
status = true;
} catch (EJBException ejbEx) {
ejbEx.printStackTrace();
}
map.put("setBar", status);

Expand All @@ -147,6 +148,7 @@ public Map<String, Boolean> checkSetParams() {
} catch (MyBadException illEx) {
status = true;
} catch (EJBException ejbEx) {
ejbEx.printStackTrace();
}
map.put("emptyArgs", status);

Expand All @@ -156,6 +158,7 @@ public Map<String, Boolean> checkSetParams() {
} catch (MyBadException illEx) {
status = true;
} catch (EJBException ejbEx) {
ejbEx.printStackTrace();
}
map.put("objectArgs", status);

Expand All @@ -168,6 +171,7 @@ public Map<String, Boolean> checkSetParams() {
} catch (WrongResultException wrEx) {
wrEx.printStackTrace();
} catch (EJBException ejbEx) {
ejbEx.printStackTrace();
}
map.put("addIntInt", status);

Expand All @@ -177,24 +181,25 @@ public Map<String, Boolean> checkSetParams() {
} catch (MyBadException illEx) {
status = true;
} catch (EJBException ejbEx) {
ejbEx.printStackTrace();
}
map.put("setInt", status);

try {
status = false;
sless.setLong(2323l);
status = true;
} catch (MyBadException illEx) {
} catch (EJBException ejbEx) {
} catch (EJBException | MyBadException ejbEx) {
ejbEx.printStackTrace();
}
map.put("setLong", status);

try {
status = false;
sless.addLongLong(123l, 234l);
status = true;
} catch (MyBadException illEx) {
} catch (EJBException ejbEx) {
} catch (EJBException | MyBadException ejbEx) {
ejbEx.printStackTrace();
}
map.put("setLongLong", status);

Expand Down

0 comments on commit d2ba362

Please sign in to comment.