MocktailTemplateProcesser { String processTemplate(Mocktail mocktail, AspectType aspectType, MocktailMode mocktailMode) { VelocityContext context = new VelocityContext(); Map<String, Object> dynamicValues = getTemplateParameterValues(mocktail, aspectType); for (Map.Entry<String, Object> contextEntry : dynamicValues.entrySet()) { context.put(contextEntry.getKey(), contextEntry.getValue()); } BufferedReader reader = new BufferedReader(new InputStreamReader(getAspectTemplate(aspectType, mocktailMode))); StringWriter writer = new StringWriter(); try { VelocityEngine ve = new VelocityEngine(); ve.evaluate(context, writer, "", reader); } catch (ParseErrorException e) { throw new IllegalStateException(e); } catch (MethodInvocationException e) { throw new IllegalStateException(e); } catch (ResourceNotFoundException e) { throw new IllegalStateException(e); } catch (IOException e) { throw new IllegalStateException(e); } return writer.toString(); }  void createAspect(Mocktail mocktail, AspectType aspectType,
            MocktailMode mocktailMode, File aspectsRootDirectory);  }
@Test public void shouldCreateClassAspect() throws Exception { Mocktail mocktail = createClassMocktail(); MocktailTemplateProcesser templateProcessor = new MocktailTemplateProcesser(); String templatedString = templateProcessor.processTemplate(mocktail, AspectType.CLASS_ASPECT_TYPE, MocktailMode.RECORDING); String expectedpointcut = "pointcut callPointcut() : call(* com.svashishtha.mocktail.metadata.aj.creator.TemplateProcesserTest.*(..));"; assertTrue(templatedString.contains(expectedpointcut)); assertTrue(templatedString .contains("String fqcn = \"com.svashishtha.mocktail.metadata.aj.creator.TemplateProcesserTest\";")); }
@Test public void shouldCreateMethodAspect() throws Exception { Mocktail mocktail = createMethodMocktail(); MocktailTemplateProcesser templateProcessor = new MocktailTemplateProcesser(); String templatedString = templateProcessor.processTemplate(mocktail, AspectType.METHODS_ASPECT_TYPE, MocktailMode.RECORDING); System.out.println(templatedString); String expectedpointcut = "@Around(\"execution(* com.svashishtha.mocktail.metadata.aj.creator.TemplateProcesserTest.method1(..))\")"; assertTrue(templatedString.contains(expectedpointcut)); expectedpointcut = "@Around(\"execution(* com.svashishtha.mocktail.metadata.aj.creator.TemplateProcesserTest.method2(..))\")"; assertTrue(templatedString.contains(expectedpointcut)); assertTrue(templatedString .contains("fqcn = \"com.svashishtha.mocktail.metadata.aj.creator.TemplateProcesserTest\";")); }
@Test @Ignore public void shouldCreateRecordingMethodAspects() throws Exception { setupMocktailContainer(); final Mocktail methodMocktail = MocktailObjectMother .createMethodMocktail("name", "com.svashishtha.mocktail.metadata", "method1", "method2"); MocktailTemplateProcesser templateProcessor = new MocktailTemplateProcesser(); String templatedMethodObjectString = templateProcessor.processTemplate( methodMocktail, AspectType.METHODS_ASPECT_TYPE, MocktailMode.RECORDING); assertThat(templatedMethodObjectString, containsString("public aspect RecorderAspectname")); assertThat(templatedMethodObjectString, containsString("String recordingDirectoryPath = \"root_dir\";")); assertThat(templatedMethodObjectString, containsString("String fqcn = \"com.svashishtha.mocktail.metadata.name\";")); assertThat( templatedMethodObjectString, containsString("pointcut callPointcutmethod1() : call(* com.svashishtha.mocktail.metadata.name.method1(..));")); assertThat( templatedMethodObjectString, containsString("pointcut callPointcutmethod2() : call(* com.svashishtha.mocktail.metadata.name.method2(..));")); }
@Test @Ignore public void shouldCreatePlaybackMethodAspects() throws Exception { final Mocktail methodMocktail = MocktailObjectMother .createMethodMocktail("name", "com.svashishtha.mocktail.metadata", "method1", "method2"); MocktailTemplateProcesser templateProcessor = new MocktailTemplateProcesser(); String templatedMethodObjectString = templateProcessor.processTemplate( methodMocktail, AspectType.METHODS_ASPECT_TYPE, MocktailMode.PLAYBACK); System.out.println("The templatemethod string is\n"+templatedMethodObjectString); assertThat(templatedMethodObjectString, containsString("public class PlaybackAspectname")); assertThat(templatedMethodObjectString, containsString("recordingDirectoryPath = \"root_dir\";")); assertThat(templatedMethodObjectString, containsString("String fqcn = \"com.svashishtha.mocktail.metadata.name\";")); assertThat( templatedMethodObjectString, containsString("pointcut callPointcutmethod1() : call(* com.svashishtha.mocktail.metadata.name.method1(..));")); assertThat( templatedMethodObjectString, containsString("pointcut callPointcutmethod2() : call(* com.svashishtha.mocktail.metadata.name.method2(..));")); }
@Test public void shouldCreateRecordingAspectForClass() throws Exception { final Mocktail classMocktail = MocktailObjectMother .createClassMocktail("AspectedClass", "com.sandy"); MocktailTemplateProcesser templateProcessor = new MocktailTemplateProcesser(); String templatedClassObjectString = templateProcessor.processTemplate( classMocktail, AspectType.CLASS_ASPECT_TYPE, MocktailMode.RECORDING); System.out.println(templatedClassObjectString); assertThat(templatedClassObjectString, containsString("public aspect RecorderAspectAspectedClass")); assertThat(templatedClassObjectString, containsString("String fqcn = \"com.sandy.AspectedClass\";")); assertThat(templatedClassObjectString, containsString("recordingDirectoryPath = \"root_dir\"")); assertThat( templatedClassObjectString, containsString("pointcut callPointcut() : call(* com.sandy.AspectedClass.*(..));")); System.out.println(templatedClassObjectString); }