Beispiel #1
0
        public void ClassWithConstructorArguments()
        {
            String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
                              " " +
                              " aspect MyAspect for ComplexClass " +
                              "   " +
                              "   pointcut method|property(*)" +
                              "     advice(AspectSharp.Tests.Classes.LogInvocationInterceptor)" +
                              "   end" +
                              "   " +
                              " end ";

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
            AspectEngine        engine  = builder.Build();

            ComplexClass instance = null;

            instance = engine.WrapClass(typeof(ComplexClass), "Eric Cartman") as ComplexClass;
            Assert.AreEqual("Eric Cartman", instance.Name);
            Assert.IsFalse(instance.Started);
            InvokeAndAssert(instance);

            instance = engine.WrapClass(typeof(ComplexClass), "Kenny McKormick", true) as ComplexClass;
            Assert.AreEqual("Kenny McKormick", instance.Name);
            Assert.IsTrue(instance.Started);
            InvokeAndAssert(instance);

            String[] messages = LogInvocationInterceptor.Messages;
            Assert.AreEqual(20, messages.Length);
        }
		private static void MixinAndInterceptorExecution()
		{
			Console.Out.WriteLine( " o0o Within security checking o0o " );

			// For the sake of readability we're keep the aspect code here:
			String contents = 
				" import AspectSharp.Example.Aop.Interceptors " + 
				" import AspectSharp.Example.Aop.Mixins " + 
				" " +
				" aspect sample for [ AspectSharp.Example.ContentProviders ] " + 
				"   include SecurityMixin " + 
				"   " + 
				"   pointcut method(* RetrieveContent())" + 
				"     advice(SecurityCheckInterceptor)" + 
				"   end" + 
				"   " + 
				" end ";

			AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder( contents );
			AspectEngine engine = builder.Build();

			RequestPipeline pipeline = new RequestPipeline();
			pipeline.Context["username"] = "******";
			pipeline.AddContentProvider( engine.WrapClass( typeof(StaticContentProvider) ) as IContentProvider );
			pipeline.AddContentProvider( engine.WrapClass( typeof(DynamicContentProvider) ) as IContentProvider );
			pipeline.View = new PlainTextView();
			pipeline.ProcessRequest( Console.Out );

			Console.Out.WriteLine();
		}
		private static void HashtableTest()
		{
			Console.Out.WriteLine( " o0o Changing default hashtable value o0o " );

			// For the sake of readability we're keep the aspect code here:
			String contents = 
				" import System.Collections " + 
				" " +
				" aspect sample for Hashtable " + 
				"   " + 
				"   pointcut propertyread(* Item(*))" + 
				"     advice(HashcodeDefaultValueInterceptor)" + 
				"   end" + 
				"   " + 
				" end ";

			AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder( contents );
			AspectEngine engine = builder.Build();

			IDictionary myHashTable = engine.WrapClass( typeof(Hashtable) ) as IDictionary;

			String value = myHashTable["item"] as String;
			Console.Out.WriteLine( "Default value is {0}", value );

			Console.Out.WriteLine();			
		}
