Skip to content

Commit 07136c6

Browse files
committed
add junit test to id generate
1 parent 79c4581 commit 07136c6

File tree

8 files changed

+74
-39
lines changed

8 files changed

+74
-39
lines changed

springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/AutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public GeneratorProperties generatorProperties() {
2020
@ConditionalOnMissingBean
2121
public IdKeyDao idKeyDao(GeneratorProperties generatorProperties) {
2222
IdKeyDao keyDao = new IdKeyDao(generatorProperties.getJdbcUrl());
23-
GeneratorContext.getInstance().init(keyDao);
23+
IdGeneratorContext.getInstance().init(keyDao);
2424
return keyDao;
2525
}
2626

springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/GeneratorContext.java

-32
This file was deleted.

springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/IdGenerate.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
public interface IdGenerate {
44

55
default long generateLongId() {
6-
return GeneratorContext.getInstance().generateId(getClass());
6+
return IdGeneratorContext.getInstance().generateId(getClass());
77
}
88

99
default int generateIntId() {
10-
return (int) GeneratorContext.getInstance().generateId(getClass());
10+
return (int) IdGeneratorContext.getInstance().generateId(getClass());
1111
}
1212

1313
default String generateStringId() {
14-
return String.valueOf(GeneratorContext.getInstance().generateId(getClass()));
14+
return String.valueOf(IdGeneratorContext.getInstance().generateId(getClass()));
1515
}
1616

1717
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.codingapi.springboot.generator;
2+
3+
import com.codingapi.springboot.generator.dao.IdKeyDao;
4+
import com.codingapi.springboot.generator.service.IdGenerateService;
5+
6+
public class IdGeneratorContext {
7+
8+
private static IdGeneratorContext instance;
9+
private IdGenerateService idGenerateService;
10+
11+
private IdGeneratorContext() {
12+
}
13+
14+
public static IdGeneratorContext getInstance() {
15+
if (instance == null) {
16+
synchronized (IdGeneratorContext.class) {
17+
if (instance == null) {
18+
instance = new IdGeneratorContext();
19+
}
20+
}
21+
}
22+
return instance;
23+
}
24+
25+
long generateId(Class<?> clazz) {
26+
return idGenerateService.generateId(clazz);
27+
}
28+
29+
void init(IdKeyDao idKeyDao) {
30+
idGenerateService = new IdGenerateService(idKeyDao);
31+
}
32+
33+
}

springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/IdGenerateContext.java renamed to springboot-starter-id-generator/src/main/java/com/codingapi/springboot/generator/service/IdGenerateService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package com.codingapi.springboot.generator;
1+
package com.codingapi.springboot.generator.service;
22

33
import com.codingapi.springboot.generator.dao.IdKeyDao;
44
import com.codingapi.springboot.generator.domain.IdKey;
55

6-
public class IdGenerateContext {
6+
public class IdGenerateService {
77

88
private final IdKeyDao keyDao;
99

10-
public IdGenerateContext(IdKeyDao keyDao) {
10+
public IdGenerateService(IdKeyDao keyDao) {
1111
this.keyDao = keyDao;
1212
}
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codingapi.springboot.generator;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
@Slf4j
10+
@SpringBootTest
11+
class IdGenerateServiceTest {
12+
13+
@Test
14+
void generateId() {
15+
for(int i=0;i<1000;i++) {
16+
long id = IdGeneratorContext.getInstance().generateId(IdGenerateServiceTest.class);
17+
log.info("id=>{}", id);
18+
assertTrue(id > 0, "id generator error.");
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codingapi.springboot.generator;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class IdGeneratorApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(IdGeneratorApplication.class,args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
codingapi.id.generator.jdbc-url=jdbc:h2:file:./test.db

0 commit comments

Comments
 (0)