### Question:
BuildVariableResolver implements VariableResolver<String> { public String resolve(final String variable) { try { if (lazyResolvers.containsKey(variable)) { return lazyResolvers.get(variable).getValue(); } else { if (computer != null) { otherResolvers.add(new VariableResolver.ByMap<String>(computer.getEnvironment())); } return new VariableResolver.Union<String>(otherResolvers).resolve(variable); } } catch (Exception e) { LOGGER.warning("Variable name '" + variable + "' look up failed because of " + e); } return null; } BuildVariableResolver(final Job<?, ?> job);  BuildVariableResolver(final Job<?, ?> project, final Computer computer);  BuildVariableResolver(final AbstractBuild<?, ?> build, final Computer computer); String resolve(final String variable);  }### Answer:
@Test public void assertJobNameIsResolved() { when(project.getName()).thenReturn("ThisIsAJob"); BuildVariableResolver resolver = new BuildVariableResolver(project, computer); assertEquals("Variable resolution was incorrect", "ThisIsAJob", resolver.resolve("JOB_NAME")); verifyZeroInteractions(computer); }
@Test public void assertJobNameWithoutComputerIsResolved() { when(project.getName()).thenReturn("ThisIsAJob"); BuildVariableResolver resolver = new BuildVariableResolver(project); assertEquals("Variable resolution was incorrect", "ThisIsAJob", resolver.resolve("JOB_NAME")); assertNull("Variable resolution was performed", resolver.resolve("NONE_EXISTING_KEY")); }
@Test public void assertComputerEnvVarIsResolved() throws Exception { EnvVars map = new EnvVars(); map.put("ENV_VAR", "This is an env var"); when(computer.getEnvironment()).thenReturn(map); BuildVariableResolver resolver = new BuildVariableResolver(project, computer); assertEquals("Variable resolution was incorrect", "This is an env var", resolver.resolve("ENV_VAR")); verifyZeroInteractions(project); }
@Test public void assertComputerUserNameIsResolved() throws Exception { Map<Object, Object> map = new HashMap<Object, Object>(); map.put("user.name", "Other_user"); when(computer.getSystemProperties()).thenReturn(map); BuildVariableResolver resolver = new BuildVariableResolver(project, computer); assertEquals("Variable resolution was incorrect", "Other_user", resolver.resolve("USER_NAME")); verifyZeroInteractions(project); }
@Test public void assertNodeNameIsResolved() { when(computer.getName()).thenReturn("AKIRA"); BuildVariableResolver resolver = new BuildVariableResolver(project, computer); assertEquals("Variable resolution was incorrect", "AKIRA", resolver.resolve("NODE_NAME")); verifyZeroInteractions(project); }
@Test public void assertMasterNodeNameIsResolved() { when(computer.getName()).thenReturn(""); BuildVariableResolver resolver = new BuildVariableResolver(project, computer); assertEquals("Variable resolution was incorrect", "MASTER", resolver.resolve("NODE_NAME")); verifyZeroInteractions(project); }
@Test public void assertNoComputeraDoesNotThrowNPEWhenResolvingNodeName() { BuildVariableResolver resolver = new BuildVariableResolver(project); assertNull("Variable resolution was incorrect", resolver.resolve("NODE_NAME")); verifyZeroInteractions(project); }
@Test public void assertBuildEnvVarIsResolved() throws Exception { EnvVars map = new EnvVars(); map.put("BUILD_ID", "121212"); when(build.getProject()).thenReturn(project); when(build.getEnvironment(TaskListener.NULL)).thenReturn(map); BuildVariableResolver resolver = new BuildVariableResolver(build, computer); assertEquals("Variable resolution was incorrect", "121212", resolver.resolve("BUILD_ID")); verify(build).getEnvironment(TaskListener.NULL); verifyZeroInteractions(project); }