Skip to content

Commit 8771d55

Browse files
committed
Fix split-files validation. Add JUnit5 dependency on testing stage.
1 parent 4cb3cbb commit 8771d55

File tree

7 files changed

+305
-37
lines changed

7 files changed

+305
-37
lines changed

Jenkinsfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ pipeline {
77
}
88

99
stages {
10+
stage('Test') {
11+
steps {
12+
sh 'mvn test'
13+
}
14+
}
1015
stage('Build') {
1116
steps {
12-
sh 'mvn -B -DskipTests clean package'
17+
sh 'mvn clean package'
1318
}
1419
}
1520
}

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@
150150
<version>1.3.0</version> <!-- Must be 1.2.0 for macOS lower than Mojave -->
151151
<scope>compile</scope>
152152
</dependency>
153+
<!-- Junit5 -->
154+
<dependency>
155+
<groupId>org.junit.jupiter</groupId>
156+
<artifactId>junit-jupiter-engine</artifactId>
157+
<version>5.5.2</version>
158+
<scope>test</scope>
159+
</dependency>
160+
<dependency>
161+
<groupId>org.junit.jupiter</groupId>
162+
<artifactId>junit-jupiter-api</artifactId>
163+
<version>5.5.2</version>
164+
<scope>test</scope>
165+
</dependency>
166+
<dependency>
167+
<groupId>org.junit.jupiter</groupId>
168+
<artifactId>junit-jupiter-params</artifactId>
169+
<version>5.5.2</version>
170+
<scope>test</scope>
171+
</dependency>
153172
</dependencies>
154173
<build>
155174
<plugins>

src/main/java/nsusbloader/COM/NET/NetworkSetupValidator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,21 @@ private void validateFiles(List<File> filesList) {
7777
Arrays.sort(subFiles, Comparator.comparingInt(file -> Integer.parseInt(file.getName())));
7878

7979
for (int i = subFiles.length - 2; i > 0 ; i--){
80-
if (subFiles[i].length() < subFiles[i-1].length()) {
80+
if (subFiles[i].length() != subFiles[i-1].length()) {
8181
logPrinter.print("NET: Exclude split file: "+f.getName()+
8282
"\n Chunk sizes of the split file are not the same, but has to be.", EMsgType.WARNING);
8383
return true;
8484
}
8585
}
86+
87+
long firstFileLength = subFiles[0].length();
88+
long lastFileLength = subFiles[subFiles.length-1].length();
89+
90+
if (lastFileLength > firstFileLength){
91+
logPrinter.print("NET: Exclude split file: "+f.getName()+
92+
"\n Chunk sizes of the split file are not the same, but has to be.", EMsgType.WARNING);
93+
return true;
94+
}
8695
return false;
8796
});
8897
}

src/main/java/nsusbloader/COM/USB/TransferModule.java

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,40 @@ public abstract class TransferModule {
4141
this.task = task;
4242
this.logPrinter = printer;
4343

44-
// Validate split files to be sure that there is no crap
45-
//logPrinter.print("TransferModule: Validating split files ...", EMsgType.INFO); // NOTE: Used for debug
46-
Iterator<Map.Entry<String, File>> iterator = nspMap.entrySet().iterator();
47-
while (iterator.hasNext()){
48-
File f = iterator.next().getValue();
49-
if (f.isDirectory()){
50-
File[] subFiles = f.listFiles((file, name) -> name.matches("[0-9]{2}"));
51-
if (subFiles == null || subFiles.length == 0) {
52-
logPrinter.print("TransferModule: Removing empty folder: " + f.getName(), EMsgType.WARNING);
53-
iterator.remove();
54-
}
55-
else {
56-
Arrays.sort(subFiles, Comparator.comparingInt(file -> Integer.parseInt(file.getName())));
57-
58-
for (int i = subFiles.length - 2; i > 0 ; i--){
59-
if (subFiles[i].length() < subFiles[i-1].length()) {
60-
logPrinter.print("TransferModule: Removing strange split file: "+f.getName()+
61-
"\n (Chunk sizes of the split file are not the same, but has to be.)", EMsgType.WARNING);
62-
iterator.remove();
63-
} // what
64-
} // a
65-
} // nice
66-
} // stairway
67-
} // here =)
68-
//logPrinter.print("TransferModule: Validation complete.", EMsgType.INFO); // NOTE: Used for debug
44+
filterFiles();
6945
}
46+
void filterFiles(){
47+
nspMap.values().removeIf(f -> {
48+
if (f.isFile())
49+
return false;
50+
51+
File[] subFiles = f.listFiles((file, name) -> name.matches("[0-9]{2}"));
52+
53+
if (subFiles == null || subFiles.length == 0) {
54+
logPrinter.print("TransferModule: Exclude folder: " + f.getName(), EMsgType.WARNING);
55+
return true;
56+
}
57+
58+
Arrays.sort(subFiles, Comparator.comparingInt(file -> Integer.parseInt(file.getName())));
7059

60+
for (int i = subFiles.length - 2; i > 0 ; i--){
61+
if (subFiles[i].length() != subFiles[i-1].length()) {
62+
logPrinter.print("TransferModule: Exclude split file: "+f.getName()+
63+
"\n Chunk sizes of the split file are not the same, but has to be.", EMsgType.WARNING);
64+
return true;
65+
}
66+
}
67+
68+
long firstFileLength = subFiles[0].length();
69+
long lastFileLength = subFiles[subFiles.length-1].length();
70+
71+
if (lastFileLength > firstFileLength){
72+
logPrinter.print("TransferModule: Exclude split file: "+f.getName()+
73+
"\n Chunk sizes of the split file are not the same, but has to be.", EMsgType.WARNING);
74+
return true;
75+
}
76+
return false;
77+
});
78+
}
7179
public EFileStatus getStatus(){ return status; }
7280
}

