a55d68de2848a801a5ac866d7f10efb701a382fd
[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 javax.sql.DataSource;
19
20 import org.wamblee.system.core.AbstractComponent;
21 import org.wamblee.system.core.DefaultProvidedInterface;
22 import org.wamblee.system.core.ProvidedInterface;
23 import org.wamblee.system.core.RequiredInterface;
24 import org.wamblee.test.EventTracker;
25
26 public class StringComponent extends AbstractComponent<Object> {
27
28         private static final ProvidedInterface[] provided(String aPrefix) {
29                 return new ProvidedInterface[] {
30                                 new DefaultProvidedInterface(aPrefix + "datasource", String.class) };
31         }
32
33         private EventTracker<String> tracker;
34         private double random;
35
36         public StringComponent() {
37                 this("environment");
38         }
39         
40         public StringComponent(String aName) {
41        this(aName, "");
42     }
43         
44         public StringComponent(String aName, String aPrefix) {
45         super(aName, provided(aPrefix), new RequiredInterface[0]);
46         random = Math.random();
47     }
48
49
50
51         public StringComponent(EventTracker aTracker) {
52                 this();
53                 tracker = aTracker;
54         }
55
56         public Integer getInteger() {
57                 return 2;
58         }
59
60         public String getString() {
61                 return getName() + ".hello";
62         }
63
64         @Override
65         protected Object doStart(Scope aScope) {
66                 addInterface(getProvidedInterfaces().get(0), getString(), aScope);
67                 track("start." + getName());
68                 return random;
69         }
70
71         @Override
72         protected void doStop(Object aRuntime) {
73                 track("stop." + getName());
74                 if (random != (Double) aRuntime) {
75                         throw new IllegalArgumentException("Wrong runtime: expected "
76                                         + random + " but got " + aRuntime);
77                 }
78         }
79
80         private void track(String aString) {
81                 if (tracker == null) {
82                         return;
83                 }
84                 tracker.eventOccurred(aString);
85         }
86 }