private void assertHasEnvironment(ApplicationContext ctx, Environment expectedEnv) { // ensure the custom environment took Environment actualEnv = ctx.getEnvironment(); assertThat(actualEnv, notNullValue()); assertThat(actualEnv, is(expectedEnv)); // ensure environment is registered as a bean assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME), is(true)); }
@Test public void testSpr12701() throws Exception { ResolvableType resolvableType = ResolvableType.forClassWithGenerics(Callable.class, String.class); Type type = resolvableType.getType(); assertThat(type, is(instanceOf(ParameterizedType.class))); assertThat(((ParameterizedType) type).getRawType(), is(equalTo(Callable.class))); assertThat(((ParameterizedType) type).getActualTypeArguments().length, is(equalTo(1))); assertThat(((ParameterizedType) type).getActualTypeArguments()[0], is(equalTo(String.class))); }
@Test public void invokeAwareMethodsInImportGroup() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(GroupedConfig1.class); assertThat(TestImportGroup.beanFactory, is(context.getBeanFactory())); assertThat(TestImportGroup.classLoader, is(context.getBeanFactory().getBeanClassLoader())); assertThat(TestImportGroup.resourceLoader, is(notNullValue())); assertThat(TestImportGroup.environment, is(context.getEnvironment())); }
@Test public void shouldReturnFalseIfLockFileAlreadyExists() throws IOException { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); when(mockfile.exists()).thenReturn(true); when(mockfile.lastModified()).thenReturn(System.currentTimeMillis()); assertThat(lockfile.tryLock(), is(false)); verify(mockfile).exists(); }
@Test public void destroy() throws Exception { filter = new PatternMappingFilterProxy(delegate, "/"); filter.destroy(); assertThat(delegate.destroy, is(true)); }
@Test public void loadIndexNoSpringComponentsResource() { CandidateComponentsIndex index = CandidateComponentsIndexLoader.loadIndex( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader())); assertThat(index, is(nullValue())); }
@Test public void decodeFromBinaryCannotConvert() throws Exception { setup(NoConvertersConfig.class); Decoder.Binary<MyType> decoder = new MyBinaryDecoder(); assertThat(decoder.willDecode(CONVERTED_BYTES), is(false)); thown.expect(DecodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); decoder.decode(CONVERTED_BYTES); }
@Test public void setAttributesFromMessage() { String sessionId = "session1"; ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>(); SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId(sessionId); headerAccessor.setSessionAttributes(map); Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders()); SimpAttributesContextHolder.setAttributesFromMessage(message); SimpAttributes attrs = SimpAttributesContextHolder.getAttributes(); assertThat(attrs, notNullValue()); assertThat(attrs.getSessionId(), is(sessionId)); attrs.setAttribute("name1", "value1"); assertThat(map.get("name1"), is("value1")); }
private static void verifySameInvocations(List<Invocation> expectedInvocations, List<Invocation> actualInvocations, InvocationArgumentsAdapter... argumentAdapters) { assertThat(expectedInvocations.size(), is(equalTo(actualInvocations.size()))); for (int i = 0; i < expectedInvocations.size(); i++) { verifySameInvocation(expectedInvocations.get(i), actualInvocations.get(i), argumentAdapters); } }
@Test public void mustCreateAndCacheNewTokens() throws Exception { when( creator.createToken( "token" ) ).thenReturn( 42 ); assertThat( holder.getOrCreateId( "token" ), is( 42 ) ); assertThat( holder.getOrCreateId( "token" ), is( 42 ) ); // Verify implies that the call only happens once. verify( creator ).createToken( "token" ); verifyNoMoreInteractions( creator ); }
@Test public void shouldSupportNullFieldType() throws Exception { String[] codes = resolver.resolveMessageCodes("errorCode", "objectName", "field", null); assertThat(codes, is(equalTo(new String[] { "errorCode.objectName.field", "errorCode.field", "errorCode" }))); }
@Test public void init() throws Exception { FilterConfig config = new MockFilterConfig(); filter = new PatternMappingFilterProxy(delegate, "/"); filter.init(config); assertThat(delegate.filterConfig, is(config)); }
@Test public void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigB.class); ctx.refresh(); Thread.sleep(100); // allow @Scheduled method to be called several times MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); assertThat("repository is not a proxy", AopUtils.isJdkDynamicProxy(repository), is(true)); assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); assertThat("no transactions were committed", txManager.commits, greaterThan(0)); }
@Test public void invokeAwareMethodsInImportSelector() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class); assertThat(SampleImportSelector.beanFactory, is(context.getBeanFactory())); assertThat(SampleImportSelector.classLoader, is(context.getBeanFactory().getBeanClassLoader())); assertThat(SampleImportSelector.resourceLoader, is(notNullValue())); assertThat(SampleImportSelector.environment, is(context.getEnvironment())); }
private static void verifySameInvocation(Invocation expectedInvocation, Invocation actualInvocation, InvocationArgumentsAdapter... argumentAdapters) { assertThat(expectedInvocation.getMethod(), is(equalTo(actualInvocation.getMethod()))); Object[] expectedArguments = getInvocationArguments(expectedInvocation, argumentAdapters); Object[] actualArguments = getInvocationArguments(actualInvocation, argumentAdapters); assertThat(expectedArguments, is(equalTo(actualArguments))); }
@Test public void shouldGetTaskView() { String jsonResponse = "{\"displayValue\":\"MyTaskPlugin\", \"template\":\"<html>junk</html>\"}"; when(goPluginApiResponse.responseBody()).thenReturn(jsonResponse); TaskView view = task.view(); assertThat(view.displayValue(), is("MyTaskPlugin")); assertThat(view.template(), is("<html>junk</html>")); ArgumentCaptor<GoPluginApiRequest> argument = ArgumentCaptor.forClass(GoPluginApiRequest.class); verify(pluginManager).submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), argument.capture()); MatcherAssert.assertThat(argument.getValue().extension(), Matchers.is(PLUGGABLE_TASK_EXTENSION)); MatcherAssert.assertThat(argument.getValue().extensionVersion(), Matchers.is(JsonBasedTaskExtensionHandler_V1.VERSION)); MatcherAssert.assertThat(argument.getValue().requestName(), Matchers.is(TaskExtension.TASK_VIEW_REQUEST)); }
@Test @SuppressWarnings("rawtypes") public void getObjectType() { assertThat(factory.getObjectType(), is(equalTo((Class) DateTimeFormatter.class))); }
@Test // SPR-14040 public void shouldDrainErrorStreamWhenResponseClosed() throws Exception { byte[] buf = new byte[6]; TestByteArrayInputStream is = new TestByteArrayInputStream("SpringSpring".getBytes(StandardCharsets.UTF_8)); given(this.connection.getErrorStream()).willReturn(is); InputStream responseStream = this.response.getBody(); responseStream.read(buf); assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring")); assertThat(is.available(), is(6)); this.response.close(); assertThat(is.available(), is(0)); assertTrue(is.isClosed()); verify(this.connection, never()).disconnect(); }
@Test public void shouldPrintAndParseDefault() throws Exception { DateFormatter formatter = new DateFormatter(); formatter.setTimeZone(UTC); Date date = getDate(2009, Calendar.JUNE, 1); assertThat(formatter.print(date, Locale.US), is("Jun 1, 2009")); assertThat(formatter.parse("Jun 1, 2009", Locale.US), is(date)); }