Skip to content

Commit c8f5572

Browse files
committed
add a test trying to reproduce #209
Signed-off-by: Ludovic Orban <[email protected]>
1 parent ccca9e5 commit c8f5572

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Diff for: src/test/java/org/mortbay/jetty/orchestrator/nodefs/NodeFileSystemTest.java

+98
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@
1616
import java.io.File;
1717
import java.io.FileOutputStream;
1818
import java.io.IOException;
19+
import java.io.OutputStream;
1920
import java.net.URI;
2021
import java.nio.file.DirectoryStream;
2122
import java.nio.file.FileSystem;
2223
import java.nio.file.FileSystems;
2324
import java.nio.file.Files;
2425
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import java.nio.file.StandardOpenOption;
2528
import java.util.HashMap;
2629
import java.util.Iterator;
30+
import java.util.Objects;
2731
import java.util.Spliterator;
2832
import java.util.Spliterators;
2933
import java.util.concurrent.TimeUnit;
34+
import java.util.stream.Stream;
3035
import java.util.stream.StreamSupport;
3136

3237
import org.apache.sshd.client.SshClient;
@@ -91,6 +96,99 @@ public void testNodeIdFolder() throws Exception
9196
assertThat(iterator.hasNext(), is(false));
9297
}
9398

99+
@Test
100+
public void testCopy() throws Exception
101+
{
102+
new File("target/testCopy/." + NodeFileSystemProvider.SCHEME + "/the-test/myhost/a").mkdirs();
103+
104+
for (String classpathEntry : System.getProperty("java.class.path").split(File.pathSeparator))
105+
{
106+
File cpFile = new File(classpathEntry);
107+
if (cpFile.isDirectory())
108+
continue;
109+
copyFile(cpFile.toPath(), Paths.get("target/testCopy/.jco/the-test/myhost", cpFile.getName()));
110+
}
111+
112+
TestSshServer testSshServer = closer.register(new TestSshServer("target/testCopy"));
113+
SshClient sshClient = closer.register(SshClient.setUpDefaultClient());
114+
sshClient.start();
115+
closer.register(sshClient.connect(null, "localhost", testSshServer.getPort())
116+
.verify(30, TimeUnit.SECONDS)
117+
.getSession());
118+
119+
HashMap<String, Object> env = new HashMap<>();
120+
env.put(NodeFileSystemProvider.IS_WINDOWS_ENV, IS_WINDOWS);
121+
env.put(NodeFileSystemProvider.SFTP_HOST_ENV, "localhost");
122+
env.put(NodeFileSystemProvider.SFTP_PORT_ENV, testSshServer.getPort());
123+
env.put(NodeFileSystemProvider.SFTP_USERNAME_ENV, System.getProperty("user.name"));
124+
env.put(SshClient.class.getName(), sshClient);
125+
NodeFileSystem fileSystem = closer.register((NodeFileSystem)FileSystems.newFileSystem(URI.create(NodeFileSystemProvider.SCHEME + ":the-test/myhost!/." + NodeFileSystemProvider.SCHEME + "/the-test/myhost"), env));
126+
127+
Path targetPath = Paths.get("target/testCopy-target/");
128+
Files.createDirectories(targetPath);
129+
Path sourcePath = fileSystem.getPath(".");
130+
131+
copyDir(sourcePath, targetPath);
132+
}
133+
134+
public static void copyDir(Path srcDir, Path destDir) throws IOException
135+
{
136+
if (!Files.isDirectory(Objects.requireNonNull(srcDir)))
137+
throw new IllegalArgumentException("Source is not a directory: " + srcDir);
138+
Objects.requireNonNull(destDir);
139+
if (Files.exists(destDir) && !Files.isDirectory(destDir))
140+
throw new IllegalArgumentException("Destination is not a directory: " + destDir);
141+
else if (!Files.exists(destDir))
142+
Files.createDirectory(destDir); // only attempt top create 1 level of directory (parent must exist)
143+
144+
try (Stream<Path> sourceStream = Files.walk(srcDir))
145+
{
146+
Iterator<Path> iterFiles = sourceStream
147+
.filter(Files::isRegularFile)
148+
.iterator();
149+
while (iterFiles.hasNext())
150+
{
151+
Path sourceFile = iterFiles.next();
152+
Path relative = srcDir.relativize(sourceFile);
153+
Path destFile = resolvePath(destDir, relative);
154+
if (!Files.exists(destFile.getParent()))
155+
Files.createDirectories(destFile.getParent());
156+
copyFile(sourceFile, destFile);
157+
}
158+
}
159+
}
160+
161+
public static void copyFile(Path srcFile, Path destFile) throws IOException
162+
{
163+
if (!Files.isRegularFile(Objects.requireNonNull(srcFile)))
164+
throw new IllegalArgumentException("Source is not a file: " + srcFile);
165+
Objects.requireNonNull(destFile);
166+
167+
try (OutputStream out = Files.newOutputStream(destFile,
168+
StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))
169+
{
170+
Files.copy(srcFile, out);
171+
}
172+
}
173+
174+
public static Path resolvePath(Path basePath, Path relative)
175+
{
176+
if (relative.isAbsolute())
177+
throw new IllegalArgumentException("Relative path cannot be absolute");
178+
179+
if (basePath.getFileSystem().equals(relative.getFileSystem()))
180+
{
181+
return basePath.resolve(relative);
182+
}
183+
else
184+
{
185+
for (Path segment : relative)
186+
basePath = basePath.resolve(segment.toString());
187+
return basePath;
188+
}
189+
}
190+
191+
94192
@Test
95193
public void testHomeFolderIsDefault() throws Exception
96194
{

0 commit comments

Comments
 (0)