GeneratorImplFunction { static String getInstantiationSourceCode(Configuration config, SourceCodeAppender appender, TestMethodMeta testMethodMeta) { Assertion.on("config").mustNotBeNull(config); Assertion.on("testMethodMeta").mustNotBeNull(testMethodMeta); if (config.isExtensionEnabled && config.extConfiguration.extInstantiations != null) { for (ExtInstantiation ins : config.extConfiguration.extInstantiations) { if (isCanonicalClassNameUsed(ins.canonicalClassName, testMethodMeta.classMeta.name, testMethodMeta.classMeta)) { for (String newImport : ins.importList) { testMethodMeta.classMeta.importedList.add(newImport); } StringBuilder buf = new StringBuilder(); if (ins.preAssignCode != null && ins.preAssignCode.trim().length() > 0) { appender.appendExtensionSourceCode(buf, ins.preAssignCode); } appender.appendTabs(buf, 2); buf.append(testMethodMeta.classMeta.name); buf.append(" target = "); buf.append(ins.assignCode.trim()); if (!ins.assignCode.trim().endsWith(StringValue.Semicolon)) { buf.append(StringValue.Semicolon); } appender.appendLineBreak(buf); if (ins.postAssignCode != null && ins.postAssignCode.trim().length() > 0) { appender.appendExtensionPostAssignSourceCode(buf, ins.postAssignCode, new String[] { "\\{instance\\}" }, "target"); } return buf.toString(); } } } ConstructorGenerator constructorGenerator = ConstructorGeneratorFactory.create(config, appender .getLineBreakProvider()); return constructorGenerator.getFirstInstantiationSourceCode(config, testMethodMeta.classMeta); } private  GeneratorImplFunction();   }
@Test(expected = JUnitHelperCoreException.class) public void getInstantiationSourceCode_A$Configuration$SourceCodeAppender$TestMethodMeta_Null() throws Exception { Configuration config = null; TestMethodMeta testMethodMeta = null; GeneratorImplFunction.getInstantiationSourceCode(config, appender, testMethodMeta); }
@Test public void getInstantiationSourceCode_A$Configuration$SourceCodeAppender$TestMethodMeta() throws Exception { Configuration config = new Configuration(); config.isExtensionEnabled = true; config.extConfiguration = new ExtConfiguration(config); ExtInstantiation ins = new ExtInstantiation("com.example.Bean"); ins.importList.add("com.example.BeanFactory"); ins.assignCode = "BeanFactory.getInstance()"; config.extConfiguration.extInstantiations.add(ins); TestMethodMeta testMethodMeta = new TestMethodMeta(); ConstructorMeta cons = new ConstructorMeta(); cons.argNames.add("bean"); ArgTypeMeta argType = new ArgTypeMeta(); argType.name = "com.example.Bean"; argType.nameInMethodName = "Bean"; cons.argTypes.add(argType); testMethodMeta.classMeta = new ClassMeta(); testMethodMeta.classMeta.name = "Target"; testMethodMeta.classMeta.importedList.add("com.example.Bean"); testMethodMeta.classMeta.packageName = "com.example"; testMethodMeta.classMeta.constructors.add(cons); String actual = GeneratorImplFunction.getInstantiationSourceCode(config, appender, testMethodMeta); String expected = "\t\tcom.example.Bean bean = BeanFactory.getInstance();\r\n\t\tTarget target = new Target(bean);\r\n"; assertThat(actual, is(equalTo(expected))); }
@Test public void getInstantiationSourceCode_A$Configuration$SourceCodeAppender$TestMethodMeta_WithSemicolon() throws Exception { Configuration config = new Configuration(); config.isExtensionEnabled = true; config.extConfiguration = new ExtConfiguration(config); ExtInstantiation ins = new ExtInstantiation("com.example.Bean"); ins.importList.add("com.example.BeanFactory"); ins.assignCode = "BeanFactory.getInstance();"; config.extConfiguration.extInstantiations.add(ins); TestMethodMeta testMethodMeta = new TestMethodMeta(); ConstructorMeta cons = new ConstructorMeta(); cons.argNames.add("bean"); ArgTypeMeta argType = new ArgTypeMeta(); argType.name = "com.example.Bean"; argType.nameInMethodName = "Bean"; cons.argTypes.add(argType); testMethodMeta.classMeta = new ClassMeta(); testMethodMeta.classMeta.name = "Target"; testMethodMeta.classMeta.importedList.add("com.example.Bean"); testMethodMeta.classMeta.packageName = "com.example"; testMethodMeta.classMeta.constructors.add(cons); String actual = GeneratorImplFunction.getInstantiationSourceCode(config, appender, testMethodMeta); String expected = "\t\tcom.example.Bean bean = BeanFactory.getInstance();\r\n\t\tTarget target = new Target(bean);\r\n"; assertThat(actual, is(equalTo(expected))); }
@Test public void getInstantiationSourceCode_A$Configuration$SourceCodeAppender$TestMethodMeta_TargetByExtInstantiation() throws Exception { Configuration config = new Configuration(); config.isExtensionEnabled = true; config.extConfiguration = new ExtConfiguration(config); ExtInstantiation ins = new ExtInstantiation("com.example.Bean"); ins.importList.add("com.example.BeanFactory"); ins.assignCode = "BeanFactory.getInstance()"; config.extConfiguration.extInstantiations.add(ins); TestMethodMeta testMethodMeta = new TestMethodMeta(); testMethodMeta.classMeta = new ClassMeta(); testMethodMeta.classMeta.name = "Bean"; testMethodMeta.classMeta.importedList.add("com.example.Bean"); testMethodMeta.classMeta.packageName = "com.example"; String actual = GeneratorImplFunction.getInstantiationSourceCode(config, appender, testMethodMeta); String expected = "\t\tBean target = BeanFactory.getInstance();\r\n"; assertThat(actual, is(equalTo(expected))); }
@Test public void getInstantiationSourceCode_A$Configuration$SourceCodeAppender$TestMethodMeta_TargetByExtInstantiation_WithSemicolon() throws Exception { Configuration config = new Configuration(); config.isExtensionEnabled = true; config.extConfiguration = new ExtConfiguration(config); ExtInstantiation ins = new ExtInstantiation("com.example.Bean"); ins.importList.add("com.example.BeanFactory"); ins.assignCode = "BeanFactory.getInstance();"; config.extConfiguration.extInstantiations.add(ins); TestMethodMeta testMethodMeta = new TestMethodMeta(); testMethodMeta.classMeta = new ClassMeta(); testMethodMeta.classMeta.name = "Bean"; testMethodMeta.classMeta.importedList.add("com.example.Bean"); testMethodMeta.classMeta.packageName = "com.example"; String actual = GeneratorImplFunction.getInstantiationSourceCode(config, appender, testMethodMeta); String expected = "\t\tBean target = BeanFactory.getInstance();\r\n"; assertThat(actual, is(equalTo(expected))); }