(no commit message)
[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 java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.wamblee.system.core.AbstractComponent;
23 import org.wamblee.system.core.DefaultRequiredInterface;
24 import org.wamblee.system.core.ProvidedInterface;
25 import org.wamblee.system.core.RequiredInterface;
26 import org.wamblee.system.core.Scope;
27 import org.wamblee.test.EventTracker;
28
29 public class Application extends AbstractComponent<Object> {
30     public static RequiredInterface[] required(boolean aOptional,
31             String aPrefix) {
32         return new RequiredInterface[] {
33                 new DefaultRequiredInterface(aPrefix + "string", String.class,
34                         aOptional),
35                 new DefaultRequiredInterface(aPrefix + "integer",
36                         Integer.class, aOptional) };
37     }
38
39     public static RequiredInterface[] required(boolean aOptional) {
40         return required(aOptional, "");
41     }
42
43     private EventTracker<String> _tracker;
44     private String _string;
45     private Integer _integer;
46     private double _random;
47
48     public Application() {
49         this("application");
50     }
51
52     public Application(String aName) {
53         this(aName, "");
54     }
55
56     public Application(String aName, String aPrefix) {
57         super(aName, new ProvidedInterface[0], required(false,
58                 aPrefix));
59         _random = Math.random();
60     }
61
62     public Application(boolean aIsOptinal) {
63         super("application", new ProvidedInterface[0], required(true, ""));
64     }
65
66     public Application(EventTracker<String> aTracker) {
67         this();
68         _tracker = aTracker;
69     }
70
71     @Override
72     public Object doStart(Scope aScope) {
73         track("start." + getName());
74         _string = aScope.getInterfaceImplementation(getRequiredInterfaces()
75                 .get(0).getProvider(), String.class);
76         _integer = aScope.getInterfaceImplementation(getRequiredInterfaces()
77                 .get(1).getProvider(), Integer.class);
78         return _random;
79     }
80
81     public String getString() {
82         return _string;
83     }
84
85     public Integer getInteger() {
86         return _integer;
87     }
88
89     @Override
90     public void doStop(Object aRuntime) {
91         track("stop." + getName());
92         if (_random != (Double) aRuntime) {
93             throw new IllegalArgumentException("Wrong runtime: expected "
94                     + _random + " but got " + aRuntime);
95         }
96     }
97
98     private void track(String aString) {
99         if (_tracker == null) {
100             return;
101         }
102         _tracker.eventOccurred(aString);
103     }
104 }