c560d6e6149b5a7a627ee01fb7edc439fd424a9e
[utils] / system / general / src / test / java / org / wamblee / system / core / IntegerComponent.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 IntegerComponent extends AbstractComponent<Object> {
33     private EventTracker<String> tracker;
34
35     private double random;
36
37     /**
38      * Creates a new IntegerComponent object.
39      */
40     public IntegerComponent() {
41         this("environment");
42     }
43
44     /**
45      * Creates a new IntegerComponent object.
46      * 
47      */
48     public IntegerComponent(String aName) {
49         this(aName, "");
50     }
51
52     /**
53      * Creates a new IntegerComponent object.
54      * 
55      */
56     public IntegerComponent(String aName, String aPrefix) {
57         super(aName, provided(aPrefix), new RequiredInterface[0]);
58         random = Math.random();
59     }
60
61     /**
62      * Creates a new IntegerComponent object.
63      * 
64      */
65     public IntegerComponent(EventTracker aTracker) {
66         this();
67         tracker = aTracker;
68     }
69
70     private static final ProvidedInterface[] provided(String aPrefix) {
71         return new ProvidedInterface[] { new DefaultProvidedInterface(aPrefix +
72             "integer", Integer.class) };
73     }
74
75     public Integer getInteger() {
76         return 2;
77     }
78
79     @Override
80     protected Object doStart(Scope aScope) {
81         addInterface(getProvidedInterfaces().get(1), getInteger(), aScope);
82         track("start." + getName());
83
84         return random;
85     }
86
87     @Override
88     protected void doStop(Object aRuntime) {
89         track("stop." + getName());
90
91         if (random != (Double) aRuntime) {
92             throw new IllegalArgumentException("Wrong runtime: expected " +
93                 random + " but got " + aRuntime);
94         }
95     }
96
97     private void track(String aString) {
98         if (tracker == null) {
99             return;
100         }
101
102         tracker.eventOccurred(aString);
103     }
104 }