Skip to content

Commit 378a0da

Browse files
author
Alex Huang
committed
more unittest support
1 parent c06444c commit 378a0da

8 files changed

Lines changed: 225 additions & 84 deletions

File tree

‎build/build-cloud.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@
589589
<delete dir="${dist.dir}" />
590590
<delete dir="${unittest.dir}"/>
591591
</target>
592+
593+
<target name="compile-all" description="Compile all of the jars" depends="compile-utils, compile-api, compile-core, compile-server"/>
592594

593595
<target name="clean-all" depends="clean" description="Clean all of the generated files, including dependency cache and javadoc">
594596
<delete dir="${target.dir}" />

‎build/developer.xml‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
<dirset dir="${base.dir}/setup/db"/>
215215
</path>
216216

217-
<target name="compile-tests" depends="build-all">
217+
<target name="compile-tests" depends="compile-all">
218218
<mkdir dir="${unittest.dir}"/>
219219
<mkdir dir="${unittest.target.dir}"/>
220220
<mkdir dir="${unittest.jar.dir}"/>
@@ -224,13 +224,17 @@
224224
</target>
225225

226226
<target name="unittest" description="Execute unit tests" depends="compile-tests">
227-
<junit fork="yes" printsummary="true" failureproperty="junit.failure">
227+
<junit fork="true" printsummary="true" showoutput="true" failureproperty="junit.failure">
228228
<!-- N.b. use failureproperty instead of haltonfailure, because if we use
229229
the former, we will get no detailed report about the failure.
230230
If the test fails, the fail element below will still assure that
231231
the Ant run will exit with error status.
232232
-->
233+
<!--bootclasspath refid="test.classpath"/-->
233234
<classpath refid="test.classpath"/>
235+
<permissions>
236+
<grant class="java.security.AllPermission"/>
237+
</permissions>
234238
<jvmarg value="${debug.jvmarg}"/>
235239
<batchtest todir="${unittest.dir}">
236240
<formatter type="plain"/>
@@ -250,12 +254,13 @@
250254
</target>
251255

252256
<target name="test" description="Execute one unit test" depends="compile-tests">
253-
<junit fork="true" printsummary="true" failureproperty="junit.failure">
257+
<junit fork="true" printsummary="true" clonevm="true" failureproperty="junit.failure">
254258
<!-- N.b. use failureproperty instead of haltonfailure, because if we use
255259
the former, we will get no detailed report about the failure.
256260
If the test fails, the fail element below will still assure that
257261
the Ant run will exit with error status.
258262
-->
263+
<bootclasspath refid="test.classpath"/>
259264
<classpath refid="test.classpath"/>
260265
<batchtest todir="${unittest.dir}">
261266
<formatter type="plain"/>

‎server/src/com/cloud/configuration/DefaultComponentLibrary.java‎

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
package com.cloud.configuration;
1919

2020
import java.io.Serializable;
21-
import java.util.ArrayList;
2221
import java.util.HashMap;
23-
import java.util.LinkedHashMap;
2422
import java.util.List;
2523
import java.util.Map;
2624

@@ -128,9 +126,9 @@
128126
import com.cloud.user.dao.UserAccountDaoImpl;
129127
import com.cloud.user.dao.UserDaoImpl;
130128
import com.cloud.user.dao.UserStatisticsDaoImpl;
131-
import com.cloud.utils.Pair;
132129
import com.cloud.utils.component.Adapter;
133130
import com.cloud.utils.component.ComponentLibrary;
131+
import com.cloud.utils.component.ComponentLibraryBase;
134132
import com.cloud.utils.component.ComponentLocator.ComponentInfo;
135133
import com.cloud.utils.component.Manager;
136134
import com.cloud.utils.db.GenericDao;
@@ -148,21 +146,7 @@
148146
import com.cloud.vm.dao.UserVmDetailsDaoImpl;
149147
import com.cloud.vm.dao.VMInstanceDaoImpl;
150148

