d2c052715e7089f8a4a7a2d0a3b9584fe47aedc7
[utils] / system / general / src / test / java / org / wamblee / system / core / Environment.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.core;
17
18 import javax.sql.DataSource;
19
20 import org.wamblee.system.core.AbstractComponent;
21 import org.wamblee.system.core.DefaultProvidedInterface;
22 import org.wamblee.system.core.ProvidedInterface;
23 import org.wamblee.system.core.RequiredInterface;
24 import org.wamblee.test.EventTracker;
25
26 public class Environment extends AbstractComponent<Object> {
27
28         private static final ProvidedInterface[] provided(String aPrefix) {
29                 return new ProvidedInterface[] {
30                                 new DefaultProvidedInterface(aPrefix + "datasource", String.class),
31                                 new DefaultProvidedInterface(aPrefix + "integer", Integer.class) };
32         }
33
34         private EventTracker<String> _tracker;
35         private double _random;
36
37         public Environment() {
38                 this("environment");
39         }
40         
41         public Environment(String aName) {
42        this(aName, "");
43     }
44         
45         public Environment(String aName, String aPrefix) {
46         super(aName, provided(aPrefix), new RequiredInterface[0]);
47         _random = Math.random();
48     }
49
50
51
52         public Environment(EventTracker aTracker) {
53                 this();
54                 _tracker = aTracker;
55         }
56
57         public Integer getInteger() {
58                 return 2;
59         }
60
61         public String getString() {
62                 return getName() + ".hello";
63         }
64
65         @Override
66         protected Object doStart(Scope aScope) {
67                 addInterface(getProvidedInterfaces().get(0), getString(), aScope);
68                 addInterface(getProvidedInterfaces().get(1), getInteger(), aScope);
69                 track("start." + getName());
70                 return _random;
71         }
72
73         @Override
74         protected void doStop(Object aRuntime) {
75                 track("stop." + getName());
76                 if (_random != (Double) aRuntime) {
77                         throw new IllegalArgumentException("Wrong runtime: expected "
78                                         + _random + " but got " + aRuntime);
79                 }
80         }
81
82         private void track(String aString) {
83                 if (_tracker == null) {
84                         return;
85                 }
86                 _tracker.eventOccurred(aString);
87         }
88 }