KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > ws > JBossWSTestSetup


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.ws;
23
24 import junit.extensions.TestSetup;
25 import junit.framework.TestSuite;
26
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.net.URL JavaDoc;
32
33 /**
34  * A test setup that deploys/undeploys archives
35  *
36  * @author Thomas.Diesler@jboss.org
37  * @since 14-Oct-2004
38  */

39 public class JBossWSTestSetup extends TestSetup
40 {
41    private JBossWSTestHelper delegate = new JBossWSTestHelper();
42    private String JavaDoc[] archives;
43
44    protected JBossWSTestSetup(Class JavaDoc testClass, String JavaDoc archiveList)
45    {
46       super(new TestSuite(testClass));
47
48       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(archiveList, ", ");
49       archives = new String JavaDoc[st.countTokens()];
50
51       for (int i = 0; i < archives.length; i++)
52          archives[i] = st.nextToken();
53    }
54
55    public static JBossWSTestSetup newTestSetup(Class JavaDoc testClass, String JavaDoc archiveList)
56    {
57       return new JBossWSTestSetup(testClass, archiveList);
58    }
59
60    protected void setUp() throws Exception JavaDoc
61    {
62       List JavaDoc clientJars = new ArrayList JavaDoc();
63       for (int i = 0; i < archives.length; i++)
64       {
65          String JavaDoc archive = archives[i];
66          boolean isJ2EEClient = archive.endsWith("-client.jar");
67          if (delegate.isTargetServerJBoss() || isJ2EEClient == false)
68          {
69             try
70             {
71                delegate.deploy(archive);
72             }
73             catch (Exception JavaDoc ex)
74             {
75                ex.printStackTrace();
76                delegate.undeploy(archive);
77             }
78          }
79          if (isJ2EEClient)
80          {
81             URL JavaDoc archiveURL = delegate.getArchiveURL(archive);
82             clientJars.add(archiveURL);
83          }
84       }
85
86       // add the client jars to the classloader
87
if( !clientJars.isEmpty() )
88       {
89          ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
90          URL JavaDoc[] urls = new URL JavaDoc[clientJars.size()];
91          for(int i=0; i<clientJars.size(); i++)
92          {
93             urls[i] = (URL JavaDoc)clientJars.get(i);
94          }
95          URLClassLoader JavaDoc cl = new URLClassLoader JavaDoc(urls, parent);
96          Thread.currentThread().setContextClassLoader(cl);
97       }
98    }
99
100    protected void tearDown() throws Exception JavaDoc
101    {
102       for (int i = 0; i < archives.length; i++)
103       {
104          String JavaDoc archive = archives[archives.length - i - 1];
105          boolean isJ2EEClient = archive.endsWith("-client.jar");
106          if (delegate.isTargetServerJBoss() || isJ2EEClient == false)
107          {
108             delegate.undeploy(archive);
109          }
110       }
111    }
112 }
113
Popular Tags