java

When writing to the file, I was using Writer. The solution is switching to OutputStream.

From JDOM reference, org.jdom2.output.XMLOutputter, output(Document doc, java.io.Writer out):
Warning: using your own Writer may cause the outputter's preferred character encoding to be ignored. If you use encodings other than UTF-8, we recommend using the method that takes an OutputStream instead.

Technology:

Wrong character encoding in XML file

After my Java code finished editing an XML file using JDOM2, the resulting project.xml has an incorrect encoding. Special characters (like italian "è") are not represented correctly, resulting in an unreadable character. XML Explorer (and sometimes even WinMerge) refuses to open the file.

I implemented a custom WebSocketAppender for log4j. Log messages were escaped using String.escapeHtml4, newlines were replaced with <br>.
Depending on the log level, messages where printed in bold (WARNING) or in red (ERROR or FATAL).
Events that were considered a problem that needs the user's attention were explicitly stored in a collection, and written to the websocket at the end when the program was done.

ProgrammingLanguage:

Technology:

please delete

How to test a mail service without SMTP-server (Java)

To test if a mail was successfully sent using Java you would normally need an SMTP-server to check. If it happens that you don’t have a server available, you can use mailtrap. To setup mailtrap, following code is used: Properties prop = new Properties(); prop.put("mail.smtp.auth", true); prop.put("mail.smtp.starttls.enable", "true"); prop.put("mail.smtp.host", "smtp.mailtrap.io"); prop.put("mail.smtp.port", "25"); prop.put("mail.smtp.ssl.trust", "smtp.mailtrap.io"); session = Session.getInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("YYY", "XXX"); } }); Mails sent using this session token end up in webservice that you can check.

Dealing with circular reference in Java

In an uni project we had a complex data structure with 2 circular references. We built an android app with a maven spring java backend using docker. The problem was that we could not load all the informations in an entity with cicular references. In this cases we always only got null if we tried to get the correct values from our backend.

Java Async testing

A Java Spring Boot powered REST Api has operations, which require minutes to finish, because of multiple HTTP connections and database queries with lots of data. These tasks are executed Asynchronously. 
The functionality needs to be tested in the integration tests (using less data, so the operation runs in seconds). Now we use Thread.sleep(…) and the tests take 10minutes to finish every time, while we see the operations finished sometimes for 2-3 seconds and the overall required time would be 5minutes. Bu we also observe flakey tests from time to time, because the operations take 100ms more than out waiting time.
 How can we test waiting time that is close to the real waiting time and remove the cause of the flakey tests?

Circumvent a license key

A friend developed an application, that was protected by a license key. Since I was curious how he implemented the key-checking and the application overall I decided to hack the app and write a key generator to use the app. The application was written in java, I looked out for a java decompiler and finally used the „java decompiler“ application. As a result I had a lot of java files. The only problem is, that the naming wasn’t useful at all. For example „class a“ doesn’t say anything about the class. So it took me a while to understand the main parts of the code. I used the information that the UI of the application provides for example certain strings to get a clue where the key verification took place. After i found the key verification it was quite easy to write a key generator, since there it was based on simple math.

Pages

Subscribe to java