Christian Wansart

Ranch Hand
+ Follow
since Dec 31, 2015
Christian likes ...
IntelliJ IDE Firefox Browser Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
32
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Christian Wansart

What do you want to achieve?

As far as I cann tell, you want to run a watcher? If so, you can just run:

I'd guess that e.which is undefined. Can you check that?

Also where does $.Event(...) com from?
Not just you. But I can't change it so I have to live with it. :-)

Okay, good point.

But still: Would you call this a factory?

I was already thinking about rewriting it to make it a real Adapter like this:


There is not so much difference to the version above. (I'd probably extract the isSingle to something like to01String().) I have 1 class less since I don't need the ClientValues class anymore. But the Adapter class now has state. Perhaps this is better than my first approach? I am not sure.
8 months ago

Campbell Ritchie wrote:Why do you want a function to turn a boolean to 1/0 in text?


The SOAP endpoints often uses strings like "0", "1" and sometimes also "1" and "2" as boolean values. Don't ask me why, this is an older system I have to work with.

Campbell Ritchie wrote:Why did you call it isSinglePerson, since it doesn't take a person nor does it test whether that person is single?


I guess the naming here is not good enough. I get several boolean values from the JAX-RS resource which need to be converted to the SOAP values. So depending on true or false I have to set "0" or "1".
Hello,

I have a Java EE application which takes input data from a JAX-RS-Resource, passes them through the application and at some point it calls a SOAP client which classes gets generated. But the problem is, that I have multiple clients which represent different start times. There's a "ClientParameters" class which assigns the input data to the client data. But the problem is: I have this class almost exactly the same 4 times. They mostly do assignments and converting. So it looks like this:



So the idea is to replace the InputData parameter with a "ClientValues" class which already holds the correctly formatted data, so that the ClientParameters only calls getters, nothing more.

First I thought I'd create a "ClientValuesAdapter" which simply contains a static method which creates the "ClientValues". Inside we have lots of pure functions that can be tested easily. Something like this:


A friend pointed out that this is not really an Adapter but a Factory. I agree with this mostly, but most definitions on the web use inheritance. I don't see any advantage in using inheritance or non-static methods here. But I don't want to use any naming which may confuse other developers. Most patterns seem to be coupled with inheritance which is sometimes just not necessary.

What would you call it? Is it an Adapter, a Factory or even something else? I was thinking about calling it ClientValuesMapper instead.

I was also thinking about changing it so it conforms the Adapter definition. But I think pure functions are better to test.
Thank you both for your answers.

@Stephan, that is a very interesting approach I'll look further into. Thank you every much, that helped a lot!

@Tim, thanks for reassuring this. I usually did that but was not sure how common it is. For most cases testing the public method is easy and sufficient, but not in the current case. Making those methods package private should solve the problem.
1 year ago
Hello together,

I am currently working on a project which transforms input data from a rest service for another service. The data we receive has probably around 50 properties. In our tests we load a JSON file and map it to the model object. That works, until we do some changes to the model and have to re-new the test data. If we generate one with the frontend and it differs from before, tests may break because they relied on specific permutations. That's bad.

Usually I try to set all data in the "when" part of my unit tests, but having such a huge model makes it hard to do so. But since we have a lot of smaller methods inside the mapping classes which are private, I cannot test them seperately. For example:



So, my question here is: would it be a bad idea to change the modifiers to access the private methods directly? If so, why? I mean, I don't want to make them public, but at least make them package private. Of course there needs to be a test for the map method as well, which has to contain a complete data set for the InputData, but for all other methods as well?

Thanks in advance.
1 year ago
In my linked example the query may throw an exception. The other exceptions were examples from other projects I attended. Let's say we have a user who types in a wrong password, I used to check whether the password is correct or wrong and throw an exception. I would catch that in my resource class like I did with NoResultException.

UserResource.java




So my issue is that the API kind of forces me to use Exceptions. I've learned to catch those and re-throw more meaningful exceptions and catch them in the resource.

@Carey Brown: so you use integers instead of exceptions to create handle different situations?
2 years ago
Thanks for all the interesting answers.

I prepared an example: https://github.com/cwansart/javaee-exceptions-test

This is how I learned to deal with issues, like not finding elements in the database. In the Resource I catch the NoResultException.

In other projects we (me and other devs) threw a lot of Exceptions on services and repositores, like CustomerNotFoundException, WrongPasswordException, DefaultLanguageNotAvailableException which were caught on the resource to create error messages or default values.

After reading and thinking about those things, I'd say that's kind of flow-control. That is why I was wondering if this is a good idea.

Thanks
2 years ago
Hello,

I tried to find an answer online to find out, which performs better: Exception handling or using Optional instead? I have a real-world scenario from a Spring application:



Or using Exceptions:


After writing the code for Exceptions I thought that this is not a very good example. But just think of JavaEE instead where you don't just get Optional returned. This is not about the style itself, but rather on the performance, since I've heard people arguing Exceptions are better and some say Optionals are better.

Which ones may perform better?

Thanks and best regards
2 years ago
Okay, thank you for your recommendations. I'll try both solutions and then decide which I like most.
3 years ago
Yes, that is the issue.

I want it hidden because it should contain the access token of the bot. If I publish it to the git repository (e. g. GitHub) anybody can use it. So I need to make it somehow safe.
I am not sure how to add the token safely except for passing it as a program argument (args).

Regards,
Christian
3 years ago
Hello,

I am currently creating a little bot for Discord using Discord4j. It needs a token, but it should not be published to the git repository.

So I could create a properties file but put it on the ignore list. But I don't really like the idea not shipping files. Another way would be using a program argument, but this bot should run on a raspberry pi later on so I would need to put the token somewhere in a config file or so.

What do you recommend to do here? What is a good way to add properties but hide them from git?

Best regards,
Christian
3 years ago
Okay, but where does the property path come into account?

It doesn't matter if I add any propertyNode, in this example it will still read the message from the ViolationMessages.property with the property name "my.custom.message".

I don't understand where the propertyNode does anything other than set a value. Where is it used?
Hello,

I have this code:


According to the api the addPropertyNode method “Adds a property node to the path the ConstraintViolation will be associated to.
name describes a single property. In particular, dot (.) is not allowed.”

But whatever this value is, it still reads the my.custom.message from ViolationMessages.properties. So, what does this do? When should I use it?

Thanks in advance.