DockerUtils { public static Object prepareParameter( String rawValue, Class<?> clazz ) throws TargetException { Object result; if( clazz == int.class || clazz == Integer.class ) result = Integer.parseInt( rawValue ); else if( clazz == long.class || clazz == Long.class ) result = Long.parseLong( rawValue ); else if( clazz == boolean.class || clazz == Boolean.class ) result = Boolean.parseBoolean( rawValue ); else if( clazz == String[].class ) { List<String> parts = Utils.splitNicely( rawValue, "," ); result = parts.toArray( new String[ parts.size()]); } else if( clazz == Capability[].class ) { List<Capability> caps = new ArrayList<> (); for( String s : Utils.splitNicely( rawValue, "," )) { try { caps.add( Capability.valueOf( s )); } catch( Exception e ) { throw new TargetException( "Unknown capability: " + s ); } } result = caps.toArray( new Capability[ caps.size()]); } else result = rawValue; return result; } private  DockerUtils(); static DockerClient createDockerClient( Map<String,String> targetProperties ); static void deleteImageIfItExists( String imageId, DockerClient dockerClient ); static Image findImageByIdOrByTag( String name, DockerClient dockerClient ); static Image findImageById( String imageId, List<Image> images ); static Image findImageByTag( String imageTag, List<Image> images ); static Container findContainerByIdOrByName( String name, DockerClient dockerClient ); static ContainerState getContainerState( String containerId, DockerClient dockerClient ); static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ); static Object prepareParameter( String rawValue, Class<?> clazz ); static boolean extractBoolean( Boolean bool ); static String findDefaultImageVersion( String rawVersion ); static String buildContainerNameFrom( String scopedInstancePath, String applicationName ); static final String LATEST; }
@Test public void testPrepareParameter() throws Exception { String input = "whatever"; Assert.assertEquals( input, DockerUtils.prepareParameter( input, String.class )); input = "50"; Assert.assertEquals( 50, DockerUtils.prepareParameter( input, int.class )); Assert.assertEquals( 50L, DockerUtils.prepareParameter( input, long.class )); input = "true"; Assert.assertEquals( true, DockerUtils.prepareParameter( input, boolean.class )); input = "test1, test2,test3, test4 "; Object result = DockerUtils.prepareParameter( input, String[].class ); Assert.assertEquals( String[].class, result.getClass()); String[] expectedString = { "test1", "test2", "test3", "test4" }; Assert.assertTrue( Arrays.deepEquals( expectedString, (String[]) result )); input = "AUDIT_CONTROL, SYS_PTRACE"; Capability[] expectedCapabilities = new Capability[] { Capability.AUDIT_CONTROL, Capability.SYS_PTRACE }; result = DockerUtils.prepareParameter( input, Capability[].class ); Assert.assertEquals( Capability[].class, result.getClass()); Assert.assertTrue( Arrays.deepEquals( expectedCapabilities, (Capability[]) result )); input = "0.4f"; Assert.assertEquals( input, DockerUtils.prepareParameter( input, double.class )); }
@Test( expected = TargetException.class ) public void testPrepareParameter_invalidCapability() throws Exception { String input = "AUDIT_CONTROL, read minds"; DockerUtils.prepareParameter( input, Capability[].class ); }