Skip to content

Commit 468ee1b

Browse files
committed
fix test + sonar
1 parent 251731a commit 468ee1b

File tree

9 files changed

+52
-95
lines changed

9 files changed

+52
-95
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/tasks/drives/RemoveDirectoryContentTask.java

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,29 @@ public Long run() throws IOException {
3333
}
3434

3535
static long removeDirectoryContent(File dir, WorkProgress progress, long work, boolean calculateSize) throws IOException {
36-
try {
37-
if (!dir.exists())
38-
return 0;
39-
File[] files = dir.listFiles();
40-
if (files == null)
41-
throw new AccessDeniedException(dir.getAbsolutePath());
42-
int nb = files.length;
43-
long size = 0;
44-
for (File f : files) {
45-
long step = work / nb--;
46-
work -= step;
47-
if (f.isDirectory()) {
48-
size += deleteDirectory(f, progress, step, calculateSize);
49-
} else {
50-
if (calculateSize) size += f.length();
51-
try {
52-
Files.delete(f.toPath());
53-
} finally {
54-
if (progress != null) progress.progress(step);
55-
}
56-
}
57-
}
58-
return size;
59-
} finally {
60-
if (progress != null && work > 0) progress.progress(work);
61-
}
36+
return remove(dir, progress, work, calculateSize, false);
6237
}
6338

