Skip to content

Commit

Permalink
Merge pull request #54 from mendix/dat/fix/large-decimal-logging
Browse files Browse the repository at this point in the history
Allow avoiding scientific notation in big decimal logging
  • Loading branch information
pijuskri authored Feb 28, 2024
2 parents 7c0a305 + 8410342 commit 9dc298a
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 46 deletions.
Binary file modified Audit trail.mpr
Binary file not shown.
2 changes: 0 additions & 2 deletions MAINTAIN.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
## Building locally

In order to prepare userlib folder to run the project locally, use `./gradlew prepareTest`. To keep only jars that are needed for the release, use `./gradlew prepareMpk`.

`./gradlew mxBuild` builds the app locally.

`./gradlew exportModule` creates an mpk to be released with only necessary jars included in userlib.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ repositories {
dependencies {
implementation(fileTree(mxRuntimeBundles).include('*.jar'))

testImplementation('org.mockito:mockito-all:2.0.2-beta')
testImplementation('com.mendix.util:junit-helper:1.0.0') {
exclude group: 'com.mendix', module: 'public-api'
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=9.1.1
version=9.1.2
6 changes: 5 additions & 1 deletion javasource/audittrail/log/CreateLogObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ private static String getMemberValueString(final IMendixObjectMember<?> member,
}

if (value instanceof BigDecimal) {
return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim();
if ((Boolean) context.getSystemConfiguration().getConstantValue("AuditTrail.LogUseDecimalScientificNotation")) {
return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim();
} else {
return ((BigDecimal) value).stripTrailingZeros().toPlainString();
}
}

if (value instanceof String) {
Expand Down
1 change: 1 addition & 0 deletions marketplace/release-notes/9.1.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We added the "LogUseDecimalScientificNotation" constant to control how large decimal values are displayed. The default behaviour is to use scientific notation, which can be changed to show the full value instead.
81 changes: 81 additions & 0 deletions src/test/java/com/mendix/audittrail/tests/TestAuditConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.mendix.audittrail.tests;

import audittrail.proxies.TypeOfLog;
import com.mendix.audittrail.tests.actual.ActualLog;
import com.mendix.audittrail.tests.expected.ExpectedLog;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.core.IContext;
import org.junit.Test;
import test_crm.proxies.Company;

import java.math.BigDecimal;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import static test_crm.proxies.Company.MemberNames.Dec;

public class TestAuditConstants extends TestAuditWithData {
@Test
public void testLogUseDecimalScientificNotationIsTrue() throws CoreException {
context = mockContextWithSetConstantValue(context, true);
// Create new record
final Company company = createBaseCompanyAndSetDecimalValue();

// Expect scientific decimal format in log
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Add, company).addAttribute(Dec, DEC_STRING_SCIENTIFIC);

final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
expectedLog.verify(actualLog);
}

@Test
public void testLogUseDecimalScientificNotationIsFalse() throws CoreException {
context = mockContextWithSetConstantValue(context, false);
// Create new record
final Company company = createBaseCompanyAndSetDecimalValue();

// Expect full decimal format in log
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Add, company).addAttribute(Dec, DEC_STRING_FULL);

final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
expectedLog.verify(actualLog);
}

@Test
public void testLogUseDecimalScientificNotationIsNotSet() throws CoreException {
// Create new record
final Company company = createBaseCompanyAndSetDecimalValue();

// Expect scientific decimal format in log
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Add, company).addAttribute(Dec, DEC_STRING_SCIENTIFIC);

final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
expectedLog.verify(actualLog);
}

private IContext mockContextWithSetConstantValue(IContext context, Boolean decimalNotationConstantValue) {
IContext spyContext = spy(context);
com.mendix.core.conf.Configuration configSpy = spy(spyContext.getSystemConfiguration());
when(spyContext.getSystemConfiguration()).thenReturn(configSpy);
when(spyContext.clone()).thenReturn(spyContext);
when(configSpy.getConstantValue("AuditTrail.LogUseDecimalScientificNotation"))
.thenReturn(decimalNotationConstantValue);
return spyContext;
}

protected Company createBaseCompanyAndSetDecimalValue() throws CoreException {
final Company company = new Company(context);

company.setName(NAME);
company.setCompanyNr(COMPANY_NR);
company.setDec(DEC);

company.commit();
return company;
}

private static final BigDecimal DEC = BigDecimal.valueOf(1000);
private static final String DEC_STRING_SCIENTIFIC = "1E+3";
private static final String DEC_STRING_FULL = "1000";

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.mendix.audittrail.tests;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.MendixRuntimeException;
import com.mendix.systemwideinterfaces.core.IMendixObject;

import org.junit.Test;

Expand All @@ -17,19 +15,14 @@
import audittrail.proxies.MemberType;
import audittrail.proxies.TypeOfLog;
import test_crm.proxies.Company;
import test_crm.proxies.Group;
import com.mendix.audittrail.tests.actual.ActualLog;
import com.mendix.audittrail.tests.expected.ExpectedLog;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;
import static test_crm.proxies.Company.MemberNames.CompanyNr;
import static test_crm.proxies.Company.MemberNames.Dec;
import static test_crm.proxies.Company.MemberNames.Founded;
import static test_crm.proxies.Company.MemberNames.Name;
import static test_crm.proxies.Company.MemberNames.Number;
import static test_crm.proxies.Company.MemberNames.InternNr;
import static test_crm.proxies.Company.MemberNames.Company_Group;

/**
Expand All @@ -40,7 +33,7 @@
* Company has 3 attributes and an association to a set of groups. Logging of
* this association is also tested in this class.
*/
public class TestAuditInheritance extends TestAuditBase {
public class TestAuditInheritance extends TestAuditWithData {

// Testing records without reference

Expand Down Expand Up @@ -204,37 +197,6 @@ public void testNoLogOnRollback() throws Exception {
assertEquals("No logs should be added for a rolled back commit", logsBefore, logsAfter);
}

private Company createBaseCompany(final Group... groups) throws CoreException {
final Company company = new Company(context);

company.setName(NAME);
company.setCompanyNr(COMPANY_NR);

company.setCompany_Group(Arrays.asList(groups));

company.commit();
return company;
}

private ExpectedLog createExpectedLog(final TypeOfLog typeOfLog, final Company company,
final IMendixObject... groupObjects) {
if (typeOfLog.equals(TypeOfLog.Add)) {
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
.addAttribute(Name, NAME).addAttribute(CompanyNr, COMPANY_NR)
.addAttribute(InternNr, company.getInternNr())
.addAttribute(Dec, 0).addAttribute(Number, 0)
.addAttribute(Founded, "")
.addReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
} else {
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
.keepAttribute(Name, NAME).keepAttribute(CompanyNr, COMPANY_NR)
.keepAttribute(InternNr, company.getInternNr())
.keepAttribute(Dec, 0).keepAttribute(Number, 0)
.keepAttribute(Founded, "")
.keepReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
}
}

private static Date createDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 1991);
Expand All @@ -243,9 +205,7 @@ private static Date createDate() {
calendar.set(Calendar.HOUR, 2);
return calendar.getTime();
}

private static final String NAME = "Company";

private static final String NAME2 = "Company2";
private static final String COMPANY_NR = "123";
private static final Date FOUNDED_DATE = createDate();
}
53 changes: 53 additions & 0 deletions src/test/java/com/mendix/audittrail/tests/TestAuditWithData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mendix.audittrail.tests;

import audittrail.proxies.MemberType;
import audittrail.proxies.TypeOfLog;
import com.mendix.audittrail.tests.expected.ExpectedLog;
import com.mendix.core.CoreException;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import test_crm.proxies.Company;
import test_crm.proxies.Group;

import java.util.Arrays;

import static test_crm.proxies.Company.MemberNames.*;
import static test_crm.proxies.Company.MemberNames.Company_Group;

/**
* Helper class for creating tests with a company objects
*/
public abstract class TestAuditWithData extends TestAuditBase {
protected Company createBaseCompany(final Group... groups) throws CoreException {
final Company company = new Company(context);

company.setName(NAME);
company.setCompanyNr(COMPANY_NR);

company.setCompany_Group(Arrays.asList(groups));

company.commit();
return company;
}

protected ExpectedLog createExpectedLog(final TypeOfLog typeOfLog, final Company company,
final IMendixObject... groupObjects) {
if (typeOfLog.equals(TypeOfLog.Add)) {
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
.addAttribute(Name, NAME).addAttribute(CompanyNr, COMPANY_NR)
.addAttribute(InternNr, company.getInternNr())
.addAttribute(Dec, 0).addAttribute(Number, 0)
.addAttribute(Founded, "")
.addReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
} else {
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
.keepAttribute(Name, NAME).keepAttribute(CompanyNr, COMPANY_NR)
.keepAttribute(InternNr, company.getInternNr())
.keepAttribute(Dec, 0).keepAttribute(Number, 0)
.keepAttribute(Founded, "")
.keepReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
}
}

protected static final String NAME = "Company";
protected static final String COMPANY_NR = "123";
}

0 comments on commit 9dc298a

Please sign in to comment.