Skip to content

Commit

Permalink
add unit tests #4
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenzhou committed Jul 22, 2021
1 parent 800e8da commit 69a2434
Show file tree
Hide file tree
Showing 37 changed files with 287 additions and 93 deletions.
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ group 'de.0xab'
version project.property('porter-version')

ext {
//plugin version
checkstylePlugin = '8.9'
//test dependencies version
jupiter = '5.7.0'
mockito = '3.11.2'
h2 = '1.4.200'

//dependencies version
jackson = '2.12.4'
hikari = '4.0.3'
clickhouse = '0.3.1-patch'
Expand All @@ -21,16 +25,13 @@ buildscript {
}
}


//apply from: rootDir.path + '/gradle/checkstyle.gradle'
allprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
apply from: rootDir.path + '/gradle/checkstyle.gradle'

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

repositories {
mavenLocal()
mavenCentral()
Expand All @@ -45,6 +46,7 @@ repositories {
subprojects {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:' + property('jupiter')
testImplementation 'org.mockito:mockito-core:' + property('mockito')
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:' + property('jupiter')
}
test.useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* comments after # will be ignored
*/
public class ExtensionLoader {
private static final String FOLDER = "extensions/";
private static final String FOLDER = "";
private static final Map<Class<?>, Map<String, Class<?>>> EXTENSION_HOLDER = new ConcurrentHashMap<>();

public static ExtensionLoader getExtensionLoader() {
Expand Down
9 changes: 9 additions & 0 deletions common/src/main/java/de/xab/porter/common/util/Jsons.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.xab.porter.api.exception.PorterException;

Expand All @@ -28,6 +29,14 @@ public static <T> T fromJson(String content, Class<T> clazz) {
return t;
}

public static <T> T fromJson(String content, TypeReference<T> type) {
try {
return MAPPER.readValue(content, type);
} catch (JsonProcessingException e) {
throw new PorterException("json deserialization failed", e);
}
}

public static String toJson(Object obj) {
final String json;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ private Strings() {
}

public static boolean notNullOrEmpty(String str) {
return str != null && str.length() != 0;
return str != null && !str.isBlank();
}
}
54 changes: 0 additions & 54 deletions common/src/test/java/de/xab/porter/common/SPITest.java

This file was deleted.

36 changes: 36 additions & 0 deletions common/src/test/java/de/xab/porter/common/test/json/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.xab.porter.common.test.json;

import java.util.List;

/**
* class for JSON test
*/
public class Entity {
private List<String> field1;
private String field2;
private int field3;

public List<String> getField1() {
return field1;
}

public void setField1(List<String> field1) {
this.field1 = field1;
}

public String getField2() {
return field2;
}

public void setField2(String field2) {
this.field2 = field2;
}

public int getField3() {
return field3;
}

public void setField3(int field3) {
this.field3 = field3;
}
}
95 changes: 95 additions & 0 deletions common/src/test/java/de/xab/porter/common/test/json/JSONTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package de.xab.porter.common.test.json;

import com.fasterxml.jackson.core.type.TypeReference;
import de.xab.porter.common.util.Jsons;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.*;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* tests for JSON utils
*/
public class JSONTest {
private final String json = "{\"field2\":\"field2\",\"field3\":0}";
private final String jsonList = "[{\"field2\":\"field2\",\"field3\":0}]";
private final String jsonSet = "[{\"field2\":\"field2\",\"field3\":0}]";
private final String jsonMap = "{\"key\":{\"field2\":\"field2\",\"field3\":0}}";
private Entity entity;

@BeforeEach
public void before() {
entity = new Entity();
entity.setField2("field2");
}

@Test
public void testObjectSerialization() {
String json = Jsons.toJson(entity);
assertEquals(this.json, json);
}

@Test
public void testObjectDeserialization() {
Entity fromJson = Jsons.fromJson(this.json, Entity.class);
assertEquals(fromJson.getField1(), entity.getField1());
assertEquals(fromJson.getField2(), entity.getField2());
assertEquals(fromJson.getField3(), entity.getField3());
}

@Test
public void testListSerialization() {
List<Entity> entities = new ArrayList<>();
entities.add(entity);
String json = Jsons.toJson(entities);
assertEquals(json, jsonList);
}

@Test
public void testListDeserialization() {
List<Entity> entities = Jsons.fromJson(jsonList, new TypeReference<>() {
});
Entity entity = entities.get(0);
assertEquals(entity.getField1(), this.entity.getField1());
assertEquals(entity.getField2(), this.entity.getField2());
assertEquals(entity.getField3(), this.entity.getField3());
}

@Test
public void testSetSerialization() {
Set<Entity> entities = new HashSet<>();
entities.add(entity);
String json = Jsons.toJson(entities);
assertEquals(json, jsonSet);
}

@Test
public void testSetDeserialization() {
Set<Entity> entities = Jsons.fromJson(jsonSet, new TypeReference<>() {
});
Entity entity = (Entity) entities.toArray()[0];
assertEquals(entity.getField1(), this.entity.getField1());
assertEquals(entity.getField2(), this.entity.getField2());
assertEquals(entity.getField3(), this.entity.getField3());
}

@Test
public void testMapSerialization() {
Map<String, Entity> entities = new HashMap<>();
entities.put("key", entity);
String json = Jsons.toJson(entities);
assertEquals(json, jsonMap);
}

@Test
public void testMapDeserialization() {
Map<String, Entity> entities = Jsons.fromJson(jsonMap, new TypeReference<>() {
});
Entity entity = entities.get("key");
assertEquals(entity.getField1(), this.entity.getField1());
assertEquals(entity.getField2(), this.entity.getField2());
assertEquals(entity.getField3(), this.entity.getField3());
}
}
45 changes: 45 additions & 0 deletions common/src/test/java/de/xab/porter/common/test/spi/SPITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package de.xab.porter.common.test.spi;

import de.xab.porter.api.exception.PorterException;
import de.xab.porter.common.spi.ExtensionLoader;
import de.xab.porter.common.test.spi.service.MockService;
import de.xab.porter.common.test.spi.service.UnregisteredService;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* test class for porter SPI
*/
public class SPITest {
@Test
public void testExists() {
MockService impl = ExtensionLoader.getExtensionLoader().loadExtension("impl", MockService.class);
assertEquals("hello world", impl.mock());
}

@Test
public void testNotExists() {
assertThrows(PorterException.class, () ->
ExtensionLoader.getExtensionLoader().loadExtension("none", MockService.class));
}

@Test
public void testTypoImpl() {
assertThrows(PorterException.class, () ->
ExtensionLoader.getExtensionLoader().loadExtension("typo", MockService.class));
}

@Test
public void testNoImplemented() {
assertThrows(PorterException.class, () ->
ExtensionLoader.getExtensionLoader().loadExtension("noimpl", MockService.class));
}

@Test
public void testNoneRegistered() {
assertThrows(PorterException.class, () ->
ExtensionLoader.getExtensionLoader().loadExtension("any", UnregisteredService.class));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package de.xab.porter.common.service;
package de.xab.porter.common.test.spi.impl;

import de.xab.porter.common.test.spi.service.MockService;

/**
* mock service impl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.xab.porter.common.service;
package de.xab.porter.common.test.spi.impl;

/**
* not a service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package de.xab.porter.common.service;
package de.xab.porter.common.test.spi.impl;

import de.xab.porter.common.test.spi.service.MockService;

/**
* service not registered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package de.xab.porter.common.service;
package de.xab.porter.common.test.spi.impl;

import de.xab.porter.common.test.spi.service.UnregisteredService;

/**
* implementation of service not registered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.xab.porter.common.service;
package de.xab.porter.common.test.spi.service;

/**
* mock service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.xab.porter.common.service;
package de.xab.porter.common.test.spi.service;

/**
* service not registered
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package de.xab.porter.common.test.string;

import de.xab.porter.common.util.Strings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* test class for Strings util
*/
public class StringTest {
@Test
public void testNull() {
String str = null;
assertFalse(Strings.notNullOrEmpty(str));
}

@Test
public void testEmpty() {
String str = "";
assertFalse(Strings.notNullOrEmpty(str));
}

@Test
public void testNotNullOrEmpty() {
String str = "abc";
Assertions.assertTrue(Strings.notNullOrEmpty(str));
}

@Test
public void testLongEmpty() {
String str = " ";
assertFalse(Strings.notNullOrEmpty(str));
}
}
Loading

0 comments on commit 69a2434

Please sign in to comment.