Skip to content

Commit d83592f

Browse files
committed
add tests
1 parent 16f95a0 commit d83592f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+812
-112
lines changed

core.loaders/maven/src/test/java/net/lecousin/core/loaders/maven/tests/TestLocalRepository.java

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
import net.lecousin.framework.concurrent.async.AsyncSupplier;
2929
import net.lecousin.framework.concurrent.async.CancelException;
3030
import net.lecousin.framework.core.test.LCCoreAbstractTest;
31+
import net.lecousin.framework.core.test.io.provider.TestURIProvider;
3132
import net.lecousin.framework.io.FileIO;
33+
import net.lecousin.framework.io.IO;
3234
import net.lecousin.framework.io.IOUtil;
35+
import net.lecousin.framework.io.buffering.ByteArrayIO;
3336
import net.lecousin.framework.io.provider.IOProvider;
3437
import net.lecousin.framework.io.provider.IOProviderFromURI;
3538
import net.lecousin.framework.util.SystemEnvironment;
@@ -160,17 +163,39 @@ public void testLoadUnknownArtifact() throws Exception {
160163
public void testDependencyWithSystemPath() throws Exception {
161164
AsyncSupplier<MavenPOM, LibraryManagementException> load = MavenPOM.load(new URI("classpath:test-maven/test-system-path.pom.xml"), Task.PRIORITY_NORMAL, pomLoader, false);
162165
MavenPOM pom = load.blockResult(30000);
166+
pom.getLoader();
167+
pom.getDirectory();
163168
List<LibraryDescriptor.Dependency> deps = pom.getAllDependenciesAnyScope();
164169
Assert.assertFalse(pom.hasClasses());
165170
Assert.assertNull(pom.getClasses().blockResult(15000));
166-
Assert.assertEquals(2, deps.size());
171+
Assert.assertEquals(3, deps.size());
167172
for (LibraryDescriptor.Dependency dep : deps) {
168-
if (dep.getArtifactId().equals("depsys"))
173+
if (dep.getArtifactId().equals("depsys")) {
169174
Assert.assertEquals("/test", dep.getKnownLocation().toString());
170-
else if (dep.getArtifactId().equals("depsys2"))
175+
Assert.assertEquals("test", dep.getGroupId());
176+
Assert.assertTrue(dep.getVersionSpecification().isMatching(new Version("1")));
177+
Assert.assertNull(dep.getClassifier());
178+
Assert.assertFalse(dep.isOptional());
179+
Assert.assertEquals(0, dep.getExcludedDependencies().size());
180+
} else if (dep.getArtifactId().equals("depsys2")) {
171181
Assert.assertEquals("file://test", dep.getKnownLocation().toString());
172-
else
182+
Assert.assertEquals("test", dep.getGroupId());
183+
Assert.assertTrue(dep.getVersionSpecification().isMatching(new Version("1")));
184+
Assert.assertEquals("classified", dep.getClassifier());
185+
Assert.assertTrue(dep.isOptional());
186+
Assert.assertEquals(0, dep.getExcludedDependencies().size());
187+
} else if (dep.getArtifactId().equals("depsys3")) {
188+
Assert.assertNull("/test", dep.getKnownLocation());
189+
Assert.assertEquals("test", dep.getGroupId());
190+
Assert.assertTrue(dep.getVersionSpecification().isMatching(new Version("1")));
191+
Assert.assertNull(dep.getClassifier());
192+
Assert.assertFalse(dep.isOptional());
193+
Assert.assertEquals(1, dep.getExcludedDependencies().size());
194+
Assert.assertEquals("groupExcluded", dep.getExcludedDependencies().get(0).getValue1());
195+
Assert.assertEquals("artifactExcluded", dep.getExcludedDependencies().get(0).getValue2());
196+
} else {
173197
throw new Exception("Unexpected dependency: " + dep.getArtifactId());
198+
}
174199
}
175200
}
176201

@@ -314,6 +339,44 @@ public void testErrors() throws Exception {
314339
testError("empty");
315340
}
316341

342+
private void testIOError(String path) throws Exception {
343+
AsyncSupplier<MavenPOM, LibraryManagementException> load =
344+
MavenPOM.load(new URI("test://" + path), Task.PRIORITY_NORMAL, pomLoader, false);
345+
try {
346+
load.blockResult(30000);
347+
throw new AssertionError("Error expected for pom " + path);
348+
} catch (LibraryManagementException e) {
349+
// ok
350+
}
351+
}
352+
353+
@Test
354+
public void testIOErrors() throws Exception {
355+
testIOError("/errors/provider/readable");
356+
testIOError("/errors/provider/writable");
357+
testIOError("/errors/always/readable");
358+
testIOError("/errors/always/readable/knownsize");
359+
testIOError("/readable/empty");
360+
}
361+
362+
@Test
363+
public void testFromTest() throws Exception {
364+
TestURIProvider.getInstance().register("/mypom", new IOProvider.Readable() {
365+
@Override
366+
public String getDescription() {
367+
return "mypom";
368+
}
369+
@Override
370+
public IO.Readable provideIOReadable(byte priority) throws IOException {
371+
return new ByteArrayIO("<project xmlns=\"http://maven.apache.org/POM/4.0.0\"><parent><relativePath>..</relativePath></parent></project>".getBytes(), "mypom");
372+
}
373+
});
374+
AsyncSupplier<MavenPOM, LibraryManagementException> load =
375+
MavenPOM.load(new URI("test:///mypom"), Task.PRIORITY_NORMAL, pomLoader, false);
376+
MavenPOM pom = load.blockResult(5000);
377+
Assert.assertNull(pom.getClasses().blockResult(0));
378+
}
379+
317380
@Test
318381
public void testLoadFile() throws Exception {
319382
Assert.assertNotNull(repo.loadFileSync("junit", "junit", junit.runner.Version.id(), null, null));

core.loaders/maven/src/test/resources/test-maven/test-system-path.pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@
1919
<artifactId>depsys2</artifactId>
2020
<version>1</version>
2121
<systemPath>file://test</systemPath>
22+
<classifier>classified</classifier>
23+
<optional>true</optional>
24+
</dependency>
25+
<dependency>
26+
<groupId>test</groupId>
27+
<artifactId>depsys3</artifactId>
28+
<version>1</version>
29+
<systemPath>toto:-è$*%)?*/!</systemPath>
30+
<optional>false</optional>
31+
<exclusions>
32+
<exclusion>
33+
<groupId>groupExcluded</groupId>
34+
<artifactId>artifactExcluded</artifactId>
35+
</exclusion>
36+
</exclusions>
2237
</dependency>
2338
</dependencies>
2439
</project>

net.lecousin.core/src/main/java/net/lecousin/framework/concurrent/DrivesTaskManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ public TaskManager getTaskManager(String path) {
9494
}
9595
return null;
9696
}
97+
98+
/** Return the known drive partition path for the given file. */
99+
public String getPartitionPath(File file) {
100+
return getPartitionPath(file.getAbsolutePath());
101+
}
102+
103+
/** Return the known drive partition path for the given file path. */
104+
public String getPartitionPath(String path) {
105+
Map.Entry<String, MonoThreadTaskManager> bestMatch = null;
106+
synchronized (rootManagers) {
107+
for (Map.Entry<String, MonoThreadTaskManager> e : rootManagers.entrySet())
108+
if (path.startsWith(e.getKey()) &&
109+
(bestMatch == null || bestMatch.getKey().length() < e.getKey().length()))
110+
bestMatch = e;
111+
if (bestMatch != null)
112+
return bestMatch.getKey();
113+
}
114+
return null;
115+
}
97116

