9e4a56b4569ee54e49b464ce42c67f713bc1a339
[utils] / system / general / src / test / java / org / wamblee / system / container / Application.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.container;
17
18 import org.wamblee.system.core.AbstractComponent;
19 import org.wamblee.system.core.DefaultRequiredInterface;
20 import org.wamblee.system.core.ProvidedInterface;
21 import org.wamblee.system.core.RequiredInterface;
22 import org.wamblee.system.core.Scope;
23
24 import org.wamblee.test.EventTracker;
25
26
27 /**
28  * DOCUMENT ME!
29  *
30  * @author $author$
31  * @version $Revision$
32   */
33 public class Application extends AbstractComponent<Object> {
34     /**
35      * DOCUMENT ME!
36      */
37     private EventTracker<String> tracker;
38
39     /**
40      * DOCUMENT ME!
41      */
42     private String string;
43
44     /**
45      * DOCUMENT ME!
46      */
47     private Integer integer;
48
49     /**
50      * DOCUMENT ME!
51      */
52     private double random;
53
54     /**
55      * Creates a new Application object.
56      */
57     public Application() {
58         this("application");
59     }
60
61     /**
62      * Creates a new Application object.
63      *
64      * @param aName DOCUMENT ME!
65      */
66     public Application(String aName) {
67         this(aName, "");
68     }
69
70     /**
71      * Creates a new Application object.
72      *
73      * @param aName DOCUMENT ME!
74      * @param aPrefix DOCUMENT ME!
75      */
76     public Application(String aName, String aPrefix) {
77         super(aName, new ProvidedInterface[0], required(false, aPrefix));
78         random = Math.random();
79     }
80
81     /**
82      * Creates a new Application object.
83      *
84      * @param aIsOptinal DOCUMENT ME!
85      */
86     public Application(boolean aIsOptinal) {
87         super("application", new ProvidedInterface[0], required(true, ""));
88     }
89
90     /**
91      * Creates a new Application object.
92      *
93      * @param aTracker DOCUMENT ME!
94      */
95     public Application(EventTracker<String> aTracker) {
96         this();
97         tracker = aTracker;
98     }
99
100     /**
101      * DOCUMENT ME!
102      *
103      * @param aOptional DOCUMENT ME!
104      * @param aPrefix DOCUMENT ME!
105      *
106      * @return DOCUMENT ME!
107      */
108     public static RequiredInterface[] required(boolean aOptional, String aPrefix) {
109         return new RequiredInterface[] {
110             new DefaultRequiredInterface(aPrefix + "string", String.class,
111                 aOptional),
112             new DefaultRequiredInterface(aPrefix + "integer", Integer.class,
113                 aOptional)
114         };
115     }
116
117     /**
118      * DOCUMENT ME!
119      *
120      * @param aOptional DOCUMENT ME!
121      *
122      * @return DOCUMENT ME!
123      */
124     public static RequiredInterface[] required(boolean aOptional) {
125         return required(aOptional, "");
126     }
127
128     /**
129      * DOCUMENT ME!
130      *
131      * @param aScope DOCUMENT ME!
132      *
133      * @return DOCUMENT ME!
134      */
135     @Override
136     public Object doStart(Scope aScope) {
137         track("start." + getName());
138         string      = aScope.getInterfaceImplementation(getRequiredInterfaces()
139                 .get(0).getProvider(), String.class);
140         integer     = aScope.getInterfaceImplementation(getRequiredInterfaces()
141                 .get(1).getProvider(), Integer.class);
142
143         return random;
144     }
145
146     /**
147      * DOCUMENT ME!
148      *
149      * @return DOCUMENT ME!
150      */
151     public String getString() {
152         return string;
153     }
154
155     /**
156      * DOCUMENT ME!
157      *
158      * @return DOCUMENT ME!
159      */
160     public Integer getInteger() {
161         return integer;
162     }
163
164     /**
165      * DOCUMENT ME!
166      *
167      * @param aRuntime DOCUMENT ME!
168      */
169     @Override
170     public void doStop(Object aRuntime) {
171         track("stop." + getName());
172
173         if (random != (Double) aRuntime) {
174             throw new IllegalArgumentException("Wrong runtime: expected "
175                 + random + " but got " + aRuntime);
176         }
177     }
178
179     /**
180      * DOCUMENT ME!
181      *
182      * @param aString DOCUMENT ME!
183      */
184     private void track(String aString) {
185         if (tracker == null) {
186             return;
187         }
188
189         tracker.eventOccurred(aString);
190     }
191 }