Beispiel #4
0
        public void ClassWithConstructorArgumentsAndNoAspects()
        {
            String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
                              " interceptors [" +
                              " \"key\" : DummyInterceptor " +
                              " ]" +
                              " mixins [" +
                              " \"key\" : DummyMixin " +
                              " ]" +
                              " " +
                              " aspect McBrother for DummyCustomer " +
                              "   include \"key\"" +
                              "   " +
                              "   pointcut method(*)" +
                              "     advice(\"key\")" +
                              "   end" +
                              "   " +
                              " end ";

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
            AspectEngine        engine  = builder.Build();

            ComplexClass instance = null;

            instance = engine.WrapClass(typeof(ComplexClass), "Eric Cartman") as ComplexClass;
            Assert.AreEqual("Eric Cartman", instance.Name);
            Assert.IsFalse(instance.Started);
        }
        public void ImplementLoggerProperty()
        {
            String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
                              " " +
                              " aspect McBrother for Author " +
                              "   " +
                              "   include Loggeable" +
                              "   " +
                              "   pointcut method|property(*)" +
                              "     advice(LogInvocationsInterceptor)" +
                              "   end" +
                              "   " +
                              " end ";

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
            AspectEngine        engine  = builder.Build();

            Author author = engine.WrapClass(typeof(Author)) as Author;

            Assert.IsNotNull(author);
            Assert.IsNotNull(author as ILoggeable);

            Assert.AreEqual(0, author.BooksPublished);
            author.MoreOneBook();
            Assert.AreEqual(1, author.BooksPublished);

            ILoggeable log      = author as ILoggeable;
            String     messages = log.GetLogMessages();

            // TODO: Correct compare
            Assert.AreEqual("Invoking get_BooksPublished;Invoking MoreOneBook;Invoking get_BooksPublished;", messages);
        }
        public void MakeAuthorAPersonAndACustomer()
        {
            String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
                              " " +
                              " aspect McBrother for Author " +
                              "   " +
                              "   include DummyCustomer" +
                              "   include DummyPerson" +
                              "   " +
                              " end ";

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
            AspectEngine        engine  = builder.Build();

            Author author = engine.WrapClass(typeof(Author)) as Author;

            Assert.IsNotNull(author);
            Assert.IsNotNull(author as ICustomer);
            Assert.IsNotNull(author as IPerson);

            ICustomer customer = author as ICustomer;

            customer.CustomerId = 10;
            Assert.AreEqual(10, customer.CustomerId);

            IPerson person = author as IPerson;

            person.Name = "Billy Paul McKinsky";
            Assert.AreEqual(10, customer.CustomerId);

            Assert.AreEqual(0, author.BooksPublished);
            author.MoreOneBook();
            Assert.AreEqual(1, author.BooksPublished);
        }
        static void Main(string[] args)
        {
            String weavingRules =
                " import AOPServices.Aspects " +
                " " +
                " aspect AuthorizationAspect for [ AOPServices.Services ] " +
                "   " +
                "   pointcut method(* Start())" +
                "     advice(AuthorizationAdvice)" +
                "   end" +
                "   " +
                "   pointcut method(* Stop())" +
                "     advice(AuthorizationAdvice)" +
                "   end" +
                "   " +
                " end ";

            AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder(weavingRules);
            AspectEngine engine = builder.Build();

            NASDAQHeartBeatService nasdaqService = engine.WrapClass(typeof(NASDAQHeartBeatService)) as NASDAQHeartBeatService;

            nasdaqService.Start();
            Console.ReadLine();
        }
Beispiel #8
0
        /// <summary>
        /// Shows interception of an class method.
        /// </summary>
        private static void ClassInvocation()
        {
            Console.WriteLine("ClassInvocation");

            //Wraping the class
            MachineGun machineGun = _engine.WrapClass(typeof(MachineGun)) as MachineGun;

            //Not intercepted. It isn't a virtual method.
            machineGun.Fire(5);

            //Intercepted.
            machineGun.FireTenTimes();
        }
        public void MixinProxyAware()
        {
            String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
                              " " +
                              " aspect McBrother for LogEnabledAuthor " +
                              "   " +
                              "   include LogFactoryMixin" +
                              "   " +
                              " end ";

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
            AspectEngine        engine  = builder.Build();

            LogEnabledAuthor author = engine.WrapClass(typeof(LogEnabledAuthor)) as LogEnabledAuthor;

            Assert.IsNotNull(author);
            Assert.IsNotNull(author as ILogEnabled);
            Assert.IsNotNull(author.Logger);
        }
        private static void WrapAndInvokeEverything(AspectEngine engine)
        {
            ComplexClass instance = engine.WrapClass(typeof(ComplexClass)) as ComplexClass;

            instance.DoNothing();
            instance.DoSomething();
            int arg = 1;

            instance.DoSomething(arg);
            instance.DoSomething(arg, "hiya");

            //TODO: Intercept by ref calls.
            //Assert.AreEqual(arg, instance.Pong(ref arg));

            instance.Name = "John Johnson";
            Assert.AreEqual("John Johnson", instance.Name);
            instance.Started = true;
            Assert.IsTrue(instance.Started);
        }
