Build method with open parameter list with javapoet - code-generation

is it possible with javapoet to create a method with an open parameter list? To create a method with a String[] parameter is no problem:
curEnumBuilder.addMethod(MethodSpec.methodBuilder("myMethod")
.addParameter(String[].class, "params", Modifier.FINAL)
.addModifiers(Modifier.PUBLIC)
.returns(String.class)
.build());
but I want to create:
public String myMethod(final String... params)

Adding "varargs(true)" to the method builder generates what I want

Related

How to test an add to list method with JUnit

I'm trying to test my add to list method using JUnit.
Here is my test method:
public int urunEkleTest(String marka, String urun) {
UrunDepo depo = new UrunDepo();
String urunekle = depo.urunEkle("Iphone","Apple");
But I have a error message with this test method
Error msg = The method urunEkle(Urun) in the type UrunDepo is not applicable for the arguments (String, String)
And my add to list method is :
public static List<Urun> urunEkle(Urun urun){
URUNLER.add(urun);
return URUNLER;
}
You call a method with one parameter with two parameters.

Get SalesFormLetter class from SalesEditLines form formRun using PreHandler AX7

I need to make some changes on closeOk of SalesEditLines form. As I know, I am not able to change the standard methods, so I need to create an event handler for closeOk.
[PreHandlerFor(formStr(SalesEditLines), formMethodStr(SalesEditLines, closeOk))]
public static void SalesEditLines_Pre_closeOk(XppPrePostArgs args)
{
FormRun sender = args.getThis() as FormRun;
Object callerObject = sender.args().caller();
}
The question is - how can i access a SalesFormLetter through SalesEditLines form formRun using PreHandler?
You can see the following line in init method of SalesEditLines form
salesFormLetter = element.args().caller();
So your callerObject is an instance of SalesFormLetter class, you need just cast it to proper type.
Please check the following link:
https://learn.microsoft.com/en-us/dynamicsax-2012/developer/expression-operators-is-and-as-for-inheritance

Map Target object with no source object using MapStruct

I want to map a Target object with no source object using MapStruct. I tried it, but getting the below error.
Can't generate mapping method with no input arguments
Mapper code
public interface MyMapper {
#Mapping(target="student.courseName", constant="Master in Science")
Target map();
}
As you can see this is not supported. And why would you do that? Why not just write your own method?
That aside, you can theoretically try passing a dummy parameter that won't be mapped.
public interface MyMapper {
#Mapping(target="student.courseName", constant="Master in Science")
Target map(Integer dummy);
}

asp.net mvc editorfor

Is it possible to get the EditorFor method also render the label and validation for a property like the EditorForModel method does ?
Now When I use the EditorFor method for a property (for example a string), it renders only the textbox.
EDIT
Arnis I tried it out and there some problems:
The extension method should be bound on the generic HtmlHelper class. Also returning string from the helper was causing encoded html.
So I modified your code
public static MvcHtmlString EditorWithLabel<T>(this HtmlHelper<T> h,Expression<Func<T, object>> p)
{
return new MvcHtmlString(string.Format("{0}: {1}", h.LabelFor(p), h.EditorFor(p)));
}
But the main problem is it works only with if the propert is of type string.
When the property is Decimal,Int,DateTime an excetion will be thrown.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
I would create custom html helper (code is untested):
public string EditorWithLabel<T>(this HtmlHelper h,
Expression<Func<T, object>> p){
return string.Format("{0}: {1}",h.LabelFor(p),h.EditorFor(p));
}
This can be achieved with templates too, but I think custom helper fits better.

NUnit TestCaseSource pass value to factory

I'm using the NUnit 2.5.3 TestCaseSource attribute and creating a factory to generate my tests. Something like this:
[Test, TestCaseSource(typeof(TestCaseFactories), "VariableString")]
public void Does_Pass_Standard_Description_Tests(string text)
{
Item obj = new Item();
obj.Description = text;
}
My source is this:
public static IEnumerable<TestCaseData> VariableString
{
get
{
yield return new TestCaseData(string.Empty).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Empty_Text");
yield return new TestCaseData(null).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Null_Text");
yield return new TestCaseData(" ").Throws(typeof(PreconditionException))
.SetName("Does_Reject_Whitespace_Text");
}
}
What I need to be able to do is to add a maximum length check to the Variable String, but this maximum length is defined in the contracts in the class under test. In our case its a simple public struct:
public struct ItemLengths
{
public const int Description = 255;
}
I can't find any way of passing a value to the test case generator. I've tried static shared values and these are not picked up. I don't want to save stuff to a file, as then I'd need to regenerate this file every time the code changed.
I want to add the following line to my testcase:
yield return new TestCaseData(new string('A', MAX_LENGTH_HERE + 1))
.Throws(typeof(PreconditionException));
Something fairly simple in concept, but something I'm finding impossible to do. Any suggestions?
Change the parameter of your test as class instead of a string. Like so:
public class StringTest {
public string testString;
public int maxLength;
}
Then construct this class to pass as an argument to TestCaseData constructor. That way you can pass the string and any other arguments you like.
Another option is to make the test have 2 arguments of string and int.
Then for the TestCaseData( "mystring", 255). Did you realize they can have multiple arguments?
Wayne
I faced a similar problem like yours and ended up writing a small NUnit addin and a custom attribute that extends the NUnit TestCaseSourceAttribute. In my particular case I wasn't interested in passing parameters to the factory method but you could easily use the same technique to achieve what you want.
It wasn't all that hard and only required me to write something like three small classes. You can read more about my solution at: blackbox testing with nunit using a custom testcasesource.
PS. In order to use this technique you have to use NUnit 2.5 (at least) Good luck.