Skip to content

Commit 4b5afc8

Browse files
author
liuyong
committed
优化test 自动删除测试数据文件
1 parent 0d01927 commit 4b5afc8

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

src/main/java/com/vue/adminlte4j/support/FileChangeListener.java

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
import java.nio.file.StandardCopyOption;
99
import java.time.LocalDateTime;
1010
import java.util.ArrayList;
11+
import java.util.HashSet;
1112
import java.util.List;
13+
import java.util.Set;
1214

1315
/**
1416
* Created by bjliuyong on 2018/3/19.
1517
*/
1618
public class FileChangeListener extends Thread {
1719

18-
private List<FileEntry> fileEntries = new ArrayList<>() ;
20+
private Set<FileEntry> fileEntries = new HashSet<>() ;
1921
private static String projectDir = System.getProperty("user.dir") ;
2022
private static Path sp , tp ;
2123

@@ -69,11 +71,19 @@ public FileChangeListener autoConfig(String type) {
6971

7072
@Override public void run() {
7173

72-
for(int i = 0 ; i < fileEntries.size() ;i++) {
73-
System.out.println("listen file change" + fileEntries.get(i));
74-
}
74+
List<FileEntry> copies = new ArrayList<>();
75+
copies.addAll(fileEntries) ;
76+
copies.forEach(fileEntry-> System.out.println("listen file change" + fileEntry) );
77+
78+
copies.forEach(o -> {
79+
try {
80+
if(o.type() == FileEntryType.DIR )
81+
addFileEntries(o);
82+
} catch (IOException e) {
83+
e.printStackTrace();
84+
}
85+
});
7586

76-
boolean deal = false ;
7787
boolean isInterrupted = false ;
7888
while(!isInterrupted) {
7989
try {
@@ -83,19 +93,13 @@ public FileChangeListener autoConfig(String type) {
8393
e.printStackTrace();
8494
}
8595

86-
for(int i = 0 ; i < fileEntries.size() ;i++) {
96+
fileEntries.forEach(f -> {
8797
try {
88-
if(fileEntries.get(i).type() == FileEntryType.DIR) {
89-
if(deal)
90-
continue;
91-
addFileEntries(fileEntries.get(i)) ;
92-
}
93-
onChange(fileEntries.get(i)) ;
98+
onChange(f);
9499
} catch (Exception e) {
95100
e.printStackTrace();
96101
}
97-
}
98-
deal = true ;
102+
});
99103
}
100104

101105
}
@@ -104,22 +108,50 @@ private void addFileEntries(FileEntry fileEntry) throws IOException {
104108
if(!fileEntry.srcPath.toFile().exists())
105109
return;
106110
Files.walk(fileEntry.srcPath, FileVisitOption.FOLLOW_LINKS).forEach(path -> {
107-
if(path.toFile().isFile() && path.toString().endsWith(".html")) {
111+
/*if(path.toFile().isFile() && path.toString().endsWith(".html")) {
108112
Path relativePath = fileEntry.srcPath.relativize(path) ;
109113
listenAbsolute(path).toAbsolute(Paths.get(fileEntry.targetPath.toString() , relativePath.toString())) ;
110-
}
114+
}*/
115+
116+
Path relativePath = fileEntry.srcPath.relativize(path) ;
117+
Path target = Paths.get(fileEntry.targetPath.toString() , relativePath.toString()) ;
118+
listenAbsolute(path)
119+
.toAbsolute(target)
120+
.type(path.toFile().isFile() ? FileEntryType.FILE : FileEntryType.DIR) ;
111121
});
112122
}
113123

114124
private void onChange(FileEntry fileEntry) throws Exception{
115125
if(!fileEntry.isChange())
116126
return;
127+
if (fileEntry.type() == FileEntryType.DIR) {
128+
Files.walk(fileEntry.srcPath, FileVisitOption.FOLLOW_LINKS).forEach(path -> {
129+
Path relativePath = fileEntry.srcPath.relativize(path) ;
130+
Path target = Paths.get(fileEntry.targetPath.toString() , relativePath.toString()) ;
131+
if(!target.toFile().exists()) {
132+
try {
133+
Files.copy(path , target, StandardCopyOption.REPLACE_EXISTING);
134+
System.out.println(LocalDateTime.now().toString() + "#####File Copy " + path);
135+
} catch (IOException e) {
136+
e.printStackTrace();
137+
}
138+
listenAbsolute(path)
139+
.toAbsolute(target)
140+
.type(path.toFile().isFile() ? FileEntryType.FILE : FileEntryType.DIR) ;
117141

118-
Files.copy(fileEntry.srcPath , fileEntry.targetPath , StandardCopyOption.REPLACE_EXISTING);
119-
System.out.println(LocalDateTime.now().toString() + "#####File Copy " + fileEntry);
120-
}
121-
142+
}
143+
});
144+
} else {
145+
if(fileEntry.srcPath.toFile().exists()) {
146+
Files.copy(fileEntry.srcPath, fileEntry.targetPath, StandardCopyOption.REPLACE_EXISTING);
147+
System.out.println(LocalDateTime.now().toString() + "#####File Copy " + fileEntry);
148+
} else {
149+
fileEntry.targetPath.toFile().delete() ;
150+
System.out.println(LocalDateTime.now().toString() + "#####File Delete " + fileEntry.targetPath);
151+
}
122152

153+
}
154+
}
123155

124156
public static void main(String ...args) {
125157
FileChangeListener fileChangeListener = new FileChangeListener() ;
@@ -199,6 +231,21 @@ public FileEntryType type() {
199231
sb.append('}');
200232
return sb.toString();
201233
}
234+
235+
@Override public boolean equals(Object o) {
236+
if (this == o)
237+
return true;
238+
if (!(o instanceof FileEntry))
239+
return false;
240+
241+
FileEntry entry = (FileEntry) o;
242+
243+
return srcPath.equals(entry.srcPath);
244+
}
245+
246+
@Override public int hashCode() {
247+
return srcPath.hashCode();
248+
}
202249
}
203250

204251
public enum FileEntryType {

0 commit comments

Comments
 (0)