public void testPutAll_Map_array() { try { MapUtils.putAll(null, null); fail(); } catch (NullPointerException ex) {} try { MapUtils.putAll(null, new Object[0]); fail(); } catch (NullPointerException ex) {} Map test = MapUtils.putAll(new HashMap(), new String[0]); assertEquals(0, test.size()); test = MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, {"GREEN", "#00FF00"}, MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, null, MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, {"GREEN"}, MapUtils.putAll(new HashMap(), new String[][] { {"RED", "#FF0000"}, {}, test = MapUtils.putAll(new HashMap(), new String[] {
@BeforeMethod public void initTestClass() { Map<String, String> settings = new HashMap<String, String>(); settings.put("sessioncapture", "false"); when(threadMXBean.isThreadCpuTimeEnabled()).thenReturn(true); when(threadMXBean.isThreadCpuTimeSupported()).thenReturn(true); Map<String, Object> map = new HashMap<String, Object>(); MapUtils.putAll(map, new String[][] { { "sessioncapture", "true" }, { "attributescapture", "true" }, { "parameterscapture", "true" } }); httpHook = new HttpHook(timer, platformManager, map, threadMXBean); }
@Test public void readArrayAttributes() { final String att1 = "a1"; final String att2 = "a2"; final String[] att1Value = { "attValue1", "attValue2", "attValue3" }; final String[] att2Value = { "a1", "a2", "a3" }; final Vector<String> attributesList = new Vector<String>(); Collections.addAll(attributesList, att1, att2); final Enumeration<String> attributes = attributesList.elements(); when(httpServletRequest.getAttributeNames()).thenReturn(attributes); when(httpServletRequest.getAttribute(att1)).thenReturn(att1Value); when(httpServletRequest.getAttribute(att2)).thenReturn(att2Value); Map<String, String> result = extractor.getAttributes(httpServletRequest.getClass(), httpServletRequest); final String extractedAttribute1Value = "[attValue1, attValue2, attValue3]".substring(0, 20) + "..."; final String extractedAttribute2Value = "[a1, a2, a3]"; Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { att1, extractedAttribute1Value }, { att2, extractedAttribute2Value } }); assertThat(result, is(equalTo(expected))); }
@Test public void sessionAttributesWithSession() { final String sa1 = "sa1"; final String sa2 = "sa2"; final String sa1Value = "saValue1"; final String sa2Value = "saValue2"; final Vector<String> sessionAttributesList = new Vector<String>(); Collections.addAll(sessionAttributesList, sa1, sa2); final Enumeration<String> sessionAttributes = sessionAttributesList.elements(); HttpSession session = Mockito.mock(HttpSession.class); when(session.getAttributeNames()).thenReturn(sessionAttributes); when(session.getAttribute(sa1)).thenReturn(sa1Value); when(session.getAttribute(sa2)).thenReturn(sa2Value); when(httpServletRequest.getSession(false)).thenReturn(session); Map<String, String> result = extractor.getSessionAttributes(httpServletRequest.getClass(), httpServletRequest); Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { sa1, sa1Value }, { sa2, sa2Value } }); assertThat(result, is(equalTo(expected))); }
@Test public void readArrayAttributesPrimitivesInteger() { final String att1 = "a1"; final String att2 = "a2"; final int[] att1Value = { 1, 2, 3 }; final int[] att2Value = { 2, 3, 4 }; final Vector<String> attributesList = new Vector<String>(); Collections.addAll(attributesList, att1, att2); final Enumeration<String> attributes = attributesList.elements(); when(httpServletRequest.getAttributeNames()).thenReturn(attributes); when(httpServletRequest.getAttribute(att1)).thenReturn(att1Value); when(httpServletRequest.getAttribute(att2)).thenReturn(att2Value); Map<String, String> result = extractor.getAttributes(httpServletRequest.getClass(), httpServletRequest); final String extractedAttribute1Value = "[1, 2, 3]"; final String extractedAttribute2Value = "[2, 3, 4]"; Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { att1, extractedAttribute1Value }, { att2, extractedAttribute2Value } }); assertThat(result, is(equalTo(expected))); }
@Test public void readHeaders() { final String h1 = "h1"; final String h2 = "h2"; final String h1Value = "hValue1"; final String h2Value = "hValue2"; final Vector<String> headersList = new Vector<String>(); Collections.addAll(headersList, h1, h2); final Enumeration<String> headers = headersList.elements(); when(httpServletRequest.getHeaderNames()).thenReturn(headers); when(httpServletRequest.getHeader(h1)).thenReturn(h1Value); when(httpServletRequest.getHeader(h2)).thenReturn(h2Value); Map<String, String> result = extractor.getHeaders(httpServletRequest.getClass(), httpServletRequest); Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { h1, h1Value }, { h2, h2Value } }); assertThat(result, is(equalTo(expected))); // We only create a new instance of the element if we need to change it (e.g. crop) assertThat("No new instances", result.get(h1) == h1Value); assertThat("No new instances", result.get(h2) == h2Value); }
@Test public void readAttributes() { final String att1 = "a1"; final String att2 = "a2"; final String att1Value = "aValue1"; final String att2Value = "aValue2"; final Vector<String> attributesList = new Vector<String>(); Collections.addAll(attributesList, att1, att2); final Enumeration<String> attributes = attributesList.elements(); when(httpServletRequest.getAttributeNames()).thenReturn(attributes); when(httpServletRequest.getAttribute(att1)).thenReturn(att1Value); when(httpServletRequest.getAttribute(att2)).thenReturn(att2Value); Map<String, String> result = extractor.getAttributes(httpServletRequest.getClass(), httpServletRequest); Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { att1, att1Value }, { att2, att2Value } }); assertThat(result, is(equalTo(expected))); // We only create a new instance of the element if we need to change it (e.g. crop) assertThat("No new instances", result.get(att1) == att1Value); assertThat("No new instances", result.get(att2) == att2Value); }
@Test public void sessionArrayAttributesWithSession() { final String sa1 = "sa1"; final String sa2 = "sa2"; final String[] sa1Value = { "saValue1", "saValue2", "saValue3" }; final String[] sa2Value = { "s1", "s2", "s3" }; final Vector<String> sessionAttributesList = new Vector<String>(); Collections.addAll(sessionAttributesList, sa1, sa2); final Enumeration<String> sessionAttributes = sessionAttributesList.elements(); HttpSession session = Mockito.mock(HttpSession.class); when(session.getAttributeNames()).thenReturn(sessionAttributes); when(session.getAttribute(sa1)).thenReturn(sa1Value); when(session.getAttribute(sa2)).thenReturn(sa2Value); when(httpServletRequest.getSession(false)).thenReturn(session); Map<String, String> result = extractor.getSessionAttributes(httpServletRequest.getClass(), httpServletRequest); final String extractedSa1Value = "[saValue1, saValue2, saValue3]".substring(0, 20) + "..."; final String extractedSa2Value = "[s1, s2, s3]"; Map<String, String> expected = new HashMap<String, String>(); MapUtils.putAll(expected, new Object[][] { { sa1, extractedSa1Value }, { sa2, extractedSa2Value } }); assertThat(result, is(equalTo(expected))); }
@Test public void readParameters() { final String param1 = "p1"; final String param2 = "p2"; final String param1VReal = "I am a really long string that should be cropped in an meaningful way."; final String param2VReal1 = "value5"; final String param2VReal2 = "value6"; final String[] param1V = new String[] { param1VReal }; final String[] param2V = new String[] { param2VReal1, param2VReal2 }; final Map<String, String[]> parameterMap = new HashMap<String, String[]>(); MapUtils.putAll(parameterMap, new Object[][] { { param1, param1V }, { param2, param2V } }); when(httpServletRequest.getParameterMap()).thenReturn(parameterMap); Map<String, String[]> result = extractor.getParameterMap(httpServletRequest.getClass(), httpServletRequest); assertThat(result.size(), is(parameterMap.size())); assertThat(result, hasKey(param1)); assertThat(result, hasKey(param2)); assertThat("Value should be cropped!", result.get(param1), is(not(param1V))); assertThat(result.get(param2), is(param2V)); }
/** * Test that the registration of the {@link MethodSensorTypeIdent} will be correct if * properties are provided. */ @SuppressWarnings("unchecked") @Test public void registerExistingMethodSensorTypeWithSettings() { final long methodSensorId = 30; long platformId = 1; final String fqcName = "class"; String regEx = "myRegEx"; String regExTemplate = "myRegExTemplate"; Map<String, Object> settings = MapUtils.putAll(new HashMap<String, Object>(), new String[][] { { "regEx", regEx }, { "regExTemplate", regExTemplate } }); when(methodSensorTypeIdentDao.findIdByClassNameAndPlatformId(fqcName, platformId)).thenReturn(Collections.singletonList(methodSensorId)); long registeredId = registrationService.registerMethodSensorTypeIdent(platformId, fqcName, settings); assertThat(registeredId, is(equalTo(methodSensorId))); verify(methodSensorTypeIdentDao, times(1)).updateParameters(methodSensorId, settings); } }
@Test /** Tests whether the first entry is correctly copied to new map */ public void cropStringMapCropSecondEntry() { constraint = new StringConstraint(Collections.<String, Object> singletonMap("stringLength", "20")); final String param1 = "p1"; final String param2 = "p2"; final String param3 = "p3"; final String param1VReal = "value"; final String param2VReal1 = "I am really very long and need to be cropped"; final String param2VReal2 = "value6"; final String param3VReal1 = "value7"; final String param3VReal2 = "value8"; final String[] param1V = new String[] { param1VReal }; final String[] param2V = new String[] { param2VReal1, param2VReal2 }; final String[] param3V = new String[] { param3VReal1, param3VReal2 }; final Map<String, String[]> parameterMap = new HashMap<String, String[]>(); MapUtils.putAll(parameterMap, new Object[][] { { param1, param1V }, { param2, param2V }, { param3, param3V } }); Map<String, String[]> result = constraint.crop(parameterMap); assertThat(result.size(), is(equalTo(parameterMap.size()))); assertThat("Not same by ObjectUtils, need to be cropped", !ObjectUtils.equals(result, parameterMap)); assertThat(result.get(param1), is(param1V)); assertThat(result.get(param2), is(not(param2V))); assertThat(result.get(param3), is(param3V)); assertThat(result.get(param1)[0], is(param1VReal)); assertThat(result.get(param2)[0], is(not(param2VReal1))); assertThat(result.get(param2)[1], is(param2VReal2)); assertThat(result.get(param3)[0], is(param3VReal1)); assertThat(result.get(param3)[1], is(param3VReal2)); }
@Test public void cropStringMapNoCropping() { constraint = new StringConstraint(Collections.<String, Object> singletonMap("stringLength", "20")); final String param1 = "p1"; final String param2 = "p2"; final String param3 = "p3"; final String param1VReal = "value"; final String param2VReal1 = "value5"; final String param2VReal2 = "value6"; final String param3VReal1 = "value7"; final String param3VReal2 = "value8"; final String[] param1V = new String[] { param1VReal }; final String[] param2V = new String[] { param2VReal1, param2VReal2 }; final String[] param3V = new String[] { param3VReal1, param3VReal2 }; final Map<String, String[]> parameterMap = new HashMap<String, String[]>(); MapUtils.putAll(parameterMap, new Object[][] { { param1, param1V }, { param2, param2V }, { param3, param3V } }); Map<String, String[]> result = constraint.crop(parameterMap); assertThat(result.size(), is(equalTo(parameterMap.size()))); assertThat("Same compared by ObjectUtils", ObjectUtils.equals(result, parameterMap)); assertThat(result.get(param1), is(param1V)); assertThat(result.get(param2), is(param2V)); assertThat(result.get(param3), is(param3V)); assertThat(result.get(param1)[0], is(param1VReal)); assertThat(result.get(param2)[0], is(param2VReal1)); assertThat(result.get(param2)[1], is(param2VReal2)); assertThat(result.get(param3)[0], is(param3VReal1)); assertThat(result.get(param3)[1], is(param3VReal2)); }
/** * Tests that clean of the branch is correct. */ @SuppressWarnings("unchecked") @Test public void clean() { IBufferTreeComponent<DefaultData> component1 = Mockito.mock(IBufferTreeComponent.class); when(component1.clean()).thenReturn(true); IBufferTreeComponent<DefaultData> component2 = Mockito.mock(IBufferTreeComponent.class); when(component2.clean()).thenReturn(false); Map<Object, ITreeComponent<DefaultData, DefaultData>> componentMap = MapUtils.putAll(new HashMap<Object, IBufferTreeComponent<DefaultData>>(), new Object[] { "c1", component1, "c2", component2 }); when(branch.getComponentMap()).thenReturn(componentMap); boolean isClean = branch.clean(); assertThat(isClean, is(false)); assertThat(componentMap, not(hasValue((ITreeComponent<DefaultData, DefaultData>) component1))); assertThat(componentMap, hasValue((ITreeComponent<DefaultData, DefaultData>) component2)); when(component2.clean()).thenReturn(true); isClean = branch.clean(); assertThat(isClean, is(true)); assertThat(componentMap.values(), is(empty())); }
/** * Test that clearing of empty components is correct. */ @SuppressWarnings("unchecked") @Test public void clearEmptyComponents() { IBufferTreeComponent<DefaultData> component1 = Mockito.mock(IBufferTreeComponent.class); when(component1.clearEmptyComponents()).thenReturn(true); IBufferTreeComponent<DefaultData> component2 = Mockito.mock(IBufferTreeComponent.class); when(component2.clearEmptyComponents()).thenReturn(false); Map<Object, ITreeComponent<DefaultData, DefaultData>> componentMap = MapUtils.putAll(new HashMap<Object, IBufferTreeComponent<DefaultData>>(), new Object[] { "c1", component1, "c2", component2 }); when(branch.getComponentMap()).thenReturn(componentMap); boolean isClear = branch.clearEmptyComponents(); assertThat(isClear, is(false)); assertThat(componentMap, not(hasValue((ITreeComponent<DefaultData, DefaultData>) component1))); assertThat(componentMap, hasValue((ITreeComponent<DefaultData, DefaultData>) component2)); when(component2.clearEmptyComponents()).thenReturn(true); isClear = branch.clearEmptyComponents(); assertThat(isClear, is(true)); assertThat(componentMap.values(), is(empty())); } }
/** * Test that if validation fails for the property it won't be included in the * {@link Properties} returned by {@link PropertyManager}. */ @SuppressWarnings("unchecked") @Test public void propertyNotValidInDefaultConfiguration() throws JAXBException, IOException, SAXException { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); SingleProperty<?> property = mock(SingleProperty.class); PropertyValidation propertyValidation = mock(PropertyValidation.class, Mockito.RETURNS_SMART_NULLS); when(configuration.getAllProperties()).thenReturn(Collections.<AbstractProperty> singleton(property)); when(configuration.validate()).thenReturn(MapUtils.putAll(new HashMap<AbstractProperty, PropertyValidation>(), new Object[][] { { property, propertyValidation } })); Properties properties = propertyManager.getProperties(); verify(property, times(0)).register(Matchers.<Properties> anyObject()); assertThat(properties.size(), is(0)); }
/** * Test that the registration of the {@link MethodSensorTypeIdent} will be correct if * properties are provided. */ @SuppressWarnings("unchecked") @Test public void registerMethodSensorTypeWithSettings() { final long methodSensorId = 30; long platformId = 1; final String fqcName = "class"; String regEx = "myRegEx"; String regExTemplate = "myRegExTemplate"; Map<String, Object> settings = MapUtils.putAll(new HashMap<String, Object>(), new String[][] { { "regEx", regEx }, { "regExTemplate", regExTemplate } }); when(methodSensorTypeIdentDao.findIdByClassNameAndPlatformId(fqcName, platformId)).thenReturn(Collections.<Long> emptyList()); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { MethodSensorTypeIdent methodSensorIdent = (MethodSensorTypeIdent) invocation.getArguments()[0]; methodSensorIdent.setId(Long.valueOf(methodSensorId)); methodSensorIdent.setFullyQualifiedClassName(fqcName); return null; } }).when(methodSensorTypeIdentDao).saveOrUpdate((MethodSensorTypeIdent) anyObject()); long registeredId = registrationService.registerMethodSensorTypeIdent(platformId, fqcName, settings); assertThat(registeredId, is(equalTo(methodSensorId))); verify(platformIdentCache, times(1)).markDirty(platformId); ArgumentCaptor<MethodSensorTypeIdent> methodSensorArgument = ArgumentCaptor.forClass(MethodSensorTypeIdent.class); verify(methodSensorTypeIdentDao, times(1)).saveOrUpdate(methodSensorArgument.capture()); assertThat(methodSensorArgument.getValue().getSettings(), is(settings)); }
@Test public void testBatchWriterConfigIsSetToValuesWithParameters() { int numThreads = 2; long timeout = 3; long maxMemory = 5; long maxLatency = 7; Map configMap = Maps.newHashMap(); MapUtils.putAll(configMap, new String[]{ AccumuloGraphConfiguration.BATCHWRITER_MAX_LATENCY, "" + maxLatency, AccumuloGraphConfiguration.BATCHWRITER_MAX_MEMORY, "" + maxMemory, AccumuloGraphConfiguration.BATCHWRITER_MAX_WRITE_THREADS, "" + numThreads, AccumuloGraphConfiguration.BATCHWRITER_TIMEOUT, "" + timeout}); AccumuloGraphConfiguration accumuloGraphConfiguration = new AccumuloGraphConfiguration(configMap); BatchWriterConfig batchWriterConfig = accumuloGraphConfiguration.createBatchWriterConfig(); assertThat(batchWriterConfig.getMaxLatency(TimeUnit.MILLISECONDS), is(maxLatency)); assertThat(batchWriterConfig.getTimeout(TimeUnit.MILLISECONDS), is(timeout)); assertThat(batchWriterConfig.getMaxMemory(), is(maxMemory)); assertThat(batchWriterConfig.getMaxWriteThreads(), is(numThreads)); } }
final String[] param2V = new String[] { param2VReal1, param2VReal2 }; final Map<String, String[]> parameterMap = new HashMap<String, String[]>(); MapUtils.putAll(parameterMap, new Object[][] { { param1, param1V }, { param2, param2V } }); tmp.getHttpInfo().setUri(uri); Map<String, String> attributeMap = new HashMap<String, String>(); MapUtils.putAll(attributeMap, new Object[][] { { att1, att1Value }, { att2, att2Value } }); tmp.setAttributes(attributeMap); MapUtils.putAll(headerMap, new Object[][] { { h1, h1Value }, { h2, h2Value } }); tmp.setHeaders(headerMap); MapUtils.putAll(sessionAtrMap, new Object[][] { { sa1, sa1Value }, { sa2, sa2Value } }); tmp.setSessionAttributes(sessionAtrMap);
@Test public void aggregationWithInspectITHeaderTwoDifferent() { final HttpTimerData data = new HttpTimerData(); Map<String, String> map1 = new HashMap<>(); MapUtils.putAll(map1, new String[][] { { HttpTimerData.INSPECTIT_TAGGING_HEADER, "tag1" } }); data.setHeaders(map1); data.getHttpInfo().setUri("URI"); data.getHttpInfo().setRequestMethod("GET"); final HttpTimerData data2 = new HttpTimerData(); Map<String, String> map2 = new HashMap<>(); MapUtils.putAll(map2, new String[][] { { HttpTimerData.INSPECTIT_TAGGING_HEADER, "tag2" } }); data2.setHeaders(map2); data2.getHttpInfo().setUri("URI"); data2.getHttpInfo().setRequestMethod("GET"); final List<HttpTimerData> input = new ArrayList<>(); Collections.addAll(input, data, data2); AggregationPerformer<HttpTimerData> aggregationPerformer = new AggregationPerformer<>(new HttpTimerDataAggregator(false, false)); aggregationPerformer.processCollection(input); final List<HttpTimerData> output = aggregationPerformer.getResultList(); assertThat(output, is(notNullValue())); assertThat(output.size(), is(equalTo(2))); }
final HttpTimerData data = new HttpTimerData(); Map<String, String> map = new HashMap<>(); MapUtils.putAll(map, new String[][] { { HttpTimerData.INSPECTIT_TAGGING_HEADER, "tag1" } }); data.setHeaders(map);