Beispiel #11
0
		private static void WrapAndInvokeEverything(AspectEngine engine)
		{
			long begin = DateTime.Now.Ticks;

			ComplexClass instance = engine.WrapClass(typeof(ComplexClass)) as ComplexClass;

			for(int i=0; i < 10000; i++)
			{
				instance.DoNothing();
				instance.DoSomething();
				instance.DoSomething(1);
				instance.DoSomething(1, "hiya");
				instance.Name = "John Johnson";
				Assert.AreEqual( "John Johnson", instance.Name );
				instance.Started = true;
				Assert.IsTrue( instance.Started );
			}

			long end = DateTime.Now.Ticks;
			long result = (end - begin) / 1000;
			System.Console.WriteLine( "Execution took " + (result).ToString() + " ms " );
		}
Beispiel #12
0
        private static void WrapAndInvokeEverything(AspectEngine engine)
        {
            long begin = DateTime.Now.Ticks;

            ComplexClass instance = engine.WrapClass(typeof(ComplexClass)) as ComplexClass;

            for (int i = 0; i < 10000; i++)
            {
                instance.DoNothing();
                instance.DoSomething();
                instance.DoSomething(1);
                instance.DoSomething(1, "hiya");
                instance.Name = "John Johnson";
                Assert.AreEqual("John Johnson", instance.Name);
                instance.Started = true;
                Assert.IsTrue(instance.Started);
            }

            long end    = DateTime.Now.Ticks;
            long result = (end - begin) / 1000;

            System.Console.WriteLine("Execution took " + (result).ToString() + " ms ");
        }
        public void MixinMethodsMustBeIntercepted()
        {
            String contents = "import AspectSharp.Tests.Classes in AspectSharp.Tests " +
                              " " +
                              " aspect McBrother for Author " +
                              "   " +
                              "   include Loggeable" +
                              "   include DummyPerson" +
                              "   " +
                              "   pointcut method|property(*)" +
                              "     advice(LogInvocationsInterceptor)" +
                              "   end" +
                              "   " +
                              " end ";

            AspectEngineBuilder builder = new AspectLanguageEngineBuilder(contents);
            AspectEngine        engine  = builder.Build();

            Author author = engine.WrapClass(typeof(Author)) as Author;

            Assert.IsNotNull(author);
            Assert.IsNotNull(author as ILoggeable);
            Assert.IsNotNull(author as IPerson);

            IPerson person = author as IPerson;

            person.Name = "McBilly";
            Assert.AreEqual("McBilly", person.Name);

            ILoggeable log = author as ILoggeable;

            log.Log("Test");
            String messages = log.GetLogMessages();

            Assert.AreEqual("Invoking set_Name;Invoking get_Name;Test;", messages);
        }
Beispiel #14
0
		private static void WrapAndInvokeEverything(AspectEngine engine)
		{
			ComplexClass instance = engine.WrapClass(typeof(ComplexClass)) as ComplexClass;
			instance.DoNothing();
			instance.DoSomething();
			int arg = 1;

			instance.DoSomething(arg);
			instance.DoSomething(arg, "hiya");

			//TODO: Intercept by ref calls.
			//Assert.AreEqual(arg, instance.Pong(ref arg));
			
			instance.Name = "John Johnson";
			Assert.AreEqual( "John Johnson", instance.Name );
			instance.Started = true;
			Assert.IsTrue( instance.Started );
		}