98117
/** Interface to provide drives and partitions. */
99118
public static interface DrivesProvider {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.AccessDeniedException;
6+
import java.nio.file.Files;
67

78
import net.lecousin.framework.concurrent.Task;
89
import net.lecousin.framework.progress.WorkProgress;
@@ -47,11 +48,11 @@ static long removeDirectoryContent(File dir, WorkProgress progress, long work, b
4748
size += deleteDirectory(f, progress, step, calculateSize);
4849
} else {
4950
if (calculateSize) size += f.length();
50-
if (!f.delete()) {
51+
try {
52+
Files.delete(f.toPath());
53+
} finally {
5154
if (progress != null) progress.progress(step);
52-
throw new IOException("Unable to delete file " + f.getAbsolutePath());
5355
}
54-
if (progress != null) progress.progress(step);
5556
}
5657
}
5758
return size;
@@ -77,15 +78,15 @@ public static long deleteDirectory(File dir, WorkProgress progress, long work, b
7778
size += deleteDirectory(f, progress, step, calculateSize);
7879
else {
7980
if (calculateSize) size += f.length();
80-
if (!f.delete() && f.exists()) {
81+
try {
82+
Files.delete(f.toPath());
83+
} finally {
8184
if (progress != null) progress.progress(step);
82-
throw new IOException("Unable to delete file " + f.getAbsolutePath());
8385
}
8486
if (progress != null && step > 0) progress.progress(step);
8587
}
8688
}
87-
if (!dir.delete() && dir.exists())
88-
throw new IOException("Unable to delete directory " + dir.getAbsolutePath());
89+
Files.delete(dir.toPath());
8990
return size;
9091
} finally {
9192
if (progress != null && work > 0)progress.progress(work);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.nio.file.Files;
56

67
import net.lecousin.framework.concurrent.Task;
78

@@ -18,8 +19,7 @@ public RemoveFileTask(File file, byte priority) {
1819

1920
@Override
2021
public Void run() throws IOException {
21-
if (!file.delete() && file.exists())
22-
throw new IOException("Unable to remove file");
22+
Files.deleteIfExists(file.toPath());
2323
return null;
2424
}
2525

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.Objects;
56

67
import net.lecousin.framework.concurrent.Task;
78
import net.lecousin.framework.concurrent.TaskManager;
@@ -21,13 +22,13 @@ public class RenameFileTask extends Task.OnFile<Void,IOException> {
2122
* on the same drive or not.
2223
*/
2324
public static IAsync<IOException> rename(File source, File destination, byte priority) {
24-
// TODO we should use the roots instead of drive
25-
TaskManager t1 = Threading.getDrivesTaskManager().getTaskManager(source);
26-
TaskManager t2 = Threading.getDrivesTaskManager().getTaskManager(destination);
27-
if (t1 == null) t1 = Threading.getUnmanagedTaskManager();
28-
if (t2 == null) t2 = Threading.getUnmanagedTaskManager();
29-
if (t1 == t2)
30-
return new RenameFileTask(t1, source, destination, priority).start().getOutput();
25+
String partitionSource = Threading.getDrivesTaskManager().getPartitionPath(source);
26+
String partitionDest = Threading.getDrivesTaskManager().getPartitionPath(destination);
27+
28+
if (Objects.equals(partitionSource, partitionDest))
29+
return new RenameFileTask(Threading.getDrivesTaskManager().getTaskManager(partitionSource), source, destination, priority)
30+
.start().getOutput();
31+
3132
AsyncSupplier<Long, IOException> copy = IOUtil.copy(source, destination, priority, source.length(), null, 0, null);
3233
Async<IOException> result = new Async<>();
3334
copy.onDone(() -> new RemoveFileTask(source, priority).start().getOutput().onDone(result), result);

net.lecousin.core/src/main/java/net/lecousin/framework/text/ArrayStringBuffer.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ public void setNewArrayStringCapacity(int capacity) {
5151
protected abstract T createString(CharSequence s);
5252

5353
protected abstract T createString(CharSequence s, int startPos, int endPos);
54-
54+
55+
protected abstract T createString(char singleChar);
56+
57+
protected abstract T createString(char[] chars);
58+
5559
protected abstract ME createBuffer();
5660

5761
protected abstract ME createBuffer(T s);
@@ -581,9 +585,35 @@ public ME replace(char oldChar, char newChar) {
581585
}
582586

583587
/** Replace all occurrences of oldChar into replaceValue. */
588+
@Override
584589
public ME replace(char oldChar, CharSequence replaceValue) {
585590
return replace(oldChar, createString(replaceValue));
586591
}
592+
593+
@Override
594+
public ME replace(CharSequence search, char replace) {
595+
return replace(search, createString(replace));
596+
}
597+
598+
@Override
599+
public ME replace(char oldChar, char[] replace) {
600+
return replace(oldChar, createString(replace));
601+
}
602+
603+
@Override
604+
public ME replace(CharSequence search, char[] replace) {
605+
return replace(search, createString(replace));
606+
}
607+
608+
@Override
609+
public ME replace(int start, int end, char replace) {
610+
return replace(start, end, createString(replace));
611+
}
612+
613+
@Override
614+
public ME replace(int start, int end, char[] replace) {
615+
return replace(start, end, createString(replace));
616+
}
587617

588618
/** Replace all occurrences of oldChar into replaceValue. */
589619
@SuppressWarnings("unchecked")
@@ -605,24 +635,26 @@ public ME replace(char oldChar, T replaceValue) {
605635
public ME replace(int start, int end, CharSequence s) {
606636
if (s.getClass().equals(getArrayType()))
607637
replace(start, end, (T)s);
638+
else if (s.getClass().equals(getClass()))
639+
replace(start, end, (ME)s);
608640
else
609641
replace(start, end, createString(s));
610642
return (ME)this;
611643
}
612644

613645
/** Remove characters from start to end (inclusive), and replace them by the given string. */
614646
@SuppressWarnings("unchecked")
615-
public void replace(int start, int end, T s) {
616-
if (strings == null) return;
617-
if (end < start) return;
647+
public ME replace(int start, int end, T s) {
648+
if (strings == null) return (ME)this;
649+
if (end < start) return (ME)this;
618650
int firstBufferIndex = 0;
619651
int firstBufferPos = 0;
620652
int firstBufferLen;
621653
do {
622654
firstBufferLen = strings[firstBufferIndex].length();
623655
if (start < firstBufferPos + firstBufferLen) break;
624656
firstBufferPos += firstBufferLen;
625-
if (++firstBufferIndex > lastUsed) return;
657+
if (++firstBufferIndex > lastUsed) return (ME)this;
626658
} while (true);
627659
int lastBufferIndex = firstBufferIndex;
628660
int lastBufferPos = firstBufferPos;
@@ -645,6 +677,7 @@ public void replace(int start, int end, T s) {
645677
1,
646678
s
647679
);
680+
return (ME)this;
648681
}
649682

650683
/** Remove characters from start to end (inclusive), and replace them by the given string. */

net.lecousin.core/src/main/java/net/lecousin/framework/text/ByteArrayStringIso8859.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,36 @@ public ByteArrayStringIso8859 replace(int start, int end, CharSequence replace)
350350
return this;
351351
}
352352

353+
@Override
354+
public ByteArrayStringIso8859 replace(char oldChar, CharSequence replace) {
355+
return replace(new CharArrayString(oldChar), replace);
356+
}
357+
358+
@Override
359+
public ByteArrayStringIso8859 replace(char oldChar, char[] replace) {
360+
return replace(new CharArrayString(oldChar), new CharArrayString(replace));
361+
}
362+
363+
@Override
364+
public ByteArrayStringIso8859 replace(CharSequence search, char replace) {
365+
return replace(search, new CharArrayString(replace));
366+
}
367+
368+
@Override
369+
public ByteArrayStringIso8859 replace(CharSequence search, char[] replace) {
370+
return replace(search, new CharArrayString(replace));
371+
}
372+
373+
@Override
374+
public ByteArrayStringIso8859 replace(int start, int end, char replace) {
375+
return replace(start, end, new CharArrayString(replace));
376+
}
377+
378+
@Override
379+
public ByteArrayStringIso8859 replace(int start, int end, char[] replace) {
380+
return replace(start, end, new CharArrayString(replace));
381+
}
382+
353383
private void overwrite(int start, CharSequence s) {
354384
for (int i = s.length() - 1; i >= 0; --i)
355385
chars[start + i] = (byte)s.charAt(i);

net.lecousin.core/src/main/java/net/lecousin/framework/text/ByteArrayStringIso8859Buffer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ protected ByteArrayStringIso8859 createString(CharSequence s, int startPos, int
7070
return new ByteArrayStringIso8859(s, startPos, endPos);
7171
}
7272

73+
@Override
74+
protected ByteArrayStringIso8859 createString(char singleChar) {
75+
return new ByteArrayStringIso8859((byte)singleChar);
76+
}
77+
78+
@Override
79+
protected ByteArrayStringIso8859 createString(char[] chars) {
80+
ByteArrayStringIso8859 s = new ByteArrayStringIso8859(chars.length);
81+
s.append(chars);
82+
return s;
83+
}
84+
7385
@Override
7486
protected ByteArrayStringIso8859Buffer createBuffer() {
7587
return new ByteArrayStringIso8859Buffer();

0 commit comments

Comments
 (0)