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