private int getIntField(Object service, String fieldName) { return Integer.parseInt(ReflectionUtil.getField(service, fieldName) + ""); }
public void run() { try { waitForLauncherCreation.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } ReflectionUtil.setField(spyBootstrapper, "loop", false); } });
@After public void teardown() { ReflectionUtil.setStaticField(JobConsoleLogger.class, "context", null); }
@Test public void shouldGenerateTheSameP4ClientValueForCommandAndEnvironment() throws Exception { P4Material p4Material = new P4Material("server:10", "out-of-the-window"); ReflectionUtil.setField(p4Material, "folder", "crapy_dir"); P4Client p4Client = p4Material._p4(tempDir, new InMemoryStreamConsumer(), false); assertThat(p4Client, is(not(nullValue()))); String client = (String) ReflectionUtil.getField(p4Client, "p4ClientName"); assertThat(client, is(p4Material.clientName(tempDir))); }
@Test public void shouldSetAndUnsetContext() { final TaskExecutionContext context = mock(TaskExecutionContext.class); JobConsoleLoggerInternal.setContext(context); assertThat(ReflectionUtil.getStaticField(JobConsoleLogger.class, "context"), is(context)); JobConsoleLoggerInternal.unsetContext(); assertThat(ReflectionUtil.getStaticField(JobConsoleLogger.class, "context"), is(nullValue())); } }
@Test public void shouldNotRefreshWorkingDirectoryIfDefaultRemoteUrlDoesNotContainPasswordButMaterialUrlDoes() throws Exception { final HgMaterial material = new HgMaterial("http://user:pwd@domain:9999/path", null); final HgCommand hgCommand = mock(HgCommand.class); final ConsoleResult consoleResult = mock(ConsoleResult.class); when(consoleResult.outputAsString()).thenReturn("http://user@domain:9999/path"); when(hgCommand.workingRepositoryUrl()).thenReturn(consoleResult); assertFalse((Boolean) ReflectionUtil.invoke(material, "isRepositoryChanged", hgCommand)); }
private static Field field(Object o, String name) throws NoSuchFieldException { return field(name, o.getClass()); }
@Test public void shouldCreatePermittedAgentContextCorrectly() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); windows.disable(); Agents matchingAgents = new Agents(linux, windows); EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); ReflectionUtil.setField(context, "rerun", true); SchedulingContext permittedAgentContext = context.permittedAgent("uuid1"); Agents agents = (Agents) ReflectionUtil.getField(permittedAgentContext, "agents"); assertThat(agents.size(), is(1)); assertThat(agents.get(0).getAgentIdentifier().getUuid(), is("uuid1")); assertThat(permittedAgentContext.isRerun(), is(true)); assertThat(permittedAgentContext.getApprovedBy(), is("approver")); EnvironmentVariablesConfig environmentVariablesUsed = permittedAgentContext.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldOnlyCreateOneInstanceOfCustomizedFileEditorAndUseIt() throws Exception { GoConfigFieldTypeConverter converter = new GoConfigFieldTypeConverter(); Field expectedField = converter.getClass().getDeclaredField("propertyEditor"); int modifier = expectedField.getModifiers(); assertThat(Modifier.isStatic(modifier), is(true)); assertThat(Modifier.isFinal(modifier), is(true)); PropertyEditor actual = converter.findCustomEditor(File.class, null); assertThat(actual, is(ReflectionUtil.getStaticField(converter.getClass(), "propertyEditor"))); } }
@Test public void shouldRefreshWorkingDirectoryIfUsernameInDefaultRemoteUrlIsDifferentFromOneInMaterialUrl() throws Exception { final HgMaterial material = new HgMaterial("http://some_new_user:pwd@domain:9999/path", null); final HgCommand hgCommand = mock(HgCommand.class); final ConsoleResult consoleResult = mock(ConsoleResult.class); when(consoleResult.outputAsString()).thenReturn("http://user:pwd@domain:9999/path"); when(hgCommand.workingRepositoryUrl()).thenReturn(consoleResult); assertTrue((Boolean) ReflectionUtil.invoke(material, "isRepositoryChanged", hgCommand)); }
public static Object getField(Object o, String name) { try { return field(o, name).get(o); } catch (Exception e) { throw new RuntimeException(e); } }
public void onTearDown() { Process process = (Process) ReflectionUtil.getField(p4dProcess, "process"); process.destroy(); FileUtils.deleteQuietly(tempRepo); FileUtils.deleteQuietly(clientFolder); }
public void run() { try { waitForLauncherInvocation.acquire(); } catch (InterruptedException e) { throw new RuntimeException(e); } ReflectionUtil.setField(spyBootstrapper, "loop", false); } });
@Before public void setUp() throws Exception { ServletHelper servletHelper = mock(ServletHelper.class); ReflectionUtil.setStaticField(ServletHelper.class, "instance", servletHelper); servletRequestWrapper = mock(com.thoughtworks.go.server.util.ServletRequest.class); httpServletRequest = mock(HttpServletRequest.class); when(httpServletRequest.getRequestURI()).thenReturn("/go/rails/stuff/action"); when(servletHelper.getRequest(httpServletRequest)).thenReturn(servletRequestWrapper); }
@Test public void shouldRegisterTaskConfigDuringExecutionAndUnregisterOnSuccessfulCompletion() throws CruiseControlException { final PluggableTaskBuilder builder = spy(new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, "", "")); taskExtension = mock(TaskExtension.class); when(taskExtension.execute(eq(TEST_PLUGIN_ID), any(ActionWithReturn.class))).thenReturn(ExecutionResult.success("yay")); builder.build(goPublisher, variableContext, taskExtension, null, null, "utf-8"); assertThat(ReflectionUtil.getStaticField(JobConsoleLogger.class, "context"), is(nullValue())); }
@Test public void shouldClone() throws Exception { CaseInsensitiveString foo = new CaseInsensitiveString("foo"); CaseInsensitiveString fooClone = (CaseInsensitiveString) ReflectionUtil.invoke(foo, "clone"); assertThat(foo, is(fooClone)); assertThat(foo, not(sameInstance(fooClone))); }
public static void setField(Object o, String name, Object value) { try { field(o, name).set(o, value); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
public void stop() throws Exception { if (processWrapper != null) { Process process = (Process) ReflectionUtil.getField(processWrapper, "process"); process.destroy(); processWrapper.waitForExit(); processWrapper = null; } }
public void setupDESCipherFile(String cipher) throws IOException { ReflectionUtil.setField(desCipherProvider, "cachedKey", null); FileUtils.writeStringToFile(systemEnvironment.getDESCipherFile(), cipher, UTF_8); } }
@Before public void setup() throws NoSuchMethodException { taskExecutionContext = mock(TaskExecutionContext.class); mockedConsole = mock(com.thoughtworks.go.plugin.api.task.Console.class); when(taskExecutionContext.console()).thenReturn(mockedConsole); environment = mock(EnvironmentVariables.class); when(taskExecutionContext.environment()).thenReturn(environment); ReflectionUtil.setStaticField(JobConsoleLogger.class, "context", taskExecutionContext); consoleLogger = JobConsoleLogger.getConsoleLogger(); }