|
| 1 | +package org.utibot.examples.spring; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.junit.jupiter.api.AfterEach; |
| 5 | +import org.junit.jupiter.api.Assertions; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import org.utbot.common.PathUtil; |
| 9 | +import org.utbot.examples.spring.autowiring.oneBeanForOneType.Order; |
| 10 | +import org.utbot.examples.spring.autowiring.oneBeanForOneType.ServiceWithInjectedField; |
| 11 | +import org.utbot.external.api.TestMethodInfo; |
| 12 | +import org.utbot.external.api.UtBotJavaApi; |
| 13 | +import org.utbot.external.api.UtModelFactory; |
| 14 | +import org.utbot.framework.codegen.domain.ForceStaticMocking; |
| 15 | +import org.utbot.framework.codegen.domain.Junit4; |
| 16 | +import org.utbot.framework.codegen.domain.MockitoStaticMocking; |
| 17 | +import org.utbot.framework.codegen.domain.ProjectType; |
| 18 | +import org.utbot.framework.context.ApplicationContext; |
| 19 | +import org.utbot.framework.context.simple.SimpleApplicationContext; |
| 20 | +import org.utbot.framework.context.spring.SpringApplicationContextImpl; |
| 21 | +import org.utbot.framework.plugin.api.*; |
| 22 | +import org.utbot.framework.util.Snippet; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.lang.reflect.Method; |
| 26 | +import java.net.URISyntaxException; |
| 27 | +import java.net.URL; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +import static org.utbot.external.api.UtModelFactoryKt.classIdForType; |
| 35 | +import static org.utbot.framework.plugin.api.MockFramework.MOCKITO; |
| 36 | +import static org.utbot.framework.util.TestUtilsKt.compileClassFile; |
| 37 | + |
| 38 | + |
| 39 | +class PredefinedGeneratorParameters { |
| 40 | + |
| 41 | + static String destinationClassName = "GeneratedTest"; |
| 42 | + |
| 43 | + static Method getMethodByName(Class<?> clazz, String name, Class<?>... parameters) { |
| 44 | + try { |
| 45 | + return clazz.getDeclaredMethod(name, parameters); |
| 46 | + } catch (NoSuchMethodException ignored) { |
| 47 | + Assertions.fail(); |
| 48 | + } |
| 49 | + throw new RuntimeException(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +public class SpringUtBotJavaApiTest { |
| 54 | + private UtModelFactory modelFactory; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + public void setUp() { |
| 58 | + modelFactory = new UtModelFactory(); |
| 59 | + } |
| 60 | + |
| 61 | + @AfterEach |
| 62 | + public void tearDown() { |
| 63 | + try { |
| 64 | + modelFactory = null; |
| 65 | + } catch (Exception e) { |
| 66 | + Assertions.fail(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @NotNull |
| 71 | + private static String normalizePath(URL url) throws URISyntaxException { |
| 72 | + return new File(url.toURI()).getPath(); |
| 73 | + } |
| 74 | + |
| 75 | + @NotNull |
| 76 | + private static String getClassPath(Class<?> clazz) { |
| 77 | + try { |
| 78 | + return normalizePath(clazz.getProtectionDomain().getCodeSource().getLocation()); |
| 79 | + } catch (URISyntaxException e) { |
| 80 | + throw new RuntimeException(e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @NotNull |
| 85 | + private static String getDependencyClassPath() { |
| 86 | + |
| 87 | + ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 88 | + URL[] urls = PathUtil.getUrlsFromClassLoader(contextClassLoader); |
| 89 | + |
| 90 | + |
| 91 | + return Arrays.stream(urls).map(url -> |
| 92 | + { |
| 93 | + try { |
| 94 | + return new File(url.toURI()).toString(); |
| 95 | + } catch (URISyntaxException e) { |
| 96 | + Assertions.fail(e); |
| 97 | + } |
| 98 | + throw new RuntimeException(); |
| 99 | + }).collect(Collectors.joining(File.pathSeparator)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testMultiMethodClass() { |
| 104 | + |
| 105 | + UtBotJavaApi.setStopConcreteExecutorOnExit(false); |
| 106 | + |
| 107 | + String classpath = getClassPath(ServiceWithInjectedField.class); |
| 108 | + String dependencyClassPath = getDependencyClassPath(); |
| 109 | + |
| 110 | + // Order clas |
| 111 | + HashMap<String, UtModel> fields = new HashMap<>(); |
| 112 | + fields.put("id", new UtPrimitiveModel(0L)); |
| 113 | + fields.put("buyer", new UtPrimitiveModel("")); |
| 114 | + fields.put("price", new UtPrimitiveModel(0d)); |
| 115 | + fields.put("qty", new UtPrimitiveModel(0)); |
| 116 | + |
| 117 | + UtCompositeModel orderClassModel = modelFactory.produceCompositeModel( |
| 118 | + classIdForType(Order.class), |
| 119 | + fields |
| 120 | + ); |
| 121 | + |
| 122 | + UtCompositeModel classUnderTestModel = modelFactory.produceCompositeModel( |
| 123 | + classIdForType(ServiceWithInjectedField.class) |
| 124 | + ); |
| 125 | + |
| 126 | + EnvironmentModels createOrderMethodState = new EnvironmentModels( |
| 127 | + classUnderTestModel, |
| 128 | + Collections.emptyList(), |
| 129 | + Collections.emptyMap() |
| 130 | + ); |
| 131 | + |
| 132 | + EnvironmentModels getOrdersMethodState = new EnvironmentModels( |
| 133 | + classUnderTestModel, |
| 134 | + Collections.singletonList(orderClassModel), |
| 135 | + Collections.emptyMap() |
| 136 | + ); |
| 137 | + |
| 138 | + |
| 139 | + Method getOrdersMethodUnderTest = PredefinedGeneratorParameters.getMethodByName( |
| 140 | + ServiceWithInjectedField.class, |
| 141 | + "getOrders" |
| 142 | + ); |
| 143 | + |
| 144 | + Method createOrderMethodUnderTest = PredefinedGeneratorParameters.getMethodByName( |
| 145 | + ServiceWithInjectedField.class, |
| 146 | + "createOrder", |
| 147 | + Order.class |
| 148 | + ); |
| 149 | + |
| 150 | + TestMethodInfo firstTestMethodInfo = new TestMethodInfo( |
| 151 | + createOrderMethodUnderTest, |
| 152 | + createOrderMethodState); |
| 153 | + |
| 154 | + TestMethodInfo secondTestMethodInfo = new TestMethodInfo( |
| 155 | + getOrdersMethodUnderTest, |
| 156 | + getOrdersMethodState); |
| 157 | + |
| 158 | + ApplicationContext springApplicationContext = new SpringApplicationContextImpl( |
| 159 | + new SimpleApplicationContext(), |
| 160 | + Collections.emptyList(), |
| 161 | + SpringTestType.UNIT_TEST, |
| 162 | + SpringSettings.AbsentSpringSettings |
| 163 | + ); |
| 164 | + |
| 165 | + List<UtMethodTestSet> testSets = UtBotJavaApi.generateTestSets( |
| 166 | + Arrays.asList(firstTestMethodInfo, secondTestMethodInfo, secondTestMethodInfo), |
| 167 | + ServiceWithInjectedField.class, |
| 168 | + classpath, |
| 169 | + dependencyClassPath, |
| 170 | + MockStrategyApi.OTHER_PACKAGES, |
| 171 | + 60000L, |
| 172 | + springApplicationContext |
| 173 | + ); |
| 174 | + |
| 175 | + String generationResult = UtBotJavaApi.generate( |
| 176 | + Collections.emptyList(), |
| 177 | + testSets, |
| 178 | + PredefinedGeneratorParameters.destinationClassName, |
| 179 | + classpath, |
| 180 | + dependencyClassPath, |
| 181 | + ServiceWithInjectedField.class, |
| 182 | + ProjectType.PureJvm, |
| 183 | + Junit4.INSTANCE, |
| 184 | + MOCKITO, |
| 185 | + CodegenLanguage.JAVA, |
| 186 | + MockitoStaticMocking.INSTANCE, |
| 187 | + false, |
| 188 | + ForceStaticMocking.DO_NOT_FORCE, |
| 189 | + ServiceWithInjectedField.class.getPackage().getName() |
| 190 | + ); |
| 191 | + |
| 192 | + Snippet snippet = new Snippet(CodegenLanguage.JAVA, generationResult); |
| 193 | + compileClassFile(PredefinedGeneratorParameters.destinationClassName, snippet); |
| 194 | + } |
| 195 | +} |
0 commit comments