c202d1ab23d317e83701bd8ba71d90e555466810
[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 static int COUNT = 0; 
35
36         private EventTracker<String> tracker;
37         private double random;
38         private int integer; 
39
40         public Environment() {
41                 this("environment");
42         }
43         
44         public Environment(String aName) {
45        this(aName, "");
46     }
47         
48         public Environment(String aName, String aPrefix) {
49         super(aName, provided(aPrefix), new RequiredInterface[0]);
50         random = Math.random();
51         integer = COUNT++;
52     }
53
54
55
56         public Environment(EventTracker aTracker) {
57                 this();
58                 tracker = aTracker;
59         }
60
61         public Integer getInteger() {
62                 return integer;
63         }
64
65         public String getString() {
66                 return getName() + ".hello";
67         }
68
69         @Override
70         protected Object doStart(Scope aScope) {
71                 addInterface(getProvidedInterfaces().get(0), getString(), aScope);
72                 addInterface(getProvidedInterfaces().get(1), getInteger(), aScope);
73                 track("start." + getName());
74                 return random;
75         }
76
77         @Override
78         protected void doStop(Object aRuntime) {
79                 track("stop." + getName());
80                 if (random != (Double) aRuntime) {
81                         throw new IllegalArgumentException("Wrong runtime: expected "
82                                         + random + " but got " + aRuntime);
83                 }
84         }
85
86         private void track(String aString) {
87                 if (tracker == null) {
88                         return;
89                 }
90                 tracker.eventOccurred(aString);
91         }
92 }