-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBreeMojoTest.java
121 lines (105 loc) · 3.49 KB
/
BreeMojoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.ttzero.plugin.bree.mybatis;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.junit.Test;
import org.ttzero.plugin.bree.mybatis.utils.ConfigUtil;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Random;
/**
* Created by guanquan.wang at 2019-05-24 23:35
*/
public class BreeMojoTest {
/**
* The default output path
*/
private static Path defaultTestPath = Paths.get("target/out/");
/**
* The root path of test resources
*/
private static Path resourcePath;
static Random random = new Random();
static char[] charArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
private static char[] cache = new char[32];
/**
* Returns the path of test dest path
*
* @return path of test dest
* @throws IOException if I/O error occur
*/
public static Path getOutputTestPath() throws IOException {
if (!Files.exists(defaultTestPath)) {
mkdir(defaultTestPath);
}
return defaultTestPath;
}
/**
* Create a directory
*
* @param destPath the destination directory path
* @return the temp directory path
* @throws IOException if I/O error occur
*/
public static Path mkdir(Path destPath) throws IOException {
Path path;
if (isWindows()) {
path = Files.createDirectories(destPath);
} else {
path = Files.createDirectories(destPath
, PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-x---")));
}
return path;
}
/**
* Returns the path of 'test/resources'
*
* @return the test/resources root path
*/
public static Path testResourceRoot() {
if (resourcePath != null) return resourcePath;
URL url = BreeMojoTest.class.getClassLoader().getResource(".");
if (url == null) {
throw new RuntimeException("Load test resources error.");
}
return resourcePath = isWindows()
? Paths.get(url.getFile().substring(1))
: Paths.get(url.getFile());
}
/**
* Returns path of config
*
* @return the config path
*/
public static Path getConfigPath() {
return testResourceRoot().resolve("bree/config/mysql/config.xml");
}
/**
* Test current OS system is windows family
*
* @return true if OS is windows family
*/
public static boolean isWindows() {
return System.getProperty("os.name").toUpperCase().startsWith("WINDOWS");
}
public static String getRandomString() {
int n = random.nextInt(cache.length) + 1, size = charArray.length;
for (int i = 0; i < n; i++) {
cache[i] = charArray[random.nextInt(size)];
}
return new String(cache, 0, n);
}
@Test
public void testExecute() throws IOException, MojoFailureException, MojoExecutionException {
Path resourceRoot = testResourceRoot();
BreeMojo mojo = new BreeMojo(getOutputTestPath().toFile()
, resourceRoot.resolve("bree/templates/").toFile(), getConfigPath().toFile(), true);
ConfigUtil.setCmd("sdm_rewrite");
// FIXME Modify a exists table name before test.
// then delete '//'
// mojo.execute();
}
}