6439
/** Remove a directory with all its content. This must be called in a task OnFile. */
65-
public static long deleteDirectory(File dir, WorkProgress progress, long work, boolean calculateSize) throws IOException {
40+
static long deleteDirectory(File dir, WorkProgress progress, long work, boolean calculateSize) throws IOException {
41+
return remove(dir, progress, work, calculateSize, true);
42+
}
43+
44+
private static long remove(File dir, WorkProgress progress, long work, boolean calculateSize, boolean deleteDir) throws IOException {
6645
try {
6746
if (!dir.exists())
6847
return 0;
6948
File[] files = dir.listFiles();
7049
if (files == null)
7150
throw new AccessDeniedException(dir.getAbsolutePath());
7251
long size = 0;
73-
int nb = 1 + files.length;
52+
int nb = files.length;
53+
if (deleteDir) nb++;
7454
for (File f : files) {
7555
long step = work / nb--;
7656
work -= step;
7757
if (f.isDirectory())
78-
size += deleteDirectory(f, progress, step, calculateSize);
58+
size += remove(f, progress, step, calculateSize, true);
7959
else {
8060
if (calculateSize) size += f.length();
8161
try {
@@ -86,7 +66,8 @@ public static long deleteDirectory(File dir, WorkProgress progress, long work, b
8666
if (progress != null && step > 0) progress.progress(step);
8767
}
8868
}
89-
Files.delete(dir.toPath());
69+
if (deleteDir)
70+
Files.delete(dir.toPath());
9071
return size;
9172
} finally {
9273
if (progress != null && work > 0)progress.progress(work);

net.lecousin.core/src/main/java/net/lecousin/framework/io/TemporaryFiles.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.lecousin.framework.application.LCCore;
1111
import net.lecousin.framework.concurrent.Task;
1212
import net.lecousin.framework.concurrent.async.AsyncSupplier;
13+
import net.lecousin.framework.concurrent.tasks.drives.RemoveDirectoryTask;
1314

1415
/** Utility class to create temporary files. */
1516
public final class TemporaryFiles {
@@ -38,13 +39,13 @@ public static TemporaryFiles get(Application app) {
3839
dir = null;
3940
}
4041
}
41-
instance = new TemporaryFiles(/*app, */dir);
42+
instance = new TemporaryFiles(app, dir);
4243
}
4344
return instance;
4445
}
4546

46-
private TemporaryFiles(/*Application app, */File dir) {
47-
//this.app = app;
47+
private TemporaryFiles(Application app, File dir) {
48+
this.app = app;
4849
this.tempDir = dir;
4950
try {
5051
prefix = InetAddress.getLocalHost().getHostName();
@@ -58,7 +59,7 @@ private TemporaryFiles(/*Application app, */File dir) {
5859
prefix = prefix.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");
5960
}
6061

61-
//private Application app;
62+
private Application app;
6263
private File tempDir;
6364
private String prefix;
6465

@@ -114,13 +115,7 @@ public FileIO.ReadWrite run() throws IOException {
114115
public File createDirectorySync(String prefix) throws IOException {
115116
Path path = Files.createTempDirectory(tempDir.toPath(), this.prefix + prefix);
116117
File dir = path.toFile();
117-
/*
118-
app.toClose(new Closeable() {
119-
@Override
120-
public void close() {
121-
new RemoveDirectoryTask(dir, null, 0, null, Task.PRIORITY_NORMAL, false).start();
122-
}
123-
});*/
118+
app.toClose(() -> new RemoveDirectoryTask(dir, null, 0, null, Task.PRIORITY_NORMAL, false).start().getOutput());
124119
return dir;
125120
}
126121

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/AttributeAnnotationToRuleOnAttribute.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,7 @@ static void addRule(
5353
+ attr.getOriginalType().getBase().getName(), t);
5454
return;
5555
}
56-
if (rule == null)
57-
return;
58-
boolean found = false;
59-
for (SerializationRule r : rules)
60-
if (r.isEquivalent(rule)) {
61-
found = true;
62-
break;
63-
}
64-
if (!found)
65-
for (SerializationRule r : newRules)
66-
if (r.isEquivalent(rule)) {
67-
found = true;
68-
break;
69-
}
70-
if (!found)
71-
newRules.add(rule);
56+
SerializationRule.addRuleIfNoEquivalent(rule, newRules, rules);
7257
}
7358

7459
/** Search for implementations to convert the given annotation into a rule.

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/AttributeAnnotationToRuleOnType.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,7 @@ static void addRuleFromAttribute(
5656
+ attr.getOriginalType().getBase().getName(), t);
5757
return;
5858
}
59-
if (rule != null) {
60-
boolean found = false;
61-
for (SerializationRule r : rules)
62-
if (r.isEquivalent(rule)) {
63-
found = true;
64-
break;
65-
}
66-
if (!found)
67-
for (SerializationRule r : newRules)
68-
if (r.isEquivalent(rule)) {
69-
found = true;
70-
break;
71-
}
72-
if (!found)
73-
newRules.add(rule);
74-
}
59+
SerializationRule.addRuleIfNoEquivalent(rule, newRules, rules);
7560
}
7661

7762
/** Search for implementations to convert the given annotation into a rule.

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/annotations/TypeAnnotationToRule.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,7 @@ static void addRule(
7272
+ " using " + toRule.getClass().getName(), t);
7373
return;
7474
}
75-
if (rule == null)
76-
return;
77-
boolean found = false;
78-
for (SerializationRule r : rules)
79-
if (r.isEquivalent(rule)) {
80-
found = true;
81-
break;
82-
}
83-
if (!found)
84-
for (SerializationRule r : newRules)
85-
if (r.isEquivalent(rule)) {
86-
found = true;
87-
break;
88-
}
89-
if (!found)
90-
newRules.add(rule);
75+
SerializationRule.addRuleIfNoEquivalent(rule, newRules, rules);
9176
}
9277

9378
/** Search for implementations to convert the given annotation into a rule.

net.lecousin.core/src/main/java/net/lecousin/framework/io/serialization/rules/SerializationRule.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ default Object instantiate(TypeDefinition type, SerializationContext context) th
5656
default void onInstantiation(TypeDefinition type, Object instance, SerializationContext context) throws SerializationException {
5757
}
5858

59+
/** Add the given rule to the newRules if no equivalent rule is found neither in rules or newRules. */
60+
static void addRuleIfNoEquivalent(SerializationRule rule, List<SerializationRule> newRules, List<SerializationRule> rules) {
61+
if (rule == null)
62+
return;
63+
boolean found = false;
64+
for (SerializationRule r : rules)
65+
if (r.isEquivalent(rule)) {
66+
found = true;
67+
break;
68+
}
69+
if (!found)
70+
for (SerializationRule r : newRules)
71+
if (r.isEquivalent(rule)) {
72+
found = true;
73+
break;
74+
}
75+
if (!found)
76+
newRules.add(rule);
77+
}
78+
5979
}

net.lecousin.core/src/main/java/net/lecousin/framework/log/appenders/RollingFileAppender.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.nio.ByteBuffer;
77
import java.nio.charset.StandardCharsets;
8+
import java.nio.file.Files;
89
import java.text.ParseException;
910
import java.util.Map;
1011

@@ -179,8 +180,11 @@ public AsyncSupplier<Void, IOException> execute() {
179180
public Void run() {
180181
File dir = file.getParentFile();
181182
File f = new File(dir, file.getName() + '.' + maxFiles);
182-
if (f.exists() && !f.delete())
183-
return error("Unable to remove log file " + f.getAbsolutePath(), null, result);
183+
try {
184+
Files.deleteIfExists(f.toPath());
185+
} catch (IOException e) {
186+
return error("Unable to remove log file " + f.getAbsolutePath(), e, result);
187+
}
184188
for (int i = maxFiles - 1; i >= 1; --i) {
185189
f = new File(dir, file.getName() + '.' + i);
186190
if (f.exists() && !f.renameTo(new File(dir, file.getName() + '.' + (i + 1))))

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/io/TestIOError.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,21 +613,21 @@ public void testWritableAlwaysError() throws Exception {
613613

614614
io = getWritable(new WritableAlwaysError());
615615
try {
616-
io.writeSync(ByteBuffer.allocate(100));
616+
io.writeSync(ByteBuffer.allocate(100000));
617617
throw new AssertionError();
618618
} catch (IOException e) { /* ok */ }
619619
io.close();
620620

621621
io = getWritable(new WritableAlwaysError());
622622
try {
623-
io.writeAsync(ByteBuffer.allocate(100)).blockResult(0);
623+
io.writeAsync(ByteBuffer.allocate(100000)).blockResult(0);
624624
throw new AssertionError();
625625
} catch (IOException e) { /* ok */ }
626626
io.close();
627627

628628
io = getWritable(new WritableAlwaysError());
629629
try {
630-
io.writeAsync(ByteBuffer.allocate(100), ondone).blockResult(0);
630+
io.writeAsync(ByteBuffer.allocate(100000), ondone).blockResult(0);
631631
throw new AssertionError();
632632
} catch (IOException e) { /* ok */ }
633633
Assert.assertTrue(called.get());

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
**/framework/collections/sort/RedBlackTree*.*,
4040
**/framework/concurrent/async/AsyncSupplier.*,
4141
**/framework/memory/*ArrayCache.*,
42-
**/framework/io/SubIO.*,
4342
**/framework/io/FileIO.*,
43+
**/framework/io/IOUtil.*,
44+
**/framework/io/PositionKnownWrapper.*,
45+
**/framework/io/SubIO.*,
4446
**/framework/io/FragmentedSubIO.*,
4547
**/framework/io/LinkedIO.*,
4648
**/framework/io/buffering/BufferedIO.*,

0 commit comments

Comments
 (0)