This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
IFileHandlerFactoryTest.java
312 lines (264 loc) · 8.11 KB
/
IFileHandlerFactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*******************************************************************************
* Copyright (c) 2019- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Initial API and implementation and/or initial documentation -
* Jay Jay Billings, Joe Osborn
*******************************************************************************/
package org.eclipse.ice.tests.commands;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.ice.commands.CommandStatus;
import org.eclipse.ice.commands.FileHandlerFactory;
import org.eclipse.ice.commands.IFileHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* This class implements several test methods for
* {@link org.eclipse.ice.commands.FileHandlerFactory}
*
* @author Joe Osborn
*
*/
public class IFileHandlerFactoryTest {
/**
* A default factory with which to create FileHandler instances
*/
FileHandlerFactory factory = new FileHandlerFactory();
/**
* A command factory test that is only useful for accessing some of its member
* functions
*/
CommandFactoryTest factorytest = new CommandFactoryTest();
/**
* A string which contains a local temporary source file to work with
*/
String localSource;
/**
* A string which contains a local temporary destination directory to work with
*/
String localDestination;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// First create a dummy text file to test
String source = "dummyfile.txt";
Path sourcePath = null;
try {
sourcePath = Files.createTempFile(null, source);
} catch (IOException e) {
e.printStackTrace();
}
// Turn the path into a string to pass to the command
localSource = sourcePath.toString();
System.out.println("Created source file at: " + localSource);
// Do the same for the destination
Path destinationPath = null;
String dest = "testCopyDirectory";
try {
destinationPath = Files.createTempDirectory(dest);
} catch (IOException e) {
e.printStackTrace();
}
// Turn the path into a string to give to the command
localDestination = destinationPath.toString();
System.out.println("Created destination file at: " + localDestination);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
System.out.println("Delete temporary files/directories that were created.");
// Get the paths
Path sourcePath = Paths.get(localSource);
Path destPath = Paths.get(localDestination);
// Delete the files
try {
Files.deleteIfExists(sourcePath);
} catch (NoSuchFileException e) {
System.err.format("%s: no such" + " file or directory%n", sourcePath);
e.printStackTrace();
} catch (DirectoryNotEmptyException e) {
System.err.format("%s not empty%n", sourcePath);
e.printStackTrace();
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
try {
Files.deleteIfExists(destPath);
} catch (NoSuchFileException e) {
System.err.format("%s: no such" + " file or directory%n", destPath);
e.printStackTrace();
} catch (DirectoryNotEmptyException e) {
// If the directory is not empty, that is because it was a move command
// and the moved file is in there. So delete the file first and then
// delete the directory
File localDestinationFile = new File(localDestination);
boolean deleted = deleteDirectory(localDestinationFile);
// Something went wrong and couldn't be deleted
if (!deleted) {
System.err.println(e);
e.printStackTrace();
}
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
}
}
/**
* Test method for
* {@link org.eclipse.ice.commands.FileHandlerFactory#getFileHandler()}
* and local file copying.
*/
@Test
public void testLocalFileHandlerFactoryCopyCommand() {
IFileHandler handler = null;
// Get the file transfer handler
try {
handler = factory.getFileHandler();
} catch (IOException e) {
e.printStackTrace();
}
// Now try to copy the file
try {
CommandStatus status = handler.copy(localSource, localDestination);
assert (status == CommandStatus.SUCCESS);
} catch (IOException e) {
e.printStackTrace();
}
// Check that the file exists now
try {
boolean exist = handler.exists(localDestination);
assert (exist == true);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Test method for
* {@link org.eclipse.ice.commands.FileHandlerFactory#getFileHandler()}
* and local file moving.
*/
@Test
public void testLocalFileHandlerFactoryMoveCommand() {
IFileHandler handler = null;
// Get the file transfer handler
try {
handler = factory.getFileHandler();
} catch (IOException e) {
e.printStackTrace();
}
// Now try to move the file
try {
CommandStatus status = handler.move(localSource, localDestination);
assert (status == CommandStatus.SUCCESS);
} catch (IOException e) {
e.printStackTrace();
}
// Check that the file exists now
try {
boolean exist = handler.exists(localDestination);
assert (exist == true);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Test method for a source file that exists but a destination directory that
* does not exist. Tests
* {@link org.eclipse.ice.commands.FileHandlerFactory#getFileHandler()}
*/
@Test
public void testLocalFileHandlerFactoryDestinationNonExistant() {
IFileHandler handler = null;
String newDirectory = "/some/new/directory/";
// Get the file transfer handler with a nonexistent destination
try {
handler = factory.getFileHandler();
} catch (IOException e) {
e.printStackTrace();
}
// Now try to move the file
try {
CommandStatus status = handler.move(localSource, localDestination + newDirectory);
assert (status == CommandStatus.SUCCESS);
} catch (IOException e) {
e.printStackTrace();
}
// Check that the file exists now
try {
boolean exist = handler.exists(localDestination + newDirectory);
assert (exist == true);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method intends to test the functionality of moving a new file not to a new
* directory but just to a new name in the same directory
*/
@Test
public void testLocalFileHandlerFactoryChangeName() {
IFileHandler handler = null;
// Make the new file name be the same file in the same directory, just a new name
String[] tokens = localSource.split("/");
String localNewName = "";
for (int i = 0; i < tokens.length - 1; i++)
localNewName += tokens[i] + "/" ;
localNewName += "NewFileName.txt";
System.out.println("New file path: " + localNewName);
try {
handler = factory.getFileHandler();
} catch (IOException e) {
e.printStackTrace();
}
try {
CommandStatus status = handler.move(localSource, localNewName);
assert (status == CommandStatus.SUCCESS);
} catch(IOException e) {
e.printStackTrace();
}
// Check that the file exists now
try {
boolean exist = handler.exists(localNewName);
assert (exist == true);
} catch (IOException e) {
e.printStackTrace();
}
// If the file was successfully created, delete it here
// Needs a special delete since the filename was created in this function
File fileToDelete = new File(localNewName);
fileToDelete.delete();
}
/**
* A simple test method to recursively delete temporary files/directories
* created in this test class
*
* @param directory - top level directory from which to delete everything
* underneath
* @return - boolean - true if everything deleted, false if not
*/
private boolean deleteDirectory(File directory) {
File[] contents = directory.listFiles();
if (contents != null) {
for (File file : contents) {
deleteDirectory(file);
}
}
return directory.delete();
}
}