151-
public class DefaultComponentLibrary implements ComponentLibrary {
152-
153-
protected final Map<String, ComponentInfo<GenericDao<?, ? extends Serializable>>> _daos = new LinkedHashMap<String, ComponentInfo<GenericDao<?, ? extends Serializable>>>();
154-
155-
protected ComponentInfo<? extends GenericDao<?, ? extends Serializable>> addDao(String name, Class<? extends GenericDao<?, ? extends Serializable>> clazz) {
156-
return addDao(name, clazz, new ArrayList<Pair<String, Object>>(), true);
157-
}
158-
159-
protected ComponentInfo<? extends GenericDao<?, ? extends Serializable>> addDao(String name, Class<? extends GenericDao<?, ? extends Serializable>> clazz, List<Pair<String, Object>> params, boolean singleton) {
160-
ComponentInfo<GenericDao<?, ? extends Serializable>> componentInfo = new ComponentInfo<GenericDao<?, ? extends Serializable>>(name, clazz, params, singleton);
161-
for (String key : componentInfo.getKeys()) {
162-
_daos.put(key, componentInfo);
163-
}
164-
return componentInfo;
165-
}
149+
public class DefaultComponentLibrary extends ComponentLibraryBase implements ComponentLibrary {
166150

167151
protected void populateDaos() {
168152
addDao("StackMaidDao", StackMaidDaoImpl.class);
@@ -263,10 +247,7 @@ protected void populateDaos() {
263247
addDao("OvsTunnelAccountDao", OvsTunnelAccountDaoImpl.class);
264248
addDao("StoragePoolWorkDao", StoragePoolWorkDaoImpl.class);
265249
}
266-
267-
Map<String, ComponentInfo<Manager>> _managers = new HashMap<String, ComponentInfo<Manager>>();
268-
Map<String, List<ComponentInfo<Adapter>>> _adapters = new HashMap<String, List<ComponentInfo<Adapter>>>();
269-
250+
270251
@Override
271252
public synchronized Map<String, ComponentInfo<GenericDao<?, ?>>> getDaos() {
272253
if (_daos.size() == 0) {
@@ -275,18 +256,6 @@ protected void populateDaos() {
275256
return _daos;
276257
}
277258

278-
protected ComponentInfo<Manager> addManager(String name, Class<? extends Manager> clazz, List<Pair<String, Object>> params, boolean singleton) {
279-
ComponentInfo<Manager> info = new ComponentInfo<Manager>(name, clazz, params, singleton);
280-
for (String key : info.getKeys()) {
281-
_managers.put(key, info);
282-
}
283-
return info;
284-
}
285-
286-
protected ComponentInfo<Manager> addManager(String name, Class<? extends Manager> clazz) {
287-
return addManager(name, clazz, new ArrayList<Pair<String, Object>>(), true);
288-
}
289-
290259
protected void populateManagers() {
291260
addManager("StackMaidManager", StackMaidManagerImpl.class);
292261
addManager("agent manager", AgentManagerImpl.class);
@@ -328,23 +297,6 @@ protected void populateManagers() {
328297
info.addParameter("consoleproxy.sslEnabled", "true");
329298
}
330299

331-
protected <T> List<ComponentInfo<Adapter>> addAdapterChain(Class<T> interphace, List<Pair<String, Class<? extends T>>> adapters) {
332-
ArrayList<ComponentInfo<Adapter>> lst = new ArrayList<ComponentInfo<Adapter>>(adapters.size());
333-
for (Pair<String, Class<? extends T>> adapter : adapters) {
334-
@SuppressWarnings("unchecked")
335-
Class<? extends Adapter> clazz = (Class<? extends Adapter>)adapter.second();
336-
lst.add(new ComponentInfo<Adapter>(adapter.first(), clazz));
337-
}
338-
_adapters.put(interphace.getName(), lst);
339-
return lst;
340-
}
341-
342-
protected <T> void addOneAdapter(Class<T> interphace, String name, Class<? extends T> adapterClass) {
343-
List<Pair<String, Class<? extends T>>> adapters = new ArrayList<Pair<String, Class<? extends T>>>();
344-
adapters.add(new Pair<String, Class<? extends T>>(name, adapterClass));
345-
addAdapterChain(interphace, adapters);
346-
}
347-
348300
@Override
349301
public synchronized Map<String, ComponentInfo<Manager>> getManagers() {
350302
if (_managers.size() == 0) {
@@ -370,5 +322,4 @@ public synchronized Map<Class<?>, Class<?>> getFactories() {
370322
factories.put(EntityManager.class, EntityManagerImpl.class);
371323
return factories;
372324
}
373-
374325
}

‎server/test/com/cloud/upgrade/dao/VersionDaoImplTest.java‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
public class VersionDaoImplTest extends TestCase {
4343
private static final Logger s_logger = Logger.getLogger(VersionDaoImplTest.class);
4444

45-
/**
46-
* @throws java.lang.Exception
47-
*/
4845
@Override
4946
@Before
5047
public void setUp() throws Exception {
@@ -82,12 +79,7 @@ protected void executeScript(String file) {
8279
throw new RuntimeException("Unable to close DB connection", e);
8380
}
8481
}
85-
86-
8782

88-
/**
89-
* @throws java.lang.Exception
90-
*/
9183
@Override
9284
@After
9385
public void tearDown() throws Exception {
@@ -96,7 +88,7 @@ public void tearDown() throws Exception {
9688
public void test217to22Upgrade() {
9789
s_logger.debug("Finding sample data from 2.1.7");
9890
executeScript("VersionDaoImplTest/2.1.7/2.1.7.sample.sql");
99-
91+
10092
VersionDaoImpl dao = ComponentLocator.inject(VersionDaoImpl.class);
10193

10294
String version = dao.getCurrentVersion();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
3+
*
4+
* This software is licensed under the GNU General Public License v3 or later.
5+
*
6+
* It is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
package com.cloud.utils.component;
19+
20+
import java.io.Serializable;
21+
import java.util.ArrayList;
22+
import java.util.LinkedHashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
import com.cloud.utils.Pair;
27+
import com.cloud.utils.component.ComponentLocator.ComponentInfo;
28+
import com.cloud.utils.db.GenericDao;
29+
30+
public abstract class ComponentLibraryBase implements ComponentLibrary {
31+
32+
protected final Map<String, ComponentInfo<GenericDao<?, ? extends Serializable>>> _daos = new LinkedHashMap<String, ComponentInfo<GenericDao<?, ? extends Serializable>>>();
33+
34+
protected ComponentInfo<? extends GenericDao<?, ? extends Serializable>> addDao(String name, Class<? extends GenericDao<?, ? extends Serializable>> clazz) {
35+
return addDao(name, clazz, new ArrayList<Pair<String, Object>>(), true);
36+
}
37+
38+
protected ComponentInfo<? extends GenericDao<?, ? extends Serializable>> addDao(String name, Class<? extends GenericDao<?, ? extends Serializable>> clazz, List<Pair<String, Object>> params, boolean singleton) {
39+
ComponentInfo<GenericDao<?, ? extends Serializable>> componentInfo = new ComponentInfo<GenericDao<?, ? extends Serializable>>(name, clazz, params, singleton);
40+
for (String key : componentInfo.getKeys()) {
41+
_daos.put(key, componentInfo);
42+
}
43+
return componentInfo;
44+
}
45+
46+
protected Map<String, ComponentInfo<Manager>> _managers = new LinkedHashMap<String, ComponentInfo<Manager>>();
47+
protected Map<String, List<ComponentInfo<Adapter>>> _adapters = new LinkedHashMap<String, List<ComponentInfo<Adapter>>>();
48+
49+
protected ComponentInfo<Manager> addManager(String name, Class<? extends Manager> clazz, List<Pair<String, Object>> params, boolean singleton) {
50+
ComponentInfo<Manager> info = new ComponentInfo<Manager>(name, clazz, params, singleton);
51+
for (String key : info.getKeys()) {
52+
_managers.put(key, info);
53+
}
54+
return info;
55+
}
56+
57+
protected ComponentInfo<Manager> addManager(String name, Class<? extends Manager> clazz) {
58+
return addManager(name, clazz, new ArrayList<Pair<String, Object>>(), true);
59+
}
60+
61+
protected <T> List<ComponentInfo<Adapter>> addAdapterChain(Class<T> interphace, List<Pair<String, Class<? extends T>>> adapters) {
62+
ArrayList<ComponentInfo<Adapter>> lst = new ArrayList<ComponentInfo<Adapter>>(adapters.size());
63+
for (Pair<String, Class<? extends T>> adapter : adapters) {
64+
@SuppressWarnings("unchecked")
65+
Class<? extends Adapter> clazz = (Class<? extends Adapter>)adapter.second();
66+
lst.add(new ComponentInfo<Adapter>(adapter.first(), clazz));
67+
}
68+
_adapters.put(interphace.getName(), lst);
69+
return lst;
70+
}
71+
72+
protected <T> ComponentInfo<Adapter> addOneAdapter(Class<T> interphace, String name, Class<? extends T> adapterClass) {
73+
List<Pair<String, Class<? extends T>>> adapters = new ArrayList<Pair<String, Class<? extends T>>>();
74+
adapters.add(new Pair<String, Class<? extends T>>(name, adapterClass));
75+
return addAdapterChain(interphace, adapters).get(0);
76+
}
77+
}

‎utils/src/com/cloud/utils/component/ComponentLocator.java‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class ComponentLocator implements ComponentLocatorMBean {
9090

9191
protected HashMap<String, Adapters<? extends Adapter>> _adapterMap;
9292
protected HashMap<String, ComponentInfo<Manager>> _managerMap;
93-
protected LinkedHashMap<String, ComponentInfo<GenericDao<?, ?>>> _daoMap;
93+
protected LinkedHashMap<String, ComponentInfo<GenericDao<?, ? extends Serializable>>> _daoMap;
9494
protected String _serverName;
9595
protected Object _component;
9696
protected HashMap<Class<?>, Class<?>> _factories;
@@ -101,6 +101,7 @@ public class ComponentLocator implements ComponentLocatorMBean {
101101

102102
public ComponentLocator(String server) {
103103
_serverName = server;
104+
Runtime.getRuntime().addShutdownHook(new CleanupThread());
104105
}
105106

106107
public String getLocatorName() {
@@ -615,7 +616,7 @@ public static <T> T inject(Class<T> clazz, Object... args) {
615616
}
616617

617618
@Override
618-
public Map<String, List<String>> getAdapters() {
619+
public Map<String, List<String>> getAdapterNames() {
619620
HashMap<String, List<String>> result = new HashMap<String, List<String>>();
620621
for (Map.Entry<String, Adapters<? extends Adapter>> entry : _adapterMap.entrySet()) {
621622
Adapters<? extends Adapter> adapters = entry.getValue();
@@ -632,13 +633,13 @@ public Map<String, List<String>> getAdapters() {
632633

633634
public Map<String, List<String>> getAllAccessibleAdapters() {
634635
Map<String, List<String>> parentResults = new HashMap<String, List<String>>();
635-
Map<String, List<String>> results = getAdapters();
636+
Map<String, List<String>> results = getAdapterNames();
636637
parentResults.putAll(results);
637638
return parentResults;
638639
}
639640

640641
@Override
641-
public Collection<String> getManagers() {
642+
public Collection<String> getManagerNames() {
642643
Collection<String> names = new HashSet<String>();
643644
for (Map.Entry<String, ComponentInfo<Manager>> entry : _managerMap.entrySet()) {
644645
names.add(entry.getValue().name);
@@ -647,7 +648,7 @@ public Collection<String> getManagers() {
647648
}
648649

649650
@Override
650-
public Collection<String> getDaos() {
651+
public Collection<String> getDaoNames() {
651652
Collection<String> names = new HashSet<String>();
652653
for (Map.Entry<String, ComponentInfo<GenericDao<?, ?>>> entry : _daoMap.entrySet()) {
653654
names.add(entry.getValue().name);
@@ -666,6 +667,20 @@ public Adapters<? extends Adapter> getAdapters(String key) {
666667
}
667668
return new Adapters<Adapter>(key, new ArrayList<ComponentInfo<Adapter>>());
668669
}
670+
671+
protected void resetInterceptors(InterceptorLibrary library) {
672+
library.addInterceptors(s_interceptors);
673+
if (s_interceptors.size() > 0) {
674+
s_callbacks = new Callback[s_interceptors.size() + 2];
675+
int i = 0;
676+
s_callbacks[i++] = NoOp.INSTANCE;
677+
s_callbacks[i++] = new InterceptorDispatcher();
678+
for (AnnotationInterceptor<?> interceptor : s_interceptors) {
679+
s_callbacks[i++] = interceptor.getCallback();
680+
}
681+
s_callbackFilter = new InterceptorFilter();
682+
}
683+
}
669684

670685
protected static ComponentLocator getLocatorInternal(String server, boolean setInThreadLocal, String configFileName, String log4jFilename) {
671686
synchronized(s_once) {
@@ -859,18 +874,8 @@ public void startElement(String namespaceURI, String localName, String qName, At
859874
String libraryName = getAttribute(atts, "library");
860875
try {
861876
Class<?> libraryClazz = Class.forName(libraryName);
862-
InterceptorLibrary library = (InterceptorLibrary)libraryClazz.newInstance();
863-
library.addInterceptors(s_interceptors);
864-
if (s_interceptors.size() > 0) {
865-
s_callbacks = new Callback[s_interceptors.size() + 2];
866-
int i = 0;
867-
s_callbacks[i++] = NoOp.INSTANCE;
868-
s_callbacks[i++] = new InterceptorDispatcher();
869-
for (AnnotationInterceptor<?> interceptor : s_interceptors) {
870-
s_callbacks[i++] = interceptor.getCallback();
871-
}
872-
s_callbackFilter = new InterceptorFilter();
873-
}
877+
InterceptorLibrary library = (InterceptorLibrary)libraryClazz.newInstance();
878+
resetInterceptors(library);
874879
} catch (ClassNotFoundException e) {
875880
throw new CloudRuntimeException("Unable to find " + libraryName, e);
876881
} catch (InstantiationException e) {

‎utils/src/com/cloud/utils/component/ComponentLocatorMBean.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public interface ComponentLocatorMBean extends ManagementBean {
2929
/**
3030
* @return the list of adapters accessible by this component locator.
3131
**/
32-
Map<String, List<String>> getAdapters();
32+
Map<String, List<String>> getAdapterNames();
3333

3434
/**
3535
* @return the list of managers accessible by this component locator.
3636
**/
37-
Collection<String> getManagers();
37+
Collection<String> getManagerNames();
3838

3939
/**
4040
* @return the list of DAOs accessible by this component locator.
4141
*/
42-
Collection<String> getDaos();
42+
Collection<String> getDaoNames();
4343

4444
}

0 commit comments

Comments
 (0)