src/main/java/nsusbloader/FilesHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import java.nio.file.Paths;
2424

2525
public class FilesHelper {
26-
public static String getRealFolder(String path){
27-
Path splitMergePath = Paths.get(path);
28-
if (Files.notExists(splitMergePath) || Files.isRegularFile(splitMergePath))
26+
public static String getRealFolder(String location){
27+
Path locationAsPath = Paths.get(location);
28+
if (Files.notExists(locationAsPath) || Files.isRegularFile(locationAsPath))
2929
return System.getProperty("user.home");
30-
return path;
30+
return location;
3131
}
3232
}

src/main/java/nsusbloader/RainbowHexDump.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public class RainbowHexDump {
3737
public static void hexDumpUTF8(byte[] byteArray){
3838
System.out.print(ANSI_BLUE);
3939
for (int i=0; i < byteArray.length; i++)
40-
System.out.print(String.format("%02d-", i%100));
40+
System.out.printf("%02d-", i%100);
4141
System.out.println(">"+ANSI_RED+byteArray.length+ANSI_RESET);
4242
for (byte b: byteArray)
43-
System.out.print(String.format("%02x ", b));
43+
System.out.printf("%02x ", b);
4444
System.out.println();
4545
System.out.print("\t\t\t"
4646
+ new String(byteArray, StandardCharsets.UTF_8)
@@ -49,10 +49,10 @@ public static void hexDumpUTF8(byte[] byteArray){
4949

5050
public static void hexDumpUTF8ForWin(byte[] byteArray){
5151
for (int i=0; i < byteArray.length; i++)
52-
System.out.print(String.format("%02d-", i%100));
52+
System.out.printf("%02d-", i%100);
5353
System.out.println(">"+byteArray.length);
5454
for (byte b: byteArray)
55-
System.out.print(String.format("%02x ", b));
55+
System.out.printf("%02x ", b);
5656
System.out.println();
5757
System.out.print(new String(byteArray, StandardCharsets.UTF_8)
5858
+ "\n");
@@ -61,10 +61,10 @@ public static void hexDumpUTF8ForWin(byte[] byteArray){
6161
public static void hexDumpUTF16LE(byte[] byteArray){
6262
System.out.print(ANSI_BLUE);
6363
for (int i=0; i < byteArray.length; i++)
64-
System.out.print(String.format("%02d-", i%100));
64+
System.out.printf("%02d-", i%100);
6565
System.out.println(">"+ANSI_RED+byteArray.length+ANSI_RESET);
6666
for (byte b: byteArray)
67-
System.out.print(String.format("%02x ", b));
67+
System.out.printf("%02x ", b);
6868
System.out.print(new String(byteArray, StandardCharsets.UTF_16LE)
6969
+ "\n");
7070
}

0 commit comments

Comments
 (0)