01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.config.schema.setup;
04:
05: import org.apache.xmlbeans.XmlException;
06:
07: import com.tc.config.schema.repository.ApplicationsRepository;
08: import com.tc.config.schema.repository.MutableBeanRepository;
09: import com.tc.util.Assert;
10:
11: import java.io.File;
12: import java.util.HashSet;
13: import java.util.Set;
14:
15: /**
16: * A {@link ConfigurationCreator} that creates config appropriate for tests only.
17: */
18: public class TestConfigurationCreator implements ConfigurationCreator {
19:
20: private final TestConfigBeanSet beanSet;
21: private boolean loadedFromTrustedSource;
22: private final Set allRepositoriesStoredInto;
23:
24: public TestConfigurationCreator(TestConfigBeanSet beanSet,
25: boolean trustedSource) {
26: Assert.assertNotNull(beanSet);
27: this .beanSet = beanSet;
28: this .loadedFromTrustedSource = trustedSource;
29: this .allRepositoriesStoredInto = new HashSet();
30: }
31:
32: public synchronized MutableBeanRepository[] allRepositoriesStoredInto() {
33: return (MutableBeanRepository[]) this .allRepositoriesStoredInto
34: .toArray(new MutableBeanRepository[this .allRepositoriesStoredInto
35: .size()]);
36: }
37:
38: public boolean loadedFromTrustedSource() {
39: return this .loadedFromTrustedSource;
40: }
41:
42: public String describeSources() {
43: return "Dynamically-generated configuration for tests";
44: }
45:
46: public void createConfigurationIntoRepositories(
47: MutableBeanRepository l1BeanRepository,
48: MutableBeanRepository l2sBeanRepository,
49: MutableBeanRepository systemBeanRepository,
50: ApplicationsRepository applicationsRepository)
51: throws ConfigurationSetupException {
52: try {
53: l1BeanRepository.setBean(this .beanSet.clientBean(),
54: "from test config");
55: addRepository(l1BeanRepository);
56: l1BeanRepository
57: .saveCopyOfBeanInAnticipationOfFutureMutation();
58:
59: l2sBeanRepository.setBean(this .beanSet.serversBean(),
60: "from test config");
61: addRepository(l2sBeanRepository);
62: l2sBeanRepository
63: .saveCopyOfBeanInAnticipationOfFutureMutation();
64:
65: systemBeanRepository.setBean(this .beanSet.systemBean(),
66: "from test config");
67: addRepository(systemBeanRepository);
68: systemBeanRepository
69: .saveCopyOfBeanInAnticipationOfFutureMutation();
70:
71: String[] allNames = this .beanSet.applicationNames();
72: for (int i = 0; i < allNames.length; ++i) {
73: MutableBeanRepository repository = applicationsRepository
74: .repositoryFor(allNames[i]);
75: addRepository(repository);
76: repository.setBean(this .beanSet
77: .applicationBeanFor(allNames[i]),
78: "from test config");
79: repository
80: .saveCopyOfBeanInAnticipationOfFutureMutation();
81: }
82: } catch (XmlException xmle) {
83: throw new ConfigurationSetupException("Unable to set bean",
84: xmle);
85: }
86: }
87:
88: public File directoryConfigurationLoadedFrom() {
89: return null;
90: }
91:
92: private synchronized void addRepository(
93: MutableBeanRepository theRepository) {
94: this.allRepositoriesStoredInto.add(theRepository);
95: }
96:
97: }
|