@Test public void proxyingWorksIfInfoReturnsNullEntityManagerInterface() { EntityManagerFactory emf = mock(EntityManagerFactory.class, withSettings().extraInterfaces(EntityManagerFactoryInfo.class)); // EntityManagerFactoryInfo.getEntityManagerInterface returns null assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf), is(notNullValue())); }
public Object process(Mock annotation, Field field) { MockSettings mockSettings = Mockito.withSettings(); if (annotation.extraInterfaces().length > 0) { // never null mockSettings.extraInterfaces(annotation.extraInterfaces()); } if ("".equals(annotation.name())) { mockSettings.name(field.getName()); } else { mockSettings.name(annotation.name()); } // see @Mock answer default value mockSettings.defaultAnswer(annotation.answer().get()); return Mockito.mock(field.getType(), mockSettings); } }
private static ExecutionGraphManipulator getMockedGraph(int address, HeapItem value) { ExecutionGraphManipulator manipulator = mock(ExecutionGraphManipulator.class); BuilderInstruction instruction = mock(BuilderInstruction.class, withSettings().extraInterfaces(OneRegisterInstruction.class)); when(((OneRegisterInstruction) instruction).getRegisterA()).thenReturn(REGISTER); when(manipulator.getRegisterConsensus(address, REGISTER)).thenReturn(value); when(manipulator.getInstruction(address)).thenReturn(instruction); return manipulator; }
private BuilderInstruction buildInstruction22t(Opcode opcode, int offset) { BuilderInstruction instruction = mock(BuilderInstruction.class, withSettings().extraInterfaces(Instruction22t.class)); when(location.getInstruction()).thenReturn(instruction); when(instruction.getLocation()).thenReturn(location); when(instruction.getCodeUnits()).thenReturn(0); when(instruction.getOpcode()).thenReturn(opcode); when(((Instruction22t) instruction).getRegisterA()).thenReturn(ARG1_REGISTER); when(((Instruction22t) instruction).getRegisterB()).thenReturn(ARG2_REGISTER); when(((Instruction22t) instruction).getCodeOffset()).thenReturn(offset); return instruction; }
@Test public void handlerMappingOrder() { HandlerMapping hm1 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class)); HandlerMapping hm2 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class)); when(((Ordered) hm1).getOrder()).thenReturn(1); when(((Ordered) hm2).getOrder()).thenReturn(2); when((hm1).getHandler(any())).thenReturn(Mono.just((Supplier<String>) () -> "1")); when((hm2).getHandler(any())).thenReturn(Mono.just((Supplier<String>) () -> "2")); StaticApplicationContext context = new StaticApplicationContext(); context.registerBean("b2", HandlerMapping.class, () -> hm2); context.registerBean("b1", HandlerMapping.class, () -> hm1); context.registerBean(HandlerAdapter.class, SupplierHandlerAdapter::new); context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new); context.refresh(); DispatcherHandler dispatcherHandler = new DispatcherHandler(context); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); dispatcherHandler.handle(exchange).block(Duration.ofSeconds(0)); assertEquals("1", exchange.getResponse().getBodyAsString().block(Duration.ofSeconds(5))); }
private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata, MockCreationSettings parentMockSettings) { MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ? withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces()) : withSettings(); return propagateSerializationSettings(mockSettings, parentMockSettings) .defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata)); }
@Test public void localSettingsOverrideClientDefaultSettings() throws Exception { RequestConfig defaultConfig = RequestConfig.custom() .setConnectTimeout(1234).setConnectionRequestTimeout(6789).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client); hrf.setConnectTimeout(5000); RequestConfig requestConfig = retrieveRequestConfig(hrf); assertEquals(5000, requestConfig.getConnectTimeout()); assertEquals(6789, requestConfig.getConnectionRequestTimeout()); assertEquals(-1, requestConfig.getSocketTimeout()); }
@Test public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception { RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client); assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf)); hrf.setConnectionRequestTimeout(4567); RequestConfig requestConfig = retrieveRequestConfig(hrf); assertNotNull(requestConfig); assertEquals(4567, requestConfig.getConnectionRequestTimeout()); // Default connection timeout merged assertEquals(1234, requestConfig.getConnectTimeout()); }
@Test public void localSettingsOverrideClientDefaultSettings() throws Exception { RequestConfig defaultConfig = RequestConfig.custom() .setConnectTimeout(1234).setConnectionRequestTimeout(6789).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client); executor.setConnectTimeout(5000); HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service"); HttpPost httpPost = executor.createHttpPost(config); RequestConfig requestConfig = httpPost.getConfig(); assertEquals(5000, requestConfig.getConnectTimeout()); assertEquals(6789, requestConfig.getConnectionRequestTimeout()); assertEquals(-1, requestConfig.getSocketTimeout()); }
@Test public void shouldCreateRSA512AlgorithmWithPublicKey() throws Exception { RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPublicKey.class)); Algorithm algorithm = Algorithm.RSA512(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(RSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA512withRSA")); assertThat(algorithm.getName(), is("RS512")); }
@Test public void shouldCreateRSA512AlgorithmWithPrivateKey() throws Exception { RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPrivateKey.class)); Algorithm algorithm = Algorithm.RSA512(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(RSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA512withRSA")); assertThat(algorithm.getName(), is("RS512")); }
@Test public void shouldCreateECDSA384AlgorithmWithPublicKey() throws Exception { ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPublicKey.class)); Algorithm algorithm = Algorithm.ECDSA384(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA384withECDSA")); assertThat(algorithm.getName(), is("ES384")); }
@Test public void shouldCreateECDSA384AlgorithmWithPrivateKey() throws Exception { ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class)); Algorithm algorithm = Algorithm.ECDSA384(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA384withECDSA")); assertThat(algorithm.getName(), is("ES384")); }
@Test public void shouldCreateECDSA512AlgorithmWithPrivateKey() throws Exception { ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class)); Algorithm algorithm = Algorithm.ECDSA512(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA512withECDSA")); assertThat(algorithm.getName(), is("ES512")); }
@Test public void shouldCreateRSA256AlgorithmWithPublicKey() throws Exception { RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPublicKey.class)); Algorithm algorithm = Algorithm.RSA256(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(RSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA256withRSA")); assertThat(algorithm.getName(), is("RS256")); }
@Test public void shouldCreateRSA384AlgorithmWithPrivateKey() throws Exception { RSAKey key = mock(RSAKey.class, withSettings().extraInterfaces(RSAPrivateKey.class)); Algorithm algorithm = Algorithm.RSA384(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(RSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA384withRSA")); assertThat(algorithm.getName(), is("RS384")); }
@Test public void shouldCreateECDSA256AlgorithmWithPrivateKey() throws Exception { ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class)); Algorithm algorithm = Algorithm.ECDSA256(key); assertThat(algorithm, is(notNullValue())); assertThat(algorithm, is(instanceOf(ECDSAAlgorithm.class))); assertThat(algorithm.getDescription(), is("SHA256withECDSA")); assertThat(algorithm.getName(), is("ES256")); }
@Test public void mergeBasedOnCurrentHttpClient() throws Exception { RequestConfig defaultConfig = RequestConfig.custom() .setSocketTimeout(1234).build(); final CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() { @Override public HttpClient getHttpClient() { return client; } }; hrf.setReadTimeout(5000); RequestConfig requestConfig = retrieveRequestConfig(hrf); assertEquals(-1, requestConfig.getConnectTimeout()); assertEquals(-1, requestConfig.getConnectionRequestTimeout()); assertEquals(5000, requestConfig.getSocketTimeout()); // Update the Http client so that it returns an updated config RequestConfig updatedDefaultConfig = RequestConfig.custom() .setConnectTimeout(1234).build(); when(configurable.getConfig()).thenReturn(updatedDefaultConfig); hrf.setReadTimeout(7000); RequestConfig requestConfig2 = retrieveRequestConfig(hrf); assertEquals(1234, requestConfig2.getConnectTimeout()); assertEquals(-1, requestConfig2.getConnectionRequestTimeout()); assertEquals(7000, requestConfig2.getSocketTimeout()); }
@Test public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws IOException { RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build(); CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig); HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client); HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service"); HttpPost httpPost = executor.createHttpPost(config); assertSame("Default client configuration is expected", defaultConfig, httpPost.getConfig()); executor.setConnectionRequestTimeout(4567); HttpPost httpPost2 = executor.createHttpPost(config); assertNotNull(httpPost2.getConfig()); assertEquals(4567, httpPost2.getConfig().getConnectionRequestTimeout()); // Default connection timeout merged assertEquals(1234, httpPost2.getConfig().getConnectTimeout()); }
.setSocketTimeout(1234).build(); final CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class)); Configurable configurable = (Configurable) client; when(configurable.getConfig()).thenReturn(defaultConfig);