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