KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > declare > DeclareTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.declare;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.jboss.test.aop.AOPTestWithSetup;
28 import org.jboss.test.aop.declare.businesslayer.BusinessObject;
29
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32 import junit.textui.TestRunner;
33
34 /**
35  *
36  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
37  * @version $Revision: 45977 $
38  */

39 public class DeclareTestCase extends AOPTestWithSetup
40 {
41    public static void main(String JavaDoc[] args)
42    {
43       TestRunner.run(suite());
44    }
45
46    public static Test suite()
47    {
48       TestSuite suite = new TestSuite("DotInPointcutNameTester");
49       suite.addTestSuite(DeclareTestCase.class);
50       return suite;
51    }
52
53    public DeclareTestCase(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    public void testLoadtimeDeclare()throws Exception JavaDoc
59    {
60       System.out.println("*** testLoadTimeDeclare");
61       SystemOutDecorator out = null;
62       try
63       {
64          out = SystemOutDecorator.initialise();
65          BusinessObject bo = new BusinessObject();
66          bo.createVehicles();
67          ArrayList JavaDoc expected = getExpectedWarnings();
68          ArrayList JavaDoc actual = out.getWarnings();
69          compareWarnings(expected, actual);
70       }
71       finally
72       {
73          out.kill();
74       }
75    }
76    
77    private ArrayList JavaDoc getExpectedWarnings()
78    {
79       //Get rid of all whitespace within strings to make them easier to compare
80
ArrayList JavaDoc list = new ArrayList JavaDoc();
81         list.add("WARNING:declare-warningcondition'(call(*org.jboss.test.aop.declare.businesslayer.*->*(..))ORcall(org.jboss.test.aop.declare.businesslayer.*->new(..)))ANDwithin(org.jboss.test.aop.declare.datalayer.*)'wasbrokenforconstructorcall:org.jboss.test.aop.declare.datalayer.Truck.new(Ljava/lang/String;)Vcallsorg.jboss.test.aop.declare.businesslayer.BusinessObject.new()VThisisanexpectedwarning:Cannotcallbusinesslayerfromdatalayer.");
82         list.add("WARNING:declare-warningcondition'(call(*org.jboss.test.aop.declare.businesslayer.*->*(..))ORcall(org.jboss.test.aop.declare.businesslayer.*->new(..)))ANDwithin(org.jboss.test.aop.declare.datalayer.*)'wasbrokenformethodcall:org.jboss.test.aop.declare.datalayer.Truck.new(Ljava/lang/String;)Vcallsorg.jboss.test.aop.declare.businesslayer.BusinessObject.someMethod()VThisisanexpectedwarning:Cannotcallbusinesslayerfromdatalayer.");
83       list.add("WARNING:declare-warningcondition'(call(*org.jboss.test.aop.declare.businesslayer.*->*(..))ORcall(org.jboss.test.aop.declare.businesslayer.*->new(..)))ANDwithin(org.jboss.test.aop.declare.datalayer.*)'wasbrokenforconstructorcall:org.jboss.test.aop.declare.datalayer.Car.badMethod()Vcallsorg.jboss.test.aop.declare.businesslayer.BusinessObject.new()VThisisanexpectedwarning:Cannotcallbusinesslayerfromdatalayer.");
84         list.add("WARNING:declare-warningcondition'(call(*org.jboss.test.aop.declare.businesslayer.*->*(..))ORcall(org.jboss.test.aop.declare.businesslayer.*->new(..)))ANDwithin(org.jboss.test.aop.declare.datalayer.*)'wasbrokenformethodcall:org.jboss.test.aop.declare.datalayer.Car.badMethod()Vcallsorg.jboss.test.aop.declare.businesslayer.BusinessObject.someMethod()VThisisanexpectedwarning:Cannotcallbusinesslayerfromdatalayer.");
85         
86       list.add("WARNING:declare-warningcondition'class($instanceof{org.jboss.test.aop.declare.datalayer.Vehicle})AND!has(*->new(java.lang.String))'wasbrokenforclassorg.jboss.test.aop.declare.datalayer.CarThisisanexpectedwarning:CARshouldbementionedinmsg");
87         list.add("WARNING:declare-warningcondition'!class(org.jboss.test.aop.declare.datalayer.Vehicle)ANDclass($instanceof{org.jboss.test.aop.declare.datalayer.Vehicle})AND!hasfield(org.jboss.test.aop.declare.Logger*->logger)'wasbrokenforclassorg.jboss.test.aop.declare.datalayer.FourWheelerThisisanexpectedwarning:FOURWHEELERshouldbementionedinmsg");
88         list.add("WARNING:declare-warningcondition'class($instanceof{org.jboss.test.aop.declare.datalayer.Vehicle})AND!has(publicboolean*->accept(org.jboss.test.aop.declare.datalayer.MyVisitor))'wasbrokenforclassorg.jboss.test.aop.declare.datalayer.TruckThisisanexpectedwarning:TRUCKshouldbementionedinmsg");
89         
90         list.add("WARNING:declare-warningcondition'call(*org.jboss.test.aop.declare.businesslayer.*->*(..))ANDwithincode(*org.jboss.test.aop.declare.datalayer.Car->badMethod())'wasbrokenformethodcall:org.jboss.test.aop.declare.datalayer.Car.badMethod()Vcallsorg.jboss.test.aop.declare.businesslayer.BusinessObject.someMethod()VThisisanexpectedwarning:CannotcallbusinesslayerfromCar.badMethod()");
91       
92       return list;
93    }
94
95    private void compareWarnings(ArrayList JavaDoc expected, ArrayList JavaDoc actual)
96    {
97       for (Iterator JavaDoc ex = expected.iterator() ; ex.hasNext() ; )
98       {
99          String JavaDoc want = (String JavaDoc)ex.next();
100          for (Iterator JavaDoc ac = actual.iterator() ; ac.hasNext() ; )
101          {
102             String JavaDoc have = (String JavaDoc)ac.next();
103             if (have.equals(want))
104             {
105                ex.remove();
106                ac.remove();
107             }
108          }
109       }
110       
111       if (actual.size() > 0 || expected.size() > 0)
112       {
113          StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114          if (actual.size() > 0)
115          {
116             sb.append("These warnings were raised but not expected\n");
117             for (Iterator JavaDoc it = actual.iterator() ; it.hasNext() ; )
118             {
119                sb.append((String JavaDoc)it.next() + "\n");
120             }
121          }
122          if (expected.size() > 0)
123          {
124             sb.append("These warnings were expected but not raised\n");
125             for (Iterator JavaDoc it = expected.iterator() ; it.hasNext() ; )
126             {
127                sb.append((String JavaDoc)it.next() + "\n");
128             }
129          }
130          
131          throw new RuntimeException JavaDoc(sb.toString());
132       }
133       
134    }
135 }
136
Popular Tags