6a39b645688b3f9fbaeb0e94a63d8d8390586593
[utils] / system / general / src / test / java / org / wamblee / system / core / Environment.java
1 /*
2  * Copyright 2005-2010 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 org.wamblee.system.core.AbstractComponent;
19 import org.wamblee.system.core.DefaultProvidedInterface;
20 import org.wamblee.system.core.ProvidedInterface;
21 import org.wamblee.system.core.RequiredInterface;
22
23 import org.wamblee.test.EventTracker;
24
25 import javax.sql.DataSource;
26
27 /**
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class Environment extends AbstractComponent<Object> {
33     private static int COUNT = 0;
34
35     private EventTracker<String> tracker;
36
37     private double random;
38
39     private int integer;
40
41     /**
42      * Creates a new Environment object.
43      */
44     public Environment() {
45         this("environment");
46     }
47
48     /**
49      * Creates a new Environment object.
50      * 
51      */
52     public Environment(String aName) {
53         this(aName, "");
54     }
55
56     /**
57      * Creates a new Environment object.
58      * 
59      */
60     public Environment(String aName, String aPrefix) {
61         super(aName, provided(aPrefix), new RequiredInterface[0]);
62         random = Math.random();
63         integer = COUNT++;
64     }
65
66     /**
67      * Creates a new Environment object.
68      * 
69      */
70     public Environment(EventTracker aTracker) {
71         this();
72         tracker = aTracker;
73     }
74
75     private static final ProvidedInterface[] provided(String aPrefix) {
76         return new ProvidedInterface[] {
77             new DefaultProvidedInterface(aPrefix + "datasource", String.class),
78             new DefaultProvidedInterface(aPrefix + "integer", Integer.class) };
79     }
80
81     public Integer getInteger() {
82         return integer;
83     }
84
85     public String getString() {
86         return getName() + ".hello";
87     }
88
89     @Override
90     protected Object doStart(Scope aScope) {
91         addInterface(getProvidedInterfaces().get(0), getString(), aScope);
92         addInterface(getProvidedInterfaces().get(1), getInteger(), aScope);
93         track("start." + getName());
94
95         return random;
96     }
97
98     @Override
99     protected void doStop(Object aRuntime) {
100         track("stop." + getName());
101
102         if (random != (Double) aRuntime) {
103             throw new IllegalArgumentException("Wrong runtime: expected " +
104                 random + " but got " + aRuntime);
105         }
106     }
107
108     private void track(String aString) {
109         if (tracker == null) {
110             return;
111         }
112
113         tracker.eventOccurred(aString);
114     }
115 }