Skip to content

2023

Mock Class Property in PyTest

It's easy to use Mock for a python method, staticmethod, classmethod, but for propery, it's not just to use Mock. Here are the example to mock a property of a class in python UT.

  • A simple class

    Python
    class Dog:
        @property
        def name(self):
            return "dog"
    

Spring Security Authorization Filter

Spring Security supports to easily configure role or authority for a specified resource by using Spring security expression.

For example,

Java
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    ...
    .antMatchers("/auth/admin/*").hasRole("ADMIN")
    .antMatchers("/auth/*").hasAnyRole("ADMIN","USER")
    ...
}

What are PO, VO, DAO, BO, POJO, DTO in Java

Definition

Name Definition Scope
DAO Database access object. A standard of J2EE pattern to access the database. It leverages PO to do database operations. DAO
DTO Data transfer object, used to transfer data between controll and client through HTTP or other ptotocol. PO may be not suitable to transfer directly to client, it may contains sensitive information. So, we need DTO to only transfer necessary data to client. Controller, Service
BO Business object, it leverages DAO, PO, VO(value object) to do business operations. Service
View Object View object are used to present a view to end users, it can be a DTO. Controller, Client
Value Object It's an object that may contains all or subset of a PO fields, and also can contain other fields. It's created by new operator, and it's not visible to Dao or database. Service
PO Persistent object, it maps to database object. Usually, entity objects are PO. Created and managed with database opereations. DAO, Service
POJO Plain Java object. All objects above are POJOs

Switch Fallthrough in Go

If you are familar with C or Java, you must know that break is requird for a switch's case branch, if break is missed, then it will fall through to next case.

For example, consider the following code

Java
int a = 1;

switch (a) {
    case 0: System.out.println("0");
    case 1: System.out.println("1");
    case 2: System.out.println("2"); break;
    default: System.out.println("unknown"); break;
}

The output is:

Bash
1
2

Java Virtual Thread Usage

What is Virtual thread?

A Java platform thread is mapped to OS Kernel thread, as the resource is limited, we can't create as many as platform threads as we want.

Even Java provides executor pool, it also can't handle some cases. For example, if we want to create thoudsands of IO tasks, virtual threads is more suitable than platform threads.

Enable Preview Feature in Idea With Gradle 7.6

When I am trying to use the Java virtual thread in Idea with Gradle 7.6, I got error,

Bash
/java-examples/src/main/java/com.veda/app/virtualthread/VirtualTheadExample.java:5: error: ofVirtual() is a preview API and is disabled by default.
        System.out.println(Thread.ofVirtual().factory());
                                 ^
  (use --enable-preview to enable preview APIs)

Java Sealed Classes

What is Sealed Classes?

A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.

A class is sealed by applying the sealed modifier to its declaration. Then, after any extends and implements clauses, the permits clause specifies the classes that are permitted to extend the sealed class.

The classes specified by permits must be located near the superclass: either in the same module (if the superclass is in a named module) or in the same package (if the superclass is in the unnamed module).

Sonarqube Integration with GitHub

After installed the sonarqube community plugin, we can analyze pull request now.

To allow sonarqube to decorate the pull request with analysis result, complete following steps,

  1. Create a github app and configure the github app in sonarqube global setting following guide github-integration
  2. Install the created github app to your repositories that need to integrate with sonarqube following guide installing-github-apps

Using Sonarqube Community Branch plugin

The Sonarqube community edition, doesn't support analyzing branch or pull request.

However, there is a plugin to support branch analysis for Sonarqube community version.

To install this plugin, you need to download the compatiablity version plugin for the Sonarqube you installed, take Docker version 8.9.10 as example.

For docker version installation please refer to this guide.