Skip to content

Commit

Permalink
Fix test assertions (#777)
Browse files Browse the repository at this point in the history
* mostly fixed reversed assertEquals parameters.
simplified some assertions.
opted to use assertThat in places where matchers exist.

* fixed reversed assertEquals parameters.
  • Loading branch information
littleaj authored Dec 4, 2018
1 parent c135c15 commit fbbd943
Show file tree
Hide file tree
Showing 44 changed files with 281 additions and 240 deletions.
1 change: 1 addition & 0 deletions ApplicationInsightsInternalLogger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ archivesBaseName = 'ApplicationInsightsInternalLogger'
dependencies {
testCompile('junit:junit:4.12')
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'
testCompile group:'org.hamcrest', name:'hamcrest-library', version:'1.3'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
compile group: 'com.google.guava', name: 'guava', version: '20.0'
compile group: 'commons-io', name: 'commons-io', version: '2.6'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

package com.microsoft.applicationinsights.internal.logger;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
Expand All @@ -37,6 +39,7 @@
import java.util.Map;
import java.util.Queue;
import org.apache.commons.io.FileUtils;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
Expand Down Expand Up @@ -147,7 +150,7 @@ private void testFileLoggerOutputWithRealFiles(String[] lines) throws IOExceptio
Collection<File> logs = FileUtils.listFiles(folder, new String[] {LOG_FILE_SUFFIX}, false);

assertNotNull(logs);
assertEquals(logs.size(), 1);
assertThat(logs, hasSize(1));

BufferedReader br = null;
File file = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public final class ClassInstrumentationDataTest {
Expand All @@ -41,9 +42,9 @@ public void testCtor() throws Exception {
.setReportExecutionTime(false);
assertTrue(test.getMethodInstrumentationInfo().isEmpty());
assertEquals(test.getClassType(), InstrumentedClassType.HTTP.toString());
assertEquals(test.getClassName(), MOCK_CLASS_NAME);
assertEquals(test.isReportCaughtExceptions(), true);
assertEquals(test.isReportExecutionTime(), false);
assertEquals(MOCK_CLASS_NAME, test.getClassName());
assertTrue(test.isReportCaughtExceptions());
assertFalse(test.isReportExecutionTime());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@

package com.microsoft.applicationinsights.agent.internal.agent;


import com.microsoft.applicationinsights.agent.internal.coresync.InstrumentedClassType;
import org.junit.*;
import org.mockito.Mockito;


public final class EnterExitClassVisitorTest {

// TODO fix these tests
// @Ignore
// @Test
// public void testVisitMethodForInterfaceClass() {
// MethodInstrumentorsFactory mockFactory = Mockito.mock(MethodInstrumentorsFactory.class);
// MethodInstrumentorsFactory mockFactory = mock(MethodInstrumentorsFactory.class);
// ClassInstrumentationData classInstrumentationData = new ClassInstrumentationData("java/lang/Runnable", InstrumentedClassType.OTHER);
// classInstrumentationData.addMethod("run", null, true, true);
// DefaultClassVisitor tested = new DefaultClassVisitor(mockFactory, classInstrumentationData, new ClassWriter(10));
Expand All @@ -34,9 +43,10 @@ public final class EnterExitClassVisitorTest {
// Mockito.verify(mockFactory, Mockito.never()).getMethodVisitor(any(MethodInstrumentationDecision.class), anyInt(), anyString(), anyString(), anyString(), any(MethodVisitor.class));
// }
//
// @Ignore
// @Test
// public void testVisitCtor1() {
// MethodInstrumentorsFactory mockFactory = Mockito.mock(MethodInstrumentorsFactory.class);
// MethodInstrumentorsFactory mockFactory = mock(MethodInstrumentorsFactory.class);
// ClassInstrumentationData classInstrumentationData = new ClassInstrumentationData("java/lang/Runnable", InstrumentedClassType.OTHER);
// DefaultClassVisitor tested = new DefaultClassVisitor(mockFactory, classInstrumentationData, new ClassWriter(10));
// tested.visit(Opcodes.ASM5, ~Opcodes.ACC_INTERFACE, "java/lang/String", "()V", null, null);
Expand All @@ -45,9 +55,10 @@ public final class EnterExitClassVisitorTest {
// Mockito.verify(mockFactory, Mockito.never()).getMethodVisitor(any(MethodInstrumentationDecision.class), anyInt(), anyString(), anyString(), anyString(), any(MethodVisitor.class));
// }
//
// @Ignore
// @Test
// public void testVisitCtor2() {
// MethodInstrumentorsFactory mockFactory = Mockito.mock(MethodInstrumentorsFactory.class);
// MethodInstrumentorsFactory mockFactory = mock(MethodInstrumentorsFactory.class);
// ClassInstrumentationData classInstrumentationData = new ClassInstrumentationData("java/lang/String", InstrumentedClassType.OTHER);
// DefaultClassVisitor tested = new DefaultClassVisitor(mockFactory, classInstrumentationData, new ClassWriter(10));
// tested.visit(Opcodes.ASM5, ~Opcodes.ACC_INTERFACE, "java/lang/String", "()V", null, null);
Expand All @@ -58,7 +69,7 @@ public final class EnterExitClassVisitorTest {
//
// @Test
// public void testVisitQualifiedMethod() {
// MethodInstrumentorsFactory mockFactory = Mockito.mock(MethodInstrumentorsFactory.class);
// MethodInstrumentorsFactory mockFactory = mock(MethodInstrumentorsFactory.class);
// ClassInstrumentationData classInstrumentationData = new ClassInstrumentationData("java/lang/String", InstrumentedClassType.OTHER);
// classInstrumentationData.addMethod("indexOf", null, true, true);
// DefaultClassVisitor tested = new DefaultClassVisitor(mockFactory, classInstrumentationData, new ClassWriter(10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public void testCtor2NullMethodName() {
public void testCtor1() {
MethodInstrumentationRequest test = new MethodInstrumentationRequest(MOCK_METHOD_NAME, MOCK_METHOD_SIGNATURE, false, true, 0);

assertEquals(test.getMethodName(), MOCK_METHOD_NAME);
assertEquals(test.getMethodSignature(), MOCK_METHOD_SIGNATURE);
assertEquals(MOCK_METHOD_NAME, test.getMethodName());
assertEquals(MOCK_METHOD_SIGNATURE, test.getMethodSignature());
assertFalse(test.isReportCaughtExceptions());
assertTrue(test.isReportExecutionTime());
}
Expand All @@ -63,7 +63,7 @@ public void testCtor1() {
public void testCtor1WithNullSignature() {
MethodInstrumentationRequest test = new MethodInstrumentationRequest(MOCK_METHOD_NAME, null, false, true, 0);

assertEquals(test.getMethodName(), MOCK_METHOD_NAME);
assertEquals(MOCK_METHOD_NAME, test.getMethodName());
assertNull(test.getMethodSignature());
assertFalse(test.isReportCaughtExceptions());
assertTrue(test.isReportExecutionTime());
Expand All @@ -73,7 +73,7 @@ public void testCtor1WithNullSignature() {
public void testCtor2() {
MethodInstrumentationRequest test = new MethodInstrumentationRequest(MOCK_METHOD_NAME, false, true);

assertEquals(test.getMethodName(), MOCK_METHOD_NAME);
assertEquals(MOCK_METHOD_NAME, test.getMethodName());
assertNull(test.getMethodSignature());
assertFalse(test.isReportCaughtExceptions());
assertTrue(test.isReportExecutionTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testForbiddenSection() throws IOException {
Set<String> excludedPrefixes = configuration.getExcludedPrefixes();

assertNotNull(excludedPrefixes);
assertEquals(excludedPrefixes.size(), 2);
assertEquals(2, excludedPrefixes.size());
assertTrue(excludedPrefixes.contains("a.AClass1"));
assertTrue(excludedPrefixes.contains("a.b.AClass1"));
}
Expand All @@ -62,18 +62,18 @@ public void testClassesConfiguration() throws IOException {
AgentConfiguration configuration = testConfiguration("InstrumentationTest.xml");
Map<String, ClassInstrumentationData> classes = configuration.getRequestedClassesToInstrument();
assertNotNull(classes);
assertEquals(classes.size(), 2);
assertEquals(2, classes.size());
}

@Test
public void testBuiltInConfiguration() throws IOException {
AgentConfiguration configuration = testConfiguration("BuiltInTest.xml");
AgentBuiltInConfiguration builtInConfiguration = configuration.getBuiltInConfiguration();
assertEquals(builtInConfiguration.isEnabled(), true);
assertEquals(builtInConfiguration.isHttpEnabled(), true);
assertEquals(builtInConfiguration.isJdbcEnabled(), true);
assertEquals(builtInConfiguration.isJdbcEnabled(), true);
assertEquals(builtInConfiguration.isHibernateEnabled(), false);
assertTrue(builtInConfiguration.isEnabled());
assertTrue(builtInConfiguration.isHttpEnabled());
assertTrue(builtInConfiguration.isJdbcEnabled());
assertTrue(builtInConfiguration.isJdbcEnabled());
assertFalse(builtInConfiguration.isHibernateEnabled());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void testConfDataEnabled() {

assertNotNull(result);
assertTrue(result.valid);
assertEquals(result.stackSize, Integer.MAX_VALUE);
assertEquals(Integer.MAX_VALUE, result.stackSize);
}

@Test
Expand Down Expand Up @@ -96,6 +96,6 @@ public void testConfDataEnabledWithSuppressedDataNotRelevant() {

assertNotNull(result);
assertTrue(result.valid);
assertEquals(result.stackSize, 1);
assertEquals(1, result.stackSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ private void testUseConfigurationInstrumentatonKey(String contextInstrumentation
telemetryClient.track(mockTelemetry);

Mockito.verify(mockChannel, Mockito.times(1)).send(mockTelemetry);
assertEquals(mockContext.getInstrumentationKey(), "00000000-0000-0000-0000-000000000000");
assertEquals("00000000-0000-0000-0000-000000000000", mockContext.getInstrumentationKey());
}

private Telemetry verifyAndGetLastEventSent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public void testSetId() {
DeviceContext context = new DeviceContext(map);
context.setId("mock");

assertEquals(context.getId(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceId()), "mock");
assertEquals("mock", context.getId());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceId()));
}

@Test
Expand All @@ -56,9 +56,9 @@ public void testSetModel() {
DeviceContext context = new DeviceContext(map);
context.setModel("mock");

assertEquals(context.getModel(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceModel()), "mock");
assertEquals("mock", context.getModel());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceModel()));
}

@Test
Expand All @@ -67,9 +67,9 @@ public void testSetNetworkType() {
DeviceContext context = new DeviceContext(map);
context.setNetworkType("mock");

assertEquals(context.getNetworkType(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceNetwork()), "mock");
assertEquals("mock", context.getNetworkType());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceNetwork()));
}

@Test
Expand All @@ -78,9 +78,9 @@ public void testSetOemName() {
DeviceContext context = new DeviceContext(map);
context.setOemName("mock");

assertEquals(context.getOemName(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceOEMName()), "mock");
assertEquals("mock", context.getOemName());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceOEMName()));
}

@Test
Expand All @@ -89,9 +89,9 @@ public void testSetOperatingSystem() {
DeviceContext context = new DeviceContext(map);
context.setOperatingSystem("mock");

assertEquals(context.getOperatingSystem(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceOS()), "mock");
assertEquals("mock", context.getOperatingSystem());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceOS()));
}

@Test
Expand All @@ -100,9 +100,9 @@ public void testSetOperatingSystemVersion() {
DeviceContext context = new DeviceContext(map);
context.setOperatingSystemVersion("mock");

assertEquals(context.getOperatingSystemVersion(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceOSVersion()), "mock");
assertEquals("mock", context.getOperatingSystemVersion());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceOSVersion()));
}

@Test
Expand All @@ -111,9 +111,9 @@ public void testSetRoleInstance() {
DeviceContext context = new DeviceContext(map);
context.setRoleInstance("mock");

assertEquals(context.getRoleInstance(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceRoleInstance()), "mock");
assertEquals("mock", context.getRoleInstance());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceRoleInstance()));
}

@Test
Expand All @@ -122,9 +122,9 @@ public void testSetRoleName() {
DeviceContext context = new DeviceContext(map);
context.setRoleName("mock");

assertEquals(context.getRoleName(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceRoleName()), "mock");
assertEquals("mock", context.getRoleName());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceRoleName()));
}

