54766f18dc517e1e950d9f160f48d3e13e2e17c8
[utils] / system / general / src / test / java / org / wamblee / system / core / StringComponent.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 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  * DOCUMENT ME!
30  *
31  * @author $author$
32  * @version $Revision$
33   */
34 public class StringComponent extends AbstractComponent<Object> {
35     /**
36      * DOCUMENT ME!
37      */
38     private EventTracker<String> tracker;
39
40     /**
41      * DOCUMENT ME!
42      */
43     private double random;
44
45     /**
46      * Creates a new StringComponent object.
47      */
48     public StringComponent() {
49         this("environment");
50     }
51
52     /**
53      * Creates a new StringComponent object.
54      *
55      * @param aName DOCUMENT ME!
56      */
57     public StringComponent(String aName) {
58         this(aName, "");
59     }
60
61     /**
62      * Creates a new StringComponent object.
63      *
64      * @param aName DOCUMENT ME!
65      * @param aPrefix DOCUMENT ME!
66      */
67     public StringComponent(String aName, String aPrefix) {
68         super(aName, provided(aPrefix), new RequiredInterface[0]);
69         random = Math.random();
70     }
71
72     /**
73      * Creates a new StringComponent object.
74      *
75      * @param aTracker DOCUMENT ME!
76      */
77     public StringComponent(EventTracker aTracker) {
78         this();
79         tracker = aTracker;
80     }
81
82     /**
83      * DOCUMENT ME!
84      *
85      * @param aPrefix DOCUMENT ME!
86      *
87      * @return DOCUMENT ME!
88      */
89     private static final ProvidedInterface[] provided(String aPrefix) {
90         return new ProvidedInterface[] {
91             new DefaultProvidedInterface(aPrefix + "datasource", String.class)
92         };
93     }
94
95     /**
96      * DOCUMENT ME!
97      *
98      * @return DOCUMENT ME!
99      */
100     public Integer getInteger() {
101         return 2;
102     }
103
104     /**
105      * DOCUMENT ME!
106      *
107      * @return DOCUMENT ME!
108      */
109     public String getString() {
110         return getName() + ".hello";
111     }
112
113     /**
114      * DOCUMENT ME!
115      *
116      * @param aScope DOCUMENT ME!
117      *
118      * @return DOCUMENT ME!
119      */
120     @Override
121     protected Object doStart(Scope aScope) {
122         addInterface(getProvidedInterfaces().get(0), getString(), aScope);
123         track("start." + getName());
124
125         return random;
126     }
127
128     /**
129      * DOCUMENT ME!
130      *
131      * @param aRuntime DOCUMENT ME!
132      */
133     @Override
134     protected void doStop(Object aRuntime) {
135         track("stop." + getName());
136
137         if (random != (Double) aRuntime) {
138             throw new IllegalArgumentException("Wrong runtime: expected "
139                 + random + " but got " + aRuntime);
140         }
141     }
142
143     /**
144      * DOCUMENT ME!
145      *
146      * @param aString DOCUMENT ME!
147      */
148     private void track(String aString) {
149         if (tracker == null) {
150             return;
151         }
152
153         tracker.eventOccurred(aString);
154     }
155 }