Skip to content

Commit 9dc298a

Browse files
authored
Merge pull request #54 from mendix/dat/fix/large-decimal-logging
Allow avoiding scientific notation in big decimal logging
2 parents 7c0a305 + 8410342 commit 9dc298a

File tree

9 files changed

+144
-46
lines changed

9 files changed

+144
-46
lines changed

Audit trail.mpr

0 Bytes
Binary file not shown.

MAINTAIN.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
## Building locally
22

3-
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`.
4-
53
`./gradlew mxBuild` builds the app locally.
64

75
`./gradlew exportModule` creates an mpk to be released with only necessary jars included in userlib.

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ repositories {
3030
dependencies {
3131
implementation(fileTree(mxRuntimeBundles).include('*.jar'))
3232

33+
testImplementation('org.mockito:mockito-all:2.0.2-beta')
3334
testImplementation('com.mendix.util:junit-helper:1.0.0') {
3435
exclude group: 'com.mendix', module: 'public-api'
3536
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=9.1.1
1+
version=9.1.2

javasource/audittrail/log/CreateLogObject.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ private static String getMemberValueString(final IMendixObjectMember<?> member,
458458
}
459459

460460
if (value instanceof BigDecimal) {
461-
return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim();
461+
if ((Boolean) context.getSystemConfiguration().getConstantValue("AuditTrail.LogUseDecimalScientificNotation")) {
462+
return String.valueOf(((BigDecimal) value).stripTrailingZeros()).trim();
463+
} else {
464+
return ((BigDecimal) value).stripTrailingZeros().toPlainString();
465+
}
462466
}
463467

464468
if (value instanceof String) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.mendix.audittrail.tests;
2+
3+
import audittrail.proxies.TypeOfLog;
4+
import com.mendix.audittrail.tests.actual.ActualLog;
5+
import com.mendix.audittrail.tests.expected.ExpectedLog;
6+
import com.mendix.core.CoreException;
7+
import com.mendix.systemwideinterfaces.core.IContext;
8+
import org.junit.Test;
9+
import test_crm.proxies.Company;
10+
11+
import java.math.BigDecimal;
12+
13+
import static org.mockito.Mockito.spy;
14+
import static org.mockito.Mockito.when;
15+
import static test_crm.proxies.Company.MemberNames.Dec;
16+
17+
public class TestAuditConstants extends TestAuditWithData {
18+
@Test
19+
public void testLogUseDecimalScientificNotationIsTrue() throws CoreException {
20+
context = mockContextWithSetConstantValue(context, true);
21+
// Create new record
22+
final Company company = createBaseCompanyAndSetDecimalValue();
23+
24+
// Expect scientific decimal format in log
25+
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Add, company).addAttribute(Dec, DEC_STRING_SCIENTIFIC);
26+
27+
final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
28+
expectedLog.verify(actualLog);
29+
}
30+
31+
@Test
32+
public void testLogUseDecimalScientificNotationIsFalse() throws CoreException {
33+
context = mockContextWithSetConstantValue(context, false);
34+
// Create new record
35+
final Company company = createBaseCompanyAndSetDecimalValue();
36+
37+
// Expect full decimal format in log
38+
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Add, company).addAttribute(Dec, DEC_STRING_FULL);
39+
40+
final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
41+
expectedLog.verify(actualLog);
42+
}
43+
44+
@Test
45+
public void testLogUseDecimalScientificNotationIsNotSet() throws CoreException {
46+
// Create new record
47+
final Company company = createBaseCompanyAndSetDecimalValue();
48+
49+
// Expect scientific decimal format in log
50+
final ExpectedLog expectedLog = createExpectedLog(TypeOfLog.Add, company).addAttribute(Dec, DEC_STRING_SCIENTIFIC);
51+
52+
final ActualLog actualLog = ActualLog.getLastLog(context, company.getMendixObject().getId().toLong());
53+
expectedLog.verify(actualLog);
54+
}
55+
56+
private IContext mockContextWithSetConstantValue(IContext context, Boolean decimalNotationConstantValue) {
57+
IContext spyContext = spy(context);
58+
com.mendix.core.conf.Configuration configSpy = spy(spyContext.getSystemConfiguration());
59+
when(spyContext.getSystemConfiguration()).thenReturn(configSpy);
60+
when(spyContext.clone()).thenReturn(spyContext);
61+
when(configSpy.getConstantValue("AuditTrail.LogUseDecimalScientificNotation"))
62+
.thenReturn(decimalNotationConstantValue);
63+
return spyContext;
64+
}
65+
66+
protected Company createBaseCompanyAndSetDecimalValue() throws CoreException {
67+
final Company company = new Company(context);
68+
69+
company.setName(NAME);
70+
company.setCompanyNr(COMPANY_NR);
71+
company.setDec(DEC);
72+
73+
company.commit();
74+
return company;
75+
}
76+
77+
private static final BigDecimal DEC = BigDecimal.valueOf(1000);
78+
private static final String DEC_STRING_SCIENTIFIC = "1E+3";
79+
private static final String DEC_STRING_FULL = "1000";
80+
81+
}

src/test/java/com/mendix/audittrail/tests/TestAuditInheritance.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.mendix.audittrail.tests;
22

3-
import java.util.Arrays;
43
import java.util.Calendar;
54
import java.util.Collections;
65
import java.util.Date;
76

87
import com.mendix.core.Core;
98
import com.mendix.core.CoreException;
109
import com.mendix.systemwideinterfaces.MendixRuntimeException;
11-
import com.mendix.systemwideinterfaces.core.IMendixObject;
1210

1311
import org.junit.Test;
1412

@@ -17,19 +15,14 @@
1715
import audittrail.proxies.MemberType;
1816
import audittrail.proxies.TypeOfLog;
1917
import test_crm.proxies.Company;
20-
import test_crm.proxies.Group;
2118
import com.mendix.audittrail.tests.actual.ActualLog;
2219
import com.mendix.audittrail.tests.expected.ExpectedLog;
2320

2421
import static org.junit.Assert.assertEquals;
2522
import static org.junit.Assert.assertTrue;
2623
import static org.junit.Assert.assertThrows;
27-
import static test_crm.proxies.Company.MemberNames.CompanyNr;
28-
import static test_crm.proxies.Company.MemberNames.Dec;
2924
import static test_crm.proxies.Company.MemberNames.Founded;
3025
import static test_crm.proxies.Company.MemberNames.Name;
31-
import static test_crm.proxies.Company.MemberNames.Number;
32-
import static test_crm.proxies.Company.MemberNames.InternNr;
3326
import static test_crm.proxies.Company.MemberNames.Company_Group;
3427

3528
/**
@@ -40,7 +33,7 @@
4033
* Company has 3 attributes and an association to a set of groups. Logging of
4134
* this association is also tested in this class.
4235
*/
43-
public class TestAuditInheritance extends TestAuditBase {
36+
public class TestAuditInheritance extends TestAuditWithData {
4437

4538
// Testing records without reference
4639

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

207-
private Company createBaseCompany(final Group... groups) throws CoreException {
208-
final Company company = new Company(context);
209-
210-
company.setName(NAME);
211-
company.setCompanyNr(COMPANY_NR);
212-
213-
company.setCompany_Group(Arrays.asList(groups));
214-
215-
company.commit();
216-
return company;
217-
}
218-
219-
private ExpectedLog createExpectedLog(final TypeOfLog typeOfLog, final Company company,
220-
final IMendixObject... groupObjects) {
221-
if (typeOfLog.equals(TypeOfLog.Add)) {
222-
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
223-
.addAttribute(Name, NAME).addAttribute(CompanyNr, COMPANY_NR)
224-
.addAttribute(InternNr, company.getInternNr())
225-
.addAttribute(Dec, 0).addAttribute(Number, 0)
226-
.addAttribute(Founded, "")
227-
.addReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
228-
} else {
229-
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
230-
.keepAttribute(Name, NAME).keepAttribute(CompanyNr, COMPANY_NR)
231-
.keepAttribute(InternNr, company.getInternNr())
232-
.keepAttribute(Dec, 0).keepAttribute(Number, 0)
233-
.keepAttribute(Founded, "")
234-
.keepReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
235-
}
236-
}
237-
238200
private static Date createDate() {
239201
Calendar calendar = Calendar.getInstance();
240202
calendar.set(Calendar.YEAR, 1991);
@@ -243,9 +205,7 @@ private static Date createDate() {
243205
calendar.set(Calendar.HOUR, 2);
244206
return calendar.getTime();
245207
}
246-
247-
private static final String NAME = "Company";
208+
248209
private static final String NAME2 = "Company2";
249-
private static final String COMPANY_NR = "123";
250210
private static final Date FOUNDED_DATE = createDate();
251211
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.mendix.audittrail.tests;
2+
3+
import audittrail.proxies.MemberType;
4+
import audittrail.proxies.TypeOfLog;
5+
import com.mendix.audittrail.tests.expected.ExpectedLog;
6+
import com.mendix.core.CoreException;
7+
import com.mendix.systemwideinterfaces.core.IMendixObject;
8+
import test_crm.proxies.Company;
9+
import test_crm.proxies.Group;
10+
11+
import java.util.Arrays;
12+
13+
import static test_crm.proxies.Company.MemberNames.*;
14+
import static test_crm.proxies.Company.MemberNames.Company_Group;
15+
16+
/**
17+
* Helper class for creating tests with a company objects
18+
*/
19+
public abstract class TestAuditWithData extends TestAuditBase {
20+
protected Company createBaseCompany(final Group... groups) throws CoreException {
21+
final Company company = new Company(context);
22+
23+
company.setName(NAME);
24+
company.setCompanyNr(COMPANY_NR);
25+
26+
company.setCompany_Group(Arrays.asList(groups));
27+
28+
company.commit();
29+
return company;
30+
}
31+
32+
protected ExpectedLog createExpectedLog(final TypeOfLog typeOfLog, final Company company,
33+
final IMendixObject... groupObjects) {
34+
if (typeOfLog.equals(TypeOfLog.Add)) {
35+
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
36+
.addAttribute(Name, NAME).addAttribute(CompanyNr, COMPANY_NR)
37+
.addAttribute(InternNr, company.getInternNr())
38+
.addAttribute(Dec, 0).addAttribute(Number, 0)
39+
.addAttribute(Founded, "")
40+
.addReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
41+
} else {
42+
return new ExpectedLog(typeOfLog, Company.entityName, admin, initialDate, Company.entityName)
43+
.keepAttribute(Name, NAME).keepAttribute(CompanyNr, COMPANY_NR)
44+
.keepAttribute(InternNr, company.getInternNr())
45+
.keepAttribute(Dec, 0).keepAttribute(Number, 0)
46+
.keepAttribute(Founded, "")
47+
.keepReferences(Company_Group, context, MemberType.ReferenceSet, groupObjects);
48+
}
49+
}
50+
51+
protected static final String NAME = "Company";
52+
protected static final String COMPANY_NR = "123";
53+
}

0 commit comments

Comments
 (0)