3838import org .bouncycastle .openpgp .operator .jcajce .JcePBEDataDecryptorFactoryBuilder ;
3939import org .bouncycastle .util .io .Streams ;
4040import org .jetbrains .annotations .NotNull ;
41+ import org .labkey .api .util .FileUtil ;
4142import org .jetbrains .annotations .Nullable ;
4243import org .openqa .selenium .NotFoundException ;
4344
@@ -97,7 +98,7 @@ public abstract class TestFileUtils
9798
9899 public static String getFileContents (String rootRelativePath )
99100 {
100- return getFileContents (Paths . get ( getLabKeyRoot (), rootRelativePath ));
101+ return getFileContents (getLabKeyRoot (). toPath (). resolve ( rootRelativePath ));
101102 }
102103
103104 public static String getFileContents (final File file )
@@ -137,7 +138,7 @@ public static String getStreamContentsAsString(InputStream is) throws IOExceptio
137138 return StringUtils .join (IOUtils .readLines (is , Charset .defaultCharset ()).toArray (), System .lineSeparator ());
138139 }
139140
140- public static String getLabKeyRoot ()
141+ public static File getLabKeyRoot ()
141142 {
142143 if (_labkeyRoot == null )
143144 {
@@ -151,7 +152,7 @@ public static String getLabKeyRoot()
151152 {
152153 throw new IllegalStateException ("Specified LabKey root does not exist [" + _labkeyRoot + "]. Configure this by passing VM arg labkey.root={yourroot}" );
153154 }
154- if (!new File (_labkeyRoot , "server" ).exists ())
155+ if (!FileUtil . appendName (_labkeyRoot , "server" ).exists ())
155156 {
156157 throw new IllegalStateException ("Specified LabKey root exists [" + _labkeyRoot + "] but isn't the root of a LabKey enlistment. Configure this by passing VM arg labkey.root={yourroot}" );
157158 }
@@ -167,25 +168,25 @@ public static String getLabKeyRoot()
167168 _labkeyRoot = _labkeyRoot .getParentFile ().getParentFile (); // Working directory is in '{labkey.root}/server'; otherwise is in enlistment root
168169 else if (_labkeyRoot .getName ().equals ("server" ))
169170 _labkeyRoot = _labkeyRoot .getParentFile (); // Working directory is in '{labkey.root}/server'; otherwise is in enlistment root
170- else if (!new File (_labkeyRoot , "server" ).exists ())
171+ else if (!FileUtil . appendName (_labkeyRoot , "server" ).exists ())
171172 {
172173 throw new IllegalStateException ("Unable to locate enlistment. Working directory [" + _labkeyRoot + "] isn't a recognized location. Configure manually with passing VM arg labkey.root={yourroot}" );
173174 }
174175 }
175176 }
176- return _labkeyRoot . toString () ;
177+ return _labkeyRoot ;
177178 }
178179
179180 public static File getServerLogDir ()
180181 {
181- return new File ( getDefaultDeployDir (), "embedded/ logs" );
182+ return FileUtil . appendName ( FileUtil . appendName ( getDefaultDeployDir (), "embedded" ), " logs" );
182183 }
183184
184185 public static File getTestRoot ()
185186 {
186187 if (_testRoot == null )
187188 {
188- _testRoot = new File ( getLabKeyRoot (), "server/ testAutomation" );
189+ _testRoot = FileUtil . appendName ( FileUtil . appendName ( getLabKeyRoot (), "server" ), " testAutomation" );
189190 }
190191 return _testRoot ;
191192 }
@@ -199,20 +200,20 @@ public static File getTestBuildDir()
199200 {
200201 if (_buildDir == null )
201202 {
202- _buildDir = new File (getLabKeyRoot (), "build/modules/" + getTestProjectName ()); // Gradle
203+ _buildDir = FileUtil . appendPath (getLabKeyRoot (), org . labkey . api . util . Path . parse ( "build/modules/" + getTestProjectName () )); // Gradle
203204 }
204205 return _buildDir ;
205206 }
206207
207208 public static File getBaseFileRoot ()
208209 {
209210 // Files are a sibling of the modules directory
210- return new File (getModulesDir ().getParentFile (), "files" );
211+ return FileUtil . appendName (getModulesDir ().getParentFile (), "files" );
211212 }
212213
213214 public static File getGradleReportDir ()
214215 {
215- return new File (getTestBuildDir (), "test/logs/reports" );
216+ return FileUtil . appendPath (getTestBuildDir (), org . labkey . api . util . Path . parse ( "test/logs/reports" ) );
216217 }
217218
218219 /**
@@ -221,35 +222,35 @@ public static File getGradleReportDir()
221222 */
222223 static File getDefaultDeployDir ()
223224 {
224- return new File (getLabKeyRoot (), "build/deploy" );
225+ return FileUtil . appendPath (getLabKeyRoot (), org . labkey . api . util . Path . parse ( "build/deploy" ) );
225226 }
226227
227228 public static File getModulesDir ()
228229 {
229230 if (_modulesDir == null )
230231 {
231232 // Module root when deploying from embedded distribution
232- _modulesDir = new File (getDefaultDeployDir (), "embedded/modules" );
233+ _modulesDir = FileUtil . appendPath (getDefaultDeployDir (), org . labkey . api . util . Path . parse ( "embedded/modules" ) );
233234 if (!_modulesDir .isDirectory ())
234235 {
235- _modulesDir = new File (getDefaultDeployDir (), "modules" );
236+ _modulesDir = FileUtil . appendName (getDefaultDeployDir (), "modules" );
236237 }
237238 }
238239 return _modulesDir ;
239240 }
240241
241242 public static File getDefaultFileRoot (String containerPath )
242243 {
243- return new File (getBaseFileRoot (), containerPath + "/@files" );
244+ return FileUtil . appendPath (getBaseFileRoot (), org . labkey . api . util . Path . parse ( containerPath + "/@files" ) );
244245 }
245246
246247 public static String getDefaultWebAppRoot ()
247248 {
248- File path = new File (getModulesDir ().getParentFile (), "labkeyWebapp" );
249+ File path = FileUtil . appendName (getModulesDir ().getParentFile (), "labkeyWebapp" );
249250 if (!path .isDirectory ())
250251 {
251252 // Casing is different when deployed from an embedded distribution
252- path = new File (getModulesDir ().getParentFile (), "labkeywebapp" );
253+ path = FileUtil . appendName (getModulesDir ().getParentFile (), "labkeywebapp" );
253254 }
254255 return path .toString ();
255256 }
@@ -300,7 +301,7 @@ public static List<File> getSampleDatas(String relativePath)
300301
301302 for (File sampledataDir : sampledataDirs )
302303 {
303- File checkFile = new File (sampledataDir , relativePath );
304+ File checkFile = FileUtil . appendPath (sampledataDir , org . labkey . api . util . Path . parse ( relativePath ) );
304305 if (checkFile .exists ())
305306 {
306307 foundFiles .add (checkFile );
@@ -317,16 +318,16 @@ public static Set<File> getSampleDataDirs()
317318 {
318319 _sampledataDirs = new TreeSet <>();
319320
320- File sampledataDirsFile = new File (getTestBuildDir (), "sampledata.dirs" );
321+ File sampledataDirsFile = FileUtil . appendName (getTestBuildDir (), "sampledata.dirs" );
321322 if (sampledataDirsFile .exists ())
322323 {
323324 String path = getFileContents (sampledataDirsFile );
324325 _sampledataDirs .addAll (Arrays .stream (path .split (";" )).map (File ::new ).toList ());
325326 }
326327 else
327328 {
328- _sampledataDirs .add (new File (getTestRoot (), "data" ));
329- Path modulesDir = new File (getLabKeyRoot (), "server/modules" ).toPath ();
329+ _sampledataDirs .add (FileUtil . appendName (getTestRoot (), "data" ));
330+ Path modulesDir = FileUtil . appendPath (getLabKeyRoot (), org . labkey . api . util . Path . parse ( "server/modules" ) ).toPath ();
330331 try
331332 {
332333 // We know where the modules live; no reason to insist that sampledata.dirs exists.
@@ -363,8 +364,8 @@ public static Set<File> getSampleDataDirs()
363364
364365 public static File getTestTempDir ()
365366 {
366- File buildDir = new File (getLabKeyRoot (), "build" );
367- return new File (buildDir , "testTemp" );
367+ File buildDir = FileUtil . appendName (getLabKeyRoot (), "build" );
368+ return FileUtil . appendName (buildDir , "testTemp" );
368369 }
369370
370371 /**
@@ -378,7 +379,7 @@ public static File ensureTestTempDir(String... children) throws IOException
378379 File file = getTestTempDir ();
379380 for (String child : children )
380381 {
381- file = new File (file , child );
382+ file = FileUtil . appendName (file , child );
382383 }
383384
384385 FileUtils .forceMkdir (file );
@@ -398,7 +399,7 @@ public static File ensureTestTempFile(String... children) throws IOException
398399
399400 for (String child : children )
400401 {
401- file = new File (file , child );
402+ file = FileUtil . appendName (file , child );
402403 }
403404
404405 if (file .toString ().length () == getTestTempDir ().toString ().length ())
@@ -448,7 +449,7 @@ private static void checkFileLocation(File file)
448449 {
449450 try
450451 {
451- if (!FileUtils .directoryContains (new File ( getLabKeyRoot () ), file ))
452+ if (!FileUtils .directoryContains (getLabKeyRoot (), file ))
452453 {
453454 // TODO: Consider throwing IllegalArgumentException
454455 LOG .info ("DEBUG: Attempting to delete a file outside of test enlistment: " + getLabKeyRoot ());
@@ -457,30 +458,6 @@ private static void checkFileLocation(File file)
457458 catch (IOException ignore ) { }
458459 }
459460
460- /**
461- *
462- * @param dir Location to create new file
463- * @param fileName Name of file to be created
464- * @param contents Text contents of file
465- * @return File object pointing to new file
466- * @deprecated Use {@link #writeFile(File, String)} or {@link #writeTempFile(String, String)}
467- */
468- @ Deprecated
469- public static File saveFile (File dir , String fileName , String contents )
470- {
471- File tsvFile = new File (dir , fileName );
472-
473- try
474- {
475- return writeFile (tsvFile , contents );
476- }
477- catch (IOException e )
478- {
479- e .printStackTrace (System .err );
480- return null ;
481- }
482- }
483-
484461 /**
485462 * Write text to a file in the test temp directory. Temp directory will be created if it does not exist.
486463 * @param name Name of the file to be created. An existing file will be overwritten
@@ -490,7 +467,7 @@ public static File saveFile(File dir, String fileName, String contents)
490467 */
491468 public static File writeTempFile (String name , InputStream contents ) throws IOException
492469 {
493- File file = new File (getTestTempDir (), name );
470+ File file = FileUtil . appendPath (getTestTempDir (), org . labkey . api . util . Path . parse ( name ) );
494471 FileUtils .forceMkdirParent (file );
495472
496473 FileUtils .copyInputStreamToFile (contents , file );
@@ -506,7 +483,7 @@ public static File writeTempFile(String name, InputStream contents) throws IOExc
506483 */
507484 public static File writeTempFile (String name , String contents ) throws IOException
508485 {
509- File file = new File (getTestTempDir (), name );
486+ File file = FileUtil . appendPath (getTestTempDir (), org . labkey . api . util . Path . parse ( name ) );
510487 FileUtils .forceMkdirParent (file );
511488
512489 return writeFile (file , contents );
@@ -587,7 +564,7 @@ public static List<File> unzipToDirectory(File sourceZip, File unzipDir) throws
587564
588565 while (null != (entry = zis .getNextEntry ()))
589566 {
590- File destFile = new File (unzipDir , entry .getName ());
567+ File destFile = FileUtil . appendName (unzipDir , entry .getName ());
591568
592569 if (!destFile .getCanonicalPath ().startsWith (unzipDir .getCanonicalPath () + File .separator )) {
593570 throw new IOException ("Zip entry is outside of the target dir: " + entry .getName ());
@@ -641,7 +618,7 @@ private static List<File> unTar(final File inputFile, final File outputDir) thro
641618
642619 while ((entry = inputStream .getNextEntry ()) != null )
643620 {
644- final File outputFile = new File (outputDir , entry .getName ());
621+ final File outputFile = FileUtil . appendPath (outputDir , org . labkey . api . util . Path . parse ( entry .getName () ));
645622
646623 if (!outputFile .toPath ().normalize ().startsWith (normalizedOutputPath ))
647624 throw new IOException ("Bad zip entry (" + entry .getName () + ") in " + inputFile .getAbsolutePath ());
@@ -675,7 +652,7 @@ private static List<File> unTar(final File inputFile, final File outputDir) thro
675652 */
676653 private static File unGzip (final File inputFile , final File outputDir ) throws IOException
677654 {
678- final File outputFile = new File (outputDir , inputFile .getName ().substring (0 , inputFile .getName ().length () - 3 ));
655+ final File outputFile = FileUtil . appendName (outputDir , inputFile .getName ().substring (0 , inputFile .getName ().length () - 3 ));
679656
680657 try (GZIPInputStream in = new GZIPInputStream (new FileInputStream (inputFile ));
681658 FileOutputStream out = new FileOutputStream (outputFile ))
0 commit comments