| 1 |
|
package guru.mikelue.foxglove.springframework; |
| 2 |
|
|
| 3 |
|
import java.util.function.Supplier; |
| 4 |
|
|
| 5 |
|
import javax.sql.DataSource; |
| 6 |
|
|
| 7 |
|
import org.springframework.beans.factory.BeanFactory; |
| 8 |
|
import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
| 9 |
|
import org.springframework.context.ApplicationContext; |
| 10 |
|
import org.springframework.jdbc.datasource.DataSourceUtils; |
| 11 |
|
import org.springframework.test.context.NestedTestConfiguration; |
| 12 |
|
import org.springframework.test.context.TestContext; |
| 13 |
|
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener; |
| 14 |
|
import org.springframework.test.context.support.AbstractTestExecutionListener; |
| 15 |
|
import org.springframework.test.context.transaction.TestTransaction; |
| 16 |
|
|
| 17 |
|
import org.slf4j.Logger; |
| 18 |
|
import org.slf4j.LoggerFactory; |
| 19 |
|
|
| 20 |
|
import guru.mikelue.foxglove.DataGenerator; |
| 21 |
|
import guru.mikelue.foxglove.TableFacet; |
| 22 |
|
import guru.mikelue.foxglove.annotation.*; |
| 23 |
|
import guru.mikelue.foxglove.functional.DataGeneratorProvider; |
| 24 |
|
import guru.mikelue.foxglove.functional.TableFacetsProvider; |
| 25 |
|
import guru.mikelue.foxglove.jdbc.JdbcDataGenerator; |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
@link@link |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
@link |
| 35 |
|
@link@link@link |
| 36 |
|
@link@link |
| 37 |
|
@link@link |
| 38 |
|
@link |
| 39 |
|
|
| 40 |
|
@link@link |
| 41 |
|
@link |
| 42 |
|
@link |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
@link |
| 47 |
|
|
| 48 |
|
@link |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
@link@link |
| 53 |
|
@link@link |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
@link |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@link |
| 64 |
|
|
| 65 |
|
@see |
| 66 |
|
@see |
| 67 |
|
@see |
| 68 |
|
@see |
| 69 |
|
|
| |
|
| 79.2% |
Uncovered Elements: 5 (24) |
Complexity: 8 |
Complexity Density: 0.5 |
|
| 70 |
|
public class FoxgloveTestListener extends AbstractTestExecutionListener { |
| 71 |
|
private final Logger logger = LoggerFactory.getLogger(FoxgloveTestListener.class); |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@link |
| 75 |
|
|
| 76 |
|
public final static int ORDER = 5000 + 1; |
| 77 |
|
|
| 78 |
|
private final static String ATTR_DATA_GEN_CONTEXT_PREFIX = "foxglove.data_gen_context."; |
| 79 |
|
private final static String ATTR_CLASS_LEVEL_PREFIX = "foxglove.class_level."; |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 84 |
6 |
public FoxgloveTestListener() {}... |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
@link |
| 88 |
|
|
| 89 |
|
@return |
| 90 |
|
|
| 91 |
|
@see |
| 92 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 93 |
40 |
@Override... |
| 94 |
|
public int getOrder() |
| 95 |
|
{ |
| 96 |
40 |
return ORDER; |
| 97 |
|
} |
| 98 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 99 |
4 |
@Override... |
| 100 |
|
public void beforeTestClass(TestContext testContext) throws Exception |
| 101 |
|
{ |
| 102 |
4 |
testContext.computeAttribute( |
| 103 |
|
keyForDataGenContext(testContext.getTestClass()), |
| 104 |
|
clazz -> DataGenContext.loadFrom( |
| 105 |
|
testContext.getTestClass() |
| 106 |
|
) |
| 107 |
|
); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 110 |
7 |
@SuppressWarnings("unchecked")... |
| 111 |
|
@Override |
| 112 |
|
public void beforeTestMethod(TestContext testContext) throws Exception |
| 113 |
|
{ |
| 114 |
7 |
Supplier<DataGenContext<TableFacet>> contextSupplier = () -> |
| 115 |
|
(DataGenContext<TableFacet>)testContext.getAttribute( |
| 116 |
|
keyForDataGenContext(testContext.getTestClass()) |
| 117 |
|
); |
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
7 |
var classLevelAttrName = ATTR_CLASS_LEVEL_PREFIX + testContext.getTestClass().getCanonicalName(); |
| 123 |
7 |
testContext.computeAttribute( |
| 124 |
|
classLevelAttrName, |
| 125 |
|
name -> doOnClassLevel( |
| 126 |
|
testContext, contextSupplier |
| 127 |
|
) |
| 128 |
|
); |
| 129 |
|
|
| 130 |
|
|
| 131 |
7 |
new SpringContextGenDataProcessor( |
| 132 |
|
testContext.getApplicationContext(), |
| 133 |
|
testContext.getTestInstance(), testContext.getTestMethod(), |
| 134 |
|
contextSupplier, |
| 135 |
|
testContext.getTestMethod().getName() |
| 136 |
|
) |
| 137 |
|
.performGenerating(); |
| 138 |
|
} |
| 139 |
|
|
| |
|
| 54.5% |
Uncovered Elements: 5 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 140 |
3 |
private Integer doOnClassLevel(... |
| 141 |
|
TestContext testContext, Supplier<DataGenContext<TableFacet>> dataGenContextSupplier |
| 142 |
|
) { |
| 143 |
3 |
var testingInstance = testContext.getTestInstance(); |
| 144 |
3 |
var testingClass = testContext.getTestClass(); |
| 145 |
|
|
| 146 |
3 |
var processor = new SpringContextGenDataProcessor( |
| 147 |
|
testContext.getApplicationContext(), |
| 148 |
|
testingInstance, testingClass, dataGenContextSupplier, |
| 149 |
|
testingClass.getSimpleName() |
| 150 |
|
); |
| 151 |
|
|
| 152 |
3 |
if (!processor.hasDataGenerating()) { |
| 153 |
3 |
return -1; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
0 |
logger.debug("Performing data generation on class level: \"{}\"", |
| 157 |
|
testingInstance.getClass().getSimpleName() |
| 158 |
|
); |
| 159 |
|
|
| 160 |
0 |
try { |
| 161 |
0 |
return processor.performGenerating(); |
| 162 |
|
} catch (Exception e) { |
| 163 |
0 |
throw new RuntimeException( |
| 164 |
|
"Failed to generating data for class level: " + testingInstance.getClass().getSimpleName(), |
| 165 |
|
e |
| 166 |
|
); |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 170 |
11 |
private static String keyForDataGenContext(Class<?> testedClass)... |
| 171 |
|
{ |
| 172 |
11 |
return ATTR_DATA_GEN_CONTEXT_PREFIX + testedClass.getCanonicalName(); |
| 173 |
|
} |
| 174 |
|
} |
| 175 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 176 |
|
class SpringContextGenDataProcessor extends GenDataProcessor { |
| 177 |
|
private final ApplicationContext appContext; |
| 178 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 179 |
10 |
public SpringContextGenDataProcessor(... |
| 180 |
|
ApplicationContext appContext, |
| 181 |
|
Object testingInstance, java.lang.reflect.AnnotatedElement annotatedElement, |
| 182 |
|
Supplier<DataGenContext<TableFacet>> dataGenContextSupplier, |
| 183 |
|
String name |
| 184 |
|
) { |
| 185 |
10 |
super( |
| 186 |
|
testingInstance, annotatedElement, |
| 187 |
|
dataGenContextSupplier, name |
| 188 |
|
); |
| 189 |
|
|
| 190 |
10 |
this.appContext = appContext; |
| 191 |
|
} |
| 192 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 193 |
7 |
@Override... |
| 194 |
|
protected GenDataObjectFactory buildObjectFactory( |
| 195 |
|
Object testingInstance, GenData genData, |
| 196 |
|
DataGenContext<TableFacet> dataGenContext, |
| 197 |
|
String name |
| 198 |
|
) { |
| 199 |
7 |
return new SpringContextObjectFactory( |
| 200 |
|
appContext, |
| 201 |
|
testingInstance, genData, |
| 202 |
|
dataGenContext, name |
| 203 |
|
); |
| 204 |
|
} |
| 205 |
|
} |