1   package org.apache.fulcrum.yaafi.service;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.apache.avalon.framework.service.ServiceException;
27  import org.apache.fulcrum.yaafi.DependentTestComponent;
28  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
29  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
30  import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
31  import org.apache.fulcrum.yaafi.interceptor.logging.LoggingInterceptorService;
32  import org.apache.fulcrum.yaafi.service.advice.AdviceService;
33  
34  /**
35   * Test suite for the ServiceManagereService.
36   *
37   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
38   */
39  
40  public class AdviceServiceTest extends TestCase implements DependentTestComponent
41  {
42      private AdviceService service;
43      private DependentTestComponent advisedThis;
44      private ServiceContainer container;
45  
46  
47      /**
48       * Constructor
49       * @param name the name of the test case
50       */
51      public AdviceServiceTest( String name )
52      {
53          super(name);
54      }
55  
56      /**
57       * @see junit.framework.TestCase#setUp()
58       */
59      protected void setUp() throws Exception
60      {
61          super.setUp();
62          ServiceContainerConfiguration config = new ServiceContainerConfiguration();
63          config.loadContainerConfiguration( "./src/test/TestYaafiContainerConfig.xml" );
64          this.container = ServiceContainerFactory.create( config );
65          service = (AdviceService) this.container.lookup(AdviceService.class.getName());
66      }
67  
68      /**
69       * @see junit.framework.TestCase#tearDown()
70       */
71      protected void tearDown() throws Exception
72      {
73          ServiceContainerFactory.dispose(this.container);
74          super.tearDown();
75      }
76  
77      public static Test suite()
78      {
79          TestSuite suite= new TestSuite();
80  
81          suite.addTest( new AdviceServiceTest("testSimpleObject") );
82          suite.addTest( new AdviceServiceTest("testDefaultAdvice") );
83          suite.addTest( new AdviceServiceTest("testChainedAdvices") );
84          suite.addTest( new AdviceServiceTest("testMultipleProxies") );
85  
86  
87          return suite;
88      }
89  
90      /**
91       * Advice a StringBuffer based on the CharSequence interface
92       */
93      public void testSimpleAdvice() throws Exception
94      {
95          String[] interceptorList = { LoggingInterceptorService.class.getName() };
96          StringBuffer unadvicedObject = new StringBuffer("foo");
97          CharSequence advicedObject = (CharSequence) this.service.advice(unadvicedObject );
98  
99          int length = advicedObject.length();
100         assertTrue(this.service.isAdviced(advicedObject));
101         assertFalse(this.service.isAdviced(unadvicedObject));
102         assertTrue(unadvicedObject.length() == length);
103     }
104 
105     /**
106      * Advice a StringBuffer based on the CharSequence interface
107      */
108     public void testSimpleObject() throws Exception
109     {
110         String[] interceptorList = { LoggingInterceptorService.class.getName() };
111         StringBuffer unadvicedObject = new StringBuffer("foo");
112         CharSequence advicedObject = (CharSequence) this.service.advice("adviced", interceptorList, unadvicedObject );
113 
114         int length = advicedObject.length();
115         assertTrue(this.service.isAdviced(advicedObject));
116         assertFalse(this.service.isAdviced(unadvicedObject));
117         assertTrue(unadvicedObject.length() == length);
118     }
119 
120     /**
121      * Advice a StringBuffer based on the CharSequenceInterface with default interceptors
122      */
123     public void testDefaultAdvice() throws Exception
124     {
125         StringBuffer unadvicedObject = new StringBuffer("foo");
126         CharSequence advicedObject = (CharSequence) this.service.advice("default adviced", unadvicedObject );
127 
128         advicedObject.length();
129     }
130 
131     /**
132      * The test implements the DependentTestComponent interface therefore we
133      * are able to intercept the invocation of test(). Whereas test() invokes
134      * another advised component.
135      */
136     public void testChainedAdvices() throws Exception
137     {
138         String[] interceptorList = { LoggingInterceptorService.class.getName() };
139         this.advisedThis = (DependentTestComponent) this.service.advice(interceptorList, this);
140         this.advisedThis.test();
141     }
142 
143     /**
144      * Advice a StringBuffer based on the CharSequenceInterface
145      */
146     public void testMultipleProxies() throws Exception
147     {
148         String[] interceptorList = { LoggingInterceptorService.class.getName() };
149         StringBuffer unadvicedObject = new StringBuffer("foo");
150         CharSequence advicedObject = (CharSequence) this.service.advice("first advice", interceptorList, unadvicedObject);
151         CharSequence advicedAdvicedObject = (CharSequence) this.service.advice("second advice", interceptorList, advicedObject );
152 
153         advicedAdvicedObject.length();
154         assertTrue(this.service.isAdviced(advicedAdvicedObject));
155     }
156 
157     /**
158      * Advice a StringBuffer based on the CharSequenceInterface
159      */
160     public void test()
161     {
162         try
163         {
164             DependentTestComponent testComponent = (DependentTestComponent) this.container.lookup(
165                 DependentTestComponent.class.getName()
166                 );
167 
168             testComponent.test();
169         }
170         catch (ServiceException e)
171         {
172             throw new RuntimeException(e.getMessage());
173         }
174     }
175 }