-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArezTableTestUtil.java
More file actions
125 lines (113 loc) · 3.19 KB
/
Copy pathArezTableTestUtil.java
File metadata and controls
125 lines (113 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package arez.table;
import java.lang.reflect.Field;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Utility class for interacting with ArezTable config settings in tests.
*/
@SuppressWarnings( "WeakerAccess" )
@GwtIncompatible
public final class ArezTableTestUtil
{
private ArezTableTestUtil()
{
}
/**
* Interface to intercept log messages emitted by ArezTable runtime.
*/
public interface Logger
{
void log( @Nonnull String message, @Nullable Throwable throwable );
}
/**
* Reset the state of ArezTable config to either production or development state.
*
* @param production true to set it to production environment configuration, false to set it to development environment config.
*/
public static void resetConfig( final boolean production )
{
if ( ArezTableConfig.isProductionEnvironment() )
{
throw new IllegalStateException( "Unable to reset config as ArezTable is in production mode" );
}
if ( production )
{
noCheckApiInvariants();
}
else
{
checkApiInvariants();
}
resetState();
}
/**
* Reset the state of ArezTable.
* This occasionally needs to be invoked after changing configuration settings in tests.
*/
public static void resetState()
{
setLogger( null );
}
/**
* Specify the logger to use to capture logging in tests
*
* @param logger the logger.
*/
public static void setLogger( @Nullable final Logger logger )
{
if ( ArezTableConfig.isProductionEnvironment() )
{
throw new IllegalStateException( "Unable to call ArezTestUtil.setLogger() as ArezTable is in production mode" );
}
final LogUtil.ProxyLogger proxyLogger = (LogUtil.ProxyLogger) LogUtil.getLogger();
proxyLogger.setLogger( null == logger ? null : logger::log );
}
/**
* Set the `arez.table.check_api_invariants` setting to true.
*/
public static void checkApiInvariants()
{
setCheckApiInvariants( true );
}
/**
* Set the `arez.table.check_api_invariants` setting to false.
*/
public static void noCheckApiInvariants()
{
setCheckApiInvariants( false );
}
/**
* Configure the `arez.table.check_api_invariants` setting.
*
* @param setting the setting.
*/
private static void setCheckApiInvariants( final boolean setting )
{
setConstant( "CHECK_API_INVARIANTS", setting );
}
/**
* Set the specified field name on ArezTableConfig.
*/
@SuppressWarnings( { "NonJREEmulationClassesInClientCode", "SameParameterValue" } )
private static void setConstant( @Nonnull final String fieldName, final boolean value )
{
if ( ArezTableConfig.isProductionEnvironment() )
{
throw new IllegalStateException( "Unable to change constant " + fieldName +
" as ArezTable is in production mode" );
}
else
{
try
{
final Field field = ArezTableConfig.class.getDeclaredField( fieldName );
field.setAccessible( true );
field.set( null, value );
}
catch ( final NoSuchFieldException | IllegalAccessException e )
{
throw new IllegalStateException( "Unable to change constant " + fieldName, e );
}
}
}
}