Junit5 set displayName programatically from extension - junit5

How can the display name of a test be defined programmatically, via an extension?
From the org.junit.jupiter.api.extension.ExtensionContext I cannot find a way to access the display name. ClassExtensionContext and MethodExtensionContext are package private so I cannot unwrap like:
((ClassExtensionContext) context).getTestDescriptor()
//or
((MethodExtensionContext) context).getTestDescriptor()
even if this was possible the displayName is private with no setter, so the only way was to use reflection which is not a good solution.

Related

KotlinPoet how to get TypeName of generated class

I want to use a class generated with TypeSpec.classBuilder as a property in another class that I am generating. But for this I need to get a TypeName and I cannot find a way to access it. Only from the superclass. Anyone knows a way to do this?
You should be perfectly fine with using ClassName there.
And the easiest way of obtaining the ClassName is to pass the package and the name of the generated type:
ClassName("your.package.here", "NameOfType")
You can see here how I'm specifying a receiver of an extension function ("similar" use-case):

OOP - When to Use Properties vs When to Use Return Values

I am designing a process that should be run daily at work, and I've written a class to do the work. It looks something like this:
class LeadReport {
public $posts = array();
public $fields = array();
protected _getPost() {
// get posts of a certain type
// set them to the property $this->posts
}
protected _getFields() {
// use $this->posts to get fields
// set $this->fields
}
protected _writeCsv() {
// use the properties to write a csv
}
protected _sendMail() {
// attach a csv and send it
}
public function main() {
$this->out('Lead Report');
$this->out("Getting Yesterday's Posts...");
$this->_getPosts();
$this->out("Done.");
$this->out("Next, Parsing the Fields...");
$this->_getFields();
$this->out("Done.");
$this->out("Next, Writing the CSVs...");
$this->_writeCsv();
$this->out("Done.");
$this->out("Finally, Sending Mail");
$this->_sendMail();
$this->out('Bye!');
}
}
After showing this code to one of my colleagues, he commented that the _get() methods should have return values, and that the _write() and _sendMail() methods should use those values as parameters.
So, two questions:
1) Which is "correct" in this case (properties or return values)?
2) Is there a general rule or principle that governs when to use properties over when to use return values in object oriented programming?
I think maybe the source of your question comes from the fact that you are not entirely convinced that using properties is better than having public fields. For example here, common practice says that should not have posts and fields as public. You should use the getField method or a Field protected property to regulate access to those fields. Having a protected getField and a public fields doesn't really make sense.
In this case your colleague may be pointing at two things. The fact that you need to use Properties and not public fields and also the fact that it is probably better to pass the post into the method and not have the method access a property if you can. That way you don't have to set a property before calling the method. Think of it as a way of documenting what the method needs for it to operate. In this way another developer doesn't need to know which properties need to be set for the method to work. Everything the method needs should be passed in.
Regarding why we need properties in the first place? why shouldn't you use public fields. Isn't it more convenient? It sure is. The reason we use properties and not public fields is that just like most other concepts in OOP, you want your object to hide its details from the outside world and just project well defined interfaces of its state. Why? Ultimately to hide implementation details and keep internal change to ripple out(Encapsulation). Also, accessing properties has the added benefit of debugging. You can simply set a breakpoint in a property to see when a variable is changed or simply do a check if the variable is of certain value. Instead of littering your code with said check all over the place. There are many more goodies that come with this, returning readonly values, access control, etc.
To sum up, fields are though of as internal state. Properties(actual get/set methods) are though of as methods that interact with internal state. Having an outside object interact with interfaces is smiley face. Having outside class interact with internal state directly is frowny face.

How to extend ScreenRender

The ScreenRender is initialised with ScreenRenderImpl in ScreenFacadeImpl.makeRender, while ScreenFacade is initialised in ExcecutionContextFactoryImpl. In some cases, I would like to add more functions in ScreenRender that can be invoked in Macro templates. Instead of overriding the ExecutionContextFactoryImpl and down to ScreenRenderImpl as well s MoquiContextListener, is there a way to simply inject a sub class of ScreenRenderImpl when ScreenFacade.makeRender?
A real case to get support of sri in macro template is:
I am trying to populate the options of select via list-options or entity-options or via manual option which return by sri.getFieldOptions(). But it is kind of bound to form fields. I want to use in non-form context. So I kind of want to extend ScreenRender to have a function like sri.getOptions().

Is it possible to override the vb.net Resource Manager GetString function?

In the auto-generated resource designer file, there are properties for each resource. The property calls "GetString" which returns the string value. I would like to override this getstring function so I can do logic to see if I need to retrieve this value or a different value. I can't figure out how to do this because the designer file is auto-generated.
Public ReadOnly Property General() As String
Get
Return ResourceManager.GetString("General", resourceCulture)
End Get
End Property
For example, in my version of the GetString function, I would check the key passed in ("General") and see if there is a custom value for this key in a database. If the custom value exists, I would use that value. If the custom value does not exist, I would call the base GetString function to get the Resource value. I'd like to use the built in Resource class for this because then in my code I can just use "#Resources.General" and take advantage of the auto-complete functionality that already exists.
Refer to ASP.NET Resourcemanager to read local .resx. It's in C# but you can just convert it over. It isn't 100% of what you are looking for but shows a way of overriding in which you may be able to adjust to work with your needs.

Really Struggling with Class Library, How do I Handle Lazy Loading/ConnectionString in Library?

I'm really struggling to get my head around the idea of working with a Class Library and I'm pretty much at the point of just maintaining separate projects with duplicate classes rather than a shared class library.
I've created a solution that contains a single class library project and two web applications. My main problems are the connection strings. These are held/declared in the web projects and I'm having to pass them into the class library every time I perform any kind of data access. I sort of understand why I should do this so I'm going with it for the moment.
This has now led me to a problem/question with lazy loading. I'm using lazy loading for the following property:
Public Property KeyRelationshipManager() As Employee
Get
If _keyRelationshipManager Is Nothing Then
_keyRelationshipManager = Employee.GetEmployee(_keyRelationshipManagerStaffNumber)
Return _keyRelationshipManager
Else
Return _keyRelationshipManager
End If
End Get
Set(ByVal value As AECOM.Employee)
_keyRelationshipManager = value
End Set
End Property
Because this property is using the function:
Employee.GetEmployee
I need to pass in the connection string to that function.
This means I would need to pass the connection string in to the property every time I use it so I could pass it into the function.
Is this correct? It doesn't 'feel' right to me because I'm going to have to adjust a huge number of functions and property and pass through the connections string.
Why are you passing the connection string in to the class library? Use ConfigurationManager.ConnectionStrings["myClassLibraryConnection"] in your class library. As long as you have that connection string in both your host applications' config files, it should be fine. It's the web.config files' jobs to bind the configuration of different class libraries to form a single application.