edu.umd.cs.findbugs.annotations.NonNull doesn't throw error with null params - annotations

I have the following
void testFunc(#NonNull String param1, #NonNull String param2) {
}
When I invoke this method with null parameters from a Junit test case, the test suceeds
testFunc(null, "testString");
This should be throwing an error instead. Any pointers are appreciated. Thanks.

Related

AspectJ applying Around advice on methods that return void

given a block of Advice like below:
#Around("execution(* com.myproject..*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable{
....
Object result = pjp.proceed();
......
return result;
}
I just want to know if I have a method that returns void, will this Advice get applied, and will that result in some kind of error?
Example:
package com.myproject.mypackage;
public Class MyClass {
public void run() {
// Will this method run properly as a result of 'pjp.proceed()' above?
}
}
Tried experimenting by running a few stub methods myself, I found that the Advice will get applied, and there will be no error other than those generated by the joinpoint itself.

Override TestNG's getTestName method

I execute a TestNG test using a dataProvider.
So I set the testName via #BeforeMethod and I override getTestName().
This works so far, but it seems TestNG is calling the test's getTestName in the beginning
before it starts. This happens when an exception was thrown during configuration, so the #BeforeMethod is not executed and therefore my test name is null.
Is there anyway to call the original method, the one that would have been called if I would not have overwritten it :D since I implement an interface an do not extend from another class I cannot use super.getTestName().
Any way to solve this may be?
#Test(groups = {TestGroups.READY}, description = "check help on each tab")
public class HelpTest extends TestControl implements ITest {
// overriding to return my individual testname, but is null at the beginning
#Override
public String getTestName() {
return TestControl.getCurrentTestName();
}
#DataProvider(name = "tabs")
public Iterator<Object[]> tabs() {
Set<Object[]> list = new LinkedHashSet<Object[]>();
for (Tab tab : Tab.values()) {
list.add(new Object[]{tab});
}
return list.iterator();
}
// before the test below starts, i set my individual testname
#BeforeMethod
public void setTestName(Method method, Object[] testData) {
TestControl.setCurrentTestName(method.getName() + "_" + StringUtils.capitalize(testData[0].toString().toLowerCase()));
}
// executing the test with the given data provider
#Test(dataProvider = "tabs")
public void testHelpSites(Tab tab) throws Exception {
TestActions.goTab(tab).callHelp(tab).checkHelp();
}
}
I guess I figured it out, I also use a TestReporter via AbstractWebDriverEventListener and ITestListener and on its onTestStart(ITestResult result) it's calling the test's name and that's the source of the call before the #BeforeMethod call.
I solved it by checking if result.getName() is null, which calls the test's getTestName() if it implements ITest and if it's null I use the original name from result.getMethod.getMethodName(). Not pretty, but rare :D
I could solve this problem using ITestNGMethod testng class.
ITestNGMethod method = result.getMethod(); // result is ITestResult Object
method.getMethodName(); // This will return method name.
My complete method here:
#Override
public void onTestSuccess(ITestResult result) {
ITestNGMethod method = result.getMethod();
String message = "Test Execution is Successful:"+method.getMethodName();
}
Hope this helps

Returning Void from a RequestContext method

Can a method present in the RequestContext method return void?
If my RequestContext looks like this,
#Service( value = PersonUtil.class, locator = PersonLocator.class )
public interface PersonRequest extends RequestContext
{
Request<void> testMethod( Long id );
......
}
I am getting this error:
Multiple markers at this line
- Return type for the method is missing
- Syntax error on token "void", Dimensions expected after
Can we not create a method with return type void? If not, why is it so?
Thanks in advance.
void is just like primitive types like int or boolean, you can't use it as a type parameter.
And just like you'd use Integer instead of int, you'll use Void here (java.lang.Void)

invoke generic method by reflection lead the error

I have a extension method as below.
public static class ExtensionMethod
{
public static string GetTableName<T>(this ObjectContext context) where T : class
{
//Content
}
}
And I invoke this method as below.
static void Main(string[] args)
{
using (BreakAwayEntities context = new BreakAwayEntities())
{
Customer cus = context.Customers.First();
ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(cus);
Type t = entry.Entity.GetType();
MethodInfo method = typeof(ExtensionMethod).GetMethod("GetTableName");
MethodInfo genericMethod = method.MakeGenericMethod(t);
genericMethod.Invoke(null, null);
}
}
But at the last line of the code "genericMethod.Invoke(null,null)" throws an exception "Parameter count mismatch."
Anyone can help?
The GetTableName method has one parameter but you are calling Invoke will a second argument of null indicating that no parameters should be passed. You need to pass your ObjectContext as the second parameter:
genericMethod.Invoke(null, new object[] { context });

understanding func<T, TResult> C#

I am trying to refactor some of my code to use Func
I have created a sample to try to understand how it works but I am getting compilation error. In the e.g. below when I am trying to access the parameter attributes or return the return value the compiler complain.
Clarifications please.
using System;
namespace chsarp
{
class Program
{
static void Main(string[] args)
{
ParamInput input = new ParamInput();
input.ReservationID = 10;
Test testMethod = new Test();
Func<ParamInput, ReservationResult> methodToInvoke = testMethod.GetStatus;
ReservationResult result = TestFn(methodToInvoke, input);
}
static Result TestFn<Param, Result>(Func<Param, Result> methodToInvoke, Param parameter) where Result : new()
{
Result result = new Result();
try
{
result = methodToInvoke(parameter);
}
catch (Exception exception)
{
result.status = string.Format("Error-{0} during reserving {1}",
parameter.ReservationID,
exception.Message);
}
return result;
}
}
class Test
{
public ReservationResult GetStatus(ParamInput msg)
{
ReservationResult result = new ReservationResult();
result.status = string.Format("The ReservationID-{0}", msg.ReservationID);
return result;
}
}
class ParamInput
{
public int ReservationID { get; set; }
}
class ReservationResult
{
public string status { get; set; }
}
}
Annotated compile errors:
error CS1061: 'Result' does not contain a definition for 'status'
error CS1061: 'Param' does not contain a definition for 'ReservationID'
Code using generic type arguments can only compile if the compiler knows the member of the generic type. It doesn't know that the Result type argument has a "status" member. It certainly doesn't if you invoke TestFn<object, object>().
You'll need to use a constraint to make this work. You already do, but new() isn't strong enough. Something like "where Result : IResult" for example, where IResult is an interface type that has a status property. Or a base class. The compiler can now be 100% sure that any allowed concrete instance of the generic method will execute without causing runtime errors. Since it can only ever be compiled when it was invoked with a type that has a "status" property.
All the same applies to the Param type argument. The usefulness of a generic method here rapidly disappears when you do this, it is not the proper usage. Unless you can leverage IResult, like you can IEnumerable.
Compiles...
You ought to descibe what you aim is in your question.
See Hans' answer for details that might help you understand the error.
Your problem is that you're saying the function should take a Result that is instantiable, but you are not saying it has to have a property called "status".
namespace chsarp
{
class Program
{
static void Main(string[] args)
{
Func<int, string> m = Test.GetStatus;
var t = TestFn(m, 10);
}
static string TestFn<TParam>(Func<TParam, string> m, TParam p)
{
try { return m(p); }
catch (Exception exception)
{
return string.Format("Reserving \"{0}\" failure exception: {1}", p, exception);
}
}
}
static class Test
{
public static string GetStatus(int inp)
{
return string.Format("The ReservationID-{0}", inp);
}
}
}