### Question:
DCRWeaver implements Weaver { @Override public String weave(Constructor<?> constructor) { return m_pointCutRegistry.add(constructor); } DCRWeaver(ClassFileTransformerFactory transformerFactory,
                   Instrumentation instrumentation); @Override String weave(Constructor<?> constructor); @Override String weave(Method method, TargetSource targetSource); @Override void applyChanges();  }### Answer:
@Test public void testMethodRegistration() throws Exception { final Weaver weaver = new DCRWeaver(m_classFileTransformerFactory, m_instrumentation); verify(m_classFileTransformerFactory) .create(m_pointCutRegistryCaptor.capture()); verify(m_instrumentation).addTransformer(m_transformer, true); final Method method = getClass().getDeclaredMethod("myMethod"); final String l1 = weaver.weave(method, TargetSource.FIRST_PARAMETER); final String l2 = weaver.weave(method, TargetSource.FIRST_PARAMETER); assertEquals(l1, l2); weaver.weave(method, TargetSource.FIRST_PARAMETER); final PointCutRegistry pointCutRegistry = m_pointCutRegistryCaptor.getValue(); final String internalClassName = getClass().getName().replace('.', '/'); final Map<Constructor<?>, List<WeavingDetails>> constructorPointCuts = pointCutRegistry.getConstructorPointCutsForClass(internalClassName); assertNull(constructorPointCuts); final Map<Method, List<WeavingDetails>> methodPointCuts = pointCutRegistry.getMethodPointCutsForClass(internalClassName); assertEquals(1, methodPointCuts.size()); final List<WeavingDetails> locations1 = methodPointCuts.get(method); assertEquals(1, locations1.size()); final String location1 = locations1.get(0).getLocation(); assertNotNull(location1); final Method method2 = getClass().getDeclaredMethod("myOtherMethod"); weaver.weave(method, TargetSource.FIRST_PARAMETER); weaver.weave(method2, TargetSource.FIRST_PARAMETER); final Map<Method, List<WeavingDetails>> pointCuts2 = pointCutRegistry.getMethodPointCutsForClass(internalClassName); assertEquals(2, pointCuts2.size()); final List<WeavingDetails> locations2 = pointCuts2.get(method); assertEquals(1, locations2.size()); assertEquals(new WeavingDetails(location1, TargetSource.FIRST_PARAMETER), locations2.get(0)); assertNotNull(pointCuts2.get(method2)); verifyNoMoreInteractions(m_classFileTransformerFactory, m_instrumentation); }
@Test public void testConstructorRegistration() throws Exception { final Weaver weaver = new DCRWeaver(m_classFileTransformerFactory, m_instrumentation); verify(m_classFileTransformerFactory) .create(m_pointCutRegistryCaptor.capture()); final Constructor<?> constructor = getClass().getDeclaredConstructor(); final String l1 = weaver.weave(constructor); final String l2 = weaver.weave(constructor); assertEquals(l1, l2); weaver.weave(constructor); final PointCutRegistry pointCutRegistry = m_pointCutRegistryCaptor.getValue(); final String internalClassName = getClass().getName().replace('.', '/'); final Map<Constructor<?>, List<WeavingDetails>> constructorPointCuts = pointCutRegistry.getConstructorPointCutsForClass(internalClassName); assertEquals(1, constructorPointCuts.size()); final Map<Method, List<WeavingDetails>> methodPointCuts = pointCutRegistry.getMethodPointCutsForClass(internalClassName); assertNull(methodPointCuts); final List<WeavingDetails> locations1 = constructorPointCuts.get(constructor); assertEquals(1, locations1.size()); final String location1 = locations1.get(0).getLocation(); assertNotNull(location1); }
@Test(expected=WeavingException.class) public void testInsufficientParameters() throws Exception { final Weaver weaver = new DCRWeaver(m_classFileTransformerFactory, m_instrumentation); final Method method = getClass().getDeclaredMethod("myMethod"); weaver.weave(method, TargetSource.SECOND_PARAMETER); }