@Test
Expand All @@ -133,8 +133,8 @@ public void testSetScreenResolution() {
DeviceContext context = new DeviceContext(map);
context.setScreenResolution("mock");

assertEquals(context.getScreenResolution(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getDeviceScreenResolution()), "mock");
assertEquals("mock", context.getScreenResolution());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getDeviceScreenResolution()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public void testSetAgentVersion() {
InternalContext context = new InternalContext(map);
context.setAgentVersion("mock");

assertEquals(context.getAgentVersion(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getInternalAgentVersion()), "mock");
assertEquals("mock", context.getAgentVersion());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getInternalAgentVersion()));
}

@Test
Expand All @@ -45,8 +45,8 @@ public void testSetSdkVersion() {
InternalContext context = new InternalContext(map);
context.setSdkVersion("mock");

assertEquals(context.getSdkVersion(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getInternalSdkVersion()), "mock");
assertEquals("mock", context.getSdkVersion());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getInternalSdkVersion()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public void testSetName() {
OperationContext context = new OperationContext(map);
context.setName("mock");

assertEquals(context.getName(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getOperationName()), "mock");
assertEquals("mock", context.getName());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getOperationName()));
}

@Test
Expand All @@ -45,9 +45,9 @@ public void testSetId() {
OperationContext context = new OperationContext(map);
context.setId("mock");

assertEquals(context.getId(), "mock");
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getOperationId()), "mock");
assertEquals("mock", context.getId());
assertEquals(1, map.size());
assertEquals("mock", map.get(ContextTagKeys.getKeys().getOperationId()));
}

@Test
Expand All @@ -57,8 +57,8 @@ public void testSetSyntheticSource() {
OperationContext context = new OperationContext(map);
context.setSyntheticSource(source);

assertEquals(context.getSyntheticSource(), source);
assertEquals(map.size(), 1);
assertEquals(map.get(ContextTagKeys.getKeys().getSyntheticSource()), source);
assertEquals(source, context.getSyntheticSource());
assertEquals(1, map.size());
assertEquals(source, map.get(ContextTagKeys.getKeys().getSyntheticSource()));
}
}
Loading

0 comments on commit fbbd943

Please sign in to comment.