Mercurial > octave
changeset 27877:0850f7c37970
javarmpath.m: Allow cell array of string inputs.
* javarmpath.m: Update documentation with new calling form and description of
cell array of strings input. Update input validation test to allow for
cellstr inputs. Add inner for loop to unpack cell array of strings if present.
Add BIST tests for functionality and input validation.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 27 Dec 2019 10:34:14 -0800 |
parents | 76373f5113db |
children | 2b78bc0ef3c5 |
files | scripts/java/javarmpath.m |
diffstat | 1 files changed, 77 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/java/javarmpath.m Fri Dec 27 10:13:17 2019 -0800 +++ b/scripts/java/javarmpath.m Fri Dec 27 10:34:14 2019 -0800 @@ -20,12 +20,14 @@ ## -*- texinfo -*- ## @deftypefn {} {} javarmpath (@var{clspath}) ## @deftypefnx {} {} javarmpath (@var{clspath1}, @dots{}) +## @deftypefnx {} {} javarmpath (@{@var{clspath1}, @dots{}@}) ## Remove @var{clspath} from the dynamic class path of the Java virtual ## machine. ## ## @var{clspath} may either be a directory where @file{.class} files are found, ## or a @file{.jar} file containing Java classes. Multiple paths may be -## removed at once by specifying additional arguments. +## removed at once by specifying additional arguments, or by using a cell array +## of strings. ## @seealso{javaaddpath, javaclasspath} ## @end deftypefn @@ -35,25 +37,85 @@ print_usage (); endif - for i = 1:numel (varargin) - clspath = varargin{i}; - if (! ischar (clspath)) - error ("javarmpath: CLSPATH must be a string"); + if (! all (cellfun (@(c) ischar (c) || iscellstr (c), varargin))) + error ("javarmpath: arguments must be strings or cell array of strings"); + endif + + for arg = varargin + if (iscellstr (arg{1})) + arg = arg{1}(:).'; # Guarantee cellstr array is a row vector endif - old_path = canonicalize_file_name (tilde_expand (clspath)); - if (isfolder (old_path)) - if (old_path(end) != filesep ()) - old_path = [old_path, filesep()]; + for clspath = arg + clspath = clspath{1}; + + old_path = canonicalize_file_name (tilde_expand (clspath)); + if (isfolder (old_path)) + if (old_path(end) != filesep ()) + old_path = [old_path, filesep()]; + endif endif - endif - success = javaMethod ("removeClassPath", "org.octave.ClassHelper", - old_path); + success = javaMethod ("removeClassPath", "org.octave.ClassHelper", + old_path); - if (! success) - warning ("javarmpath: %s: not found in Java classpath", old_path); - endif + if (! success) + warning ("javarmpath: %s: not found in Java classpath", old_path); + endif + endfor endfor endfunction + + +## FIXME: These tests may fail if either TEMPDIR or HOME have already +## been added to the Java class path. + +## Basic test with single string +%!test +%! pth = tempdir (); +%! unwind_protect +%! javaaddpath (pth); +%! clspth1 = javaclasspath ("-dynamic"); +%! javarmpath (pth); +%! clspth2 = javaclasspath ("-dynamic"); +%! assert (numel (clspth2), numel (clspth1) - 1); +%! assert (clspth2(1:end), clspth1(2:end)); +%! unwind_protect_cleanup +%! javarmpath (pth); +%! end_unwind_protect + +## Remove two strings +%!test +%! pth1 = tempdir (); +%! pth2 = tilde_expand ("~"); +%! unwind_protect +%! javaaddpath (pth1, pth2); +%! clspth1 = javaclasspath ("-dynamic"); +%! javarmpath (pth1, pth2); +%! clspth2 = javaclasspath ("-dynamic"); +%! assert (numel (clspth2), numel (clspth1) - 2); +%! assert (clspth2(1:end), clspth1(3:end)); +%! unwind_protect_cleanup +%! javarmpath (pth1, pth2); +%! end_unwind_protect + +## Remove cell array of two strings +%!test +%! pth1 = tempdir (); +%! pth2 = tilde_expand ("~"); +%! unwind_protect +%! javaaddpath (pth1, pth2); +%! clspth1 = javaclasspath ("-dynamic"); +%! javarmpath ({pth1, pth2}); +%! clspth2 = javaclasspath ("-dynamic"); +%! assert (numel (clspth2), numel (clspth1) - 2); +%! assert (clspth2(1:end), clspth1(3:end)); +%! unwind_protect_cleanup +%! javarmpath (pth1, pth2); +%! end_unwind_protect + +## Test input validation +%!error <Invalid call> javarmpath () +%!error <arguments must be strings> javarmpath (5) +%!error <arguments must be .* cell array of strings> javarmpath ({5})