Skip to content

Commit

Permalink
move the enableLoggingFail option from ArbitraryGeneratorContext to A…
Browse files Browse the repository at this point in the history
…rbitraryGeneratorLoggingContext (naver#1066)
  • Loading branch information
HanJaehee committed Oct 29, 2024
1 parent 35637b7 commit 2715458
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final class ArbitraryGeneratorContext implements Traceable {
private final int generateUniqueMaxTries;
private final AtomicReference<CombinableArbitrary<?>> generated =
new AtomicReference<>(CombinableArbitrary.NOT_GENERATED);
private final boolean enableLoggingFail;
private final ArbitraryGeneratorLoggingContext loggingContext;

public ArbitraryGeneratorContext(
Property resolvedProperty,
Expand All @@ -71,7 +71,7 @@ public ArbitraryGeneratorContext(
LazyArbitrary<PropertyPath> lazyPropertyPath,
MonkeyGeneratorContext monkeyGeneratorContext,
int generateUniqueMaxTries,
boolean enableLoggingFail
ArbitraryGeneratorLoggingContext loggingContext
) {
this.resolvedProperty = resolvedProperty;
this.property = property;
Expand All @@ -81,7 +81,7 @@ public ArbitraryGeneratorContext(
this.lazyPropertyPath = lazyPropertyPath;
this.monkeyGeneratorContext = monkeyGeneratorContext;
this.generateUniqueMaxTries = generateUniqueMaxTries;
this.enableLoggingFail = enableLoggingFail;
this.loggingContext = loggingContext;
}

public ArbitraryProperty getArbitraryProperty() {
Expand Down Expand Up @@ -152,8 +152,8 @@ public int getGenerateUniqueMaxTries() {
return generateUniqueMaxTries;
}

public boolean isEnableLoggingFail() {
return enableLoggingFail;
public ArbitraryGeneratorLoggingContext getLoggingContext() {
return loggingContext;
}

public CombinableArbitrary<?> getGenerated() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.navercorp.fixturemonkey.api.generator;


public final class ArbitraryGeneratorLoggingContext {
private final boolean enableLoggingFail;

public ArbitraryGeneratorLoggingContext(boolean enableLoggingFail) {
this.enableLoggingFail = enableLoggingFail;
}

public boolean isEnableLoggingFail() {
return enableLoggingFail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitraryDelegator;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorLoggingContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
Expand Down Expand Up @@ -66,7 +67,8 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
try {
checkPrerequisite(type);
} catch (Exception ex) {
if (context.isEnableLoggingFail()) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail()) {
LOGGER.warn("Given type {} is failed to generate due to the exception. It may be null.", type, ex);
}
return ArbitraryIntrospectorResult.NOT_INTROSPECTED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorLoggingContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.lazy.LazyArbitrary;
import com.navercorp.fixturemonkey.api.property.CompositeProperty;
Expand Down Expand Up @@ -90,7 +91,8 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
}
);
} catch (Exception ex) {
if (context.isEnableLoggingFail()) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail()) {
LOGGER.warn("Given type {} is failed to generate due to the exception. It may be null.", type, ex);
}
return ArbitraryIntrospectorResult.NOT_INTROSPECTED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorLoggingContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.ConstructorParameterPropertyGenerator;
import com.navercorp.fixturemonkey.api.property.Property;
Expand Down Expand Up @@ -68,7 +69,8 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)

Entry<Constructor<?>, String[]> parameterNamesByConstructor = TypeCache.getParameterNamesByConstructor(type);
if (parameterNamesByConstructor == null) {
if (context.isEnableLoggingFail()) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail()) {
LOGGER.warn(
"Given type {} is failed to generate due to the exception. It may be null.",
type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorLoggingContext;

@API(since = "0.6.0", status = Status.MAINTAINED)
public final class FailoverIntrospector implements ArbitraryIntrospector {
Expand Down Expand Up @@ -57,7 +58,8 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
results.add(new FailoverIntrospectorResult(introspector, result));
}
} catch (Exception ex) {
if (context.isEnableLoggingFail() || enableLoggingFail) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail() || enableLoggingFail) {
LOGGER.warn(
String.format(
"\"%s\" is failed to introspect \"%s\" type.",
Expand Down Expand Up @@ -85,7 +87,8 @@ public Object combined() {
result = iterator.next();
return result.getResult().getValue().combined();
} catch (Exception ex) {
if (context.isEnableLoggingFail() || enableLoggingFail) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail() || enableLoggingFail) {
LOGGER.warn(
String.format(
"\"%s\" is failed to introspect \"%s\" type.",
Expand Down Expand Up @@ -115,7 +118,8 @@ public Object rawValue() {
result = iterator.next();
return result.getResult().getValue().rawValue();
} catch (Exception ex) {
if (context.isEnableLoggingFail() || enableLoggingFail) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail() || enableLoggingFail) {
LOGGER.warn(
String.format(
"\"%s\" is failed to introspect type \"%s\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitrary;
import com.navercorp.fixturemonkey.api.arbitrary.CombinableArbitraryDelegator;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorLoggingContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.property.Property;
import com.navercorp.fixturemonkey.api.property.PropertyGenerator;
Expand Down Expand Up @@ -62,7 +63,8 @@ public ArbitraryIntrospectorResult introspect(ArbitraryGeneratorContext context)
try {
checkPrerequisite(type);
} catch (Exception ex) {
if (context.isEnableLoggingFail()) {
ArbitraryGeneratorLoggingContext loggingContext = context.getLoggingContext();
if (loggingContext.isEnableLoggingFail()) {
LOGGER.warn("Given type {} is failed to generate due to the exception. It may be null.", type, ex);
}
return ArbitraryIntrospectorResult.NOT_INTROSPECTED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.navercorp.fixturemonkey.api.context.MonkeyGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGenerator;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryGeneratorLoggingContext;
import com.navercorp.fixturemonkey.api.generator.ArbitraryProperty;
import com.navercorp.fixturemonkey.api.generator.CompositeArbitraryGenerator;
import com.navercorp.fixturemonkey.api.generator.IntrospectedArbitraryGenerator;
Expand Down Expand Up @@ -112,6 +113,9 @@ private ArbitraryGeneratorContext generateContext(
}

MonkeyGeneratorContext monkeyGeneratorContext = monkeyContext.retrieveGeneratorContext(rootProperty);
ArbitraryGeneratorLoggingContext loggingContext = new ArbitraryGeneratorLoggingContext(
fixtureMonkeyOptions.isEnableLoggingFail());

return new ArbitraryGeneratorContext(
resolvedParentProperty,
arbitraryProperty,
Expand All @@ -128,7 +132,7 @@ private ArbitraryGeneratorContext generateContext(
objectNode.getLazyPropertyPath(),
monkeyGeneratorContext,
fixtureMonkeyOptions.getGenerateUniqueMaxTries(),
fixtureMonkeyOptions.isEnableLoggingFail()
loggingContext
);
}

Expand Down

0 comments on commit 2715458

Please sign in to comment.