package org.automon.aspects;

import org.aspectj.lang.Aspects;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.automon.monitors.SysOut;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class AspectJBaseTest {

    @Before
    public void setUp() throws Exception {
        MyAspectJTestAspect aspect = Aspects.aspectOf(MyAspectJTestAspect.class);
        aspect.setOpenMon(new SysOut());
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testSys_monitor() throws Exception {
        MyTestClass obj = new MyTestClass();
        obj.hello();
        obj.world();
        obj.iAmHiding();
    }

    @Test
    public void testSys_exceptions() throws Exception {

    }

    @Test
    public void testIfEnabled() throws Exception {

    }

    // Note the @Override annotation was not used below as it will not compile with ajc.
    @Aspect
    static class MyAspectJTestAspect extends AspectJBase {

        @Pointcut("within(MyTestClass) && org.automon.pointcuts.Basic.publicMethod()")
        public void user_monitor() {

        }

        @Pointcut("within(MyTestClass) && org.automon.pointcuts.Basic.publicMethod()")
        public void user_exceptions() {
        }

    }


    private class MyTestClass {
        public void hello() {

        }

        public void world() {

        }

        private void iAmHiding() {

        }
    }
}
@@ -140,24 +140,61 @@
 n.Pointcut;%0A
+import org.automon.monitors.OpenMon;%0A
 import org.a
@@ -432,127 +432,214 @@
 ito.
-mock;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when;%0A%0Apublic class AspectJBaseTest %7B
+*;%0A%0Apublic class AspectJBaseTest %7B%0A%0A    public static final RuntimeException  TEST_RUNTIME_EXCEPTION = new RuntimeException(%22testing throwing exceptions%22);%0A    private OpenMon openMon = mock(OpenMon.class);
 %0A%0A  
@@ -803,20 +803,15 @@
 Mon(
-new SysOut()
+openMon
 );%0A 
@@ -1055,16 +1055,216 @@
 iding();
+%0A%0A        // start/stop pair should be called once per public method due to public method pointcut.%0A        verify(openMon, times(2)).start(anyString());%0A        verify(openMon, times(2)).stop(any());
 %0A    %7D%0A%0A
@@ -1333,85 +1333,260 @@
 n %7B%0A
-%0A
+   
     
-%7D%0A%0A    @Test%0A    public void testIfEnabled() throws Exception %7B%0A
+ MyTestClass obj = new MyTestClass();%0A        try %7B%0A            obj.throwException();%0A        %7D catch (Throwable t) %7B%0A            assertThat(t).isEqualTo(TEST_RUNTIME_EXCEPTION);%0A        %7D%0A%0A        verify(openMon).exception(anyString());
 %0A    %7D%0A%0A
     
@@ -1577,24 +1577,26 @@
 ());%0A    %7D%0A%0A
+%0A%0A
     // Note 
@@ -2187,16 +2187,108 @@
        %7D
+%0A%0A        public void throwException() %7B%0A            throw TEST_RUNTIME_EXCEPTION;%0A        %7D
 %0A    %7D%0A%7D

test - added test to see that exception handling logic is being handled in aspect