(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / container / Application.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.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  * @author $author$
29  * @version $Revision$
30  */
31 public class Application extends AbstractComponent<Object> {
32     private EventTracker<String> tracker;
33
34     private String string;
35
36     private Integer integer;
37
38     private double random;
39
40     /**
41      * Creates a new Application object.
42      */
43     public Application() {
44         this("application");
45     }
46
47     /**
48      * Creates a new Application object.
49      * 
50      */
51     public Application(String aName) {
52         this(aName, "");
53     }
54
55     /**
56      * Creates a new Application object.
57      * 
58      */
59     public Application(String aName, String aPrefix) {
60         super(aName, new ProvidedInterface[0], required(false, aPrefix));
61         random = Math.random();
62     }
63
64     /**
65      * Creates a new Application object.
66      * 
67      */
68     public Application(boolean aIsOptinal) {
69         super("application", new ProvidedInterface[0], required(true, ""));
70     }
71
72     /**
73      * Creates a new Application object.
74      * 
75      */
76     public Application(EventTracker<String> aTracker) {
77         this();
78         tracker = aTracker;
79     }
80
81     public static RequiredInterface[] required(boolean aOptional, String aPrefix) {
82         return new RequiredInterface[] {
83             new DefaultRequiredInterface(aPrefix + "string", String.class,
84                 aOptional),
85             new DefaultRequiredInterface(aPrefix + "integer", Integer.class,
86                 aOptional) };
87     }
88
89     public static RequiredInterface[] required(boolean aOptional) {
90         return required(aOptional, "");
91     }
92
93     @Override
94     public Object doStart(Scope aScope) {
95         track("start." + getName());
96         string = aScope.getInterfaceImplementation(getRequiredInterfaces().get(
97             0).getProvider(), String.class);
98         integer = aScope.getInterfaceImplementation(getRequiredInterfaces()
99             .get(1).getProvider(), Integer.class);
100
101         return random;
102     }
103
104     public String getString() {
105         return string;
106     }
107
108     public Integer getInteger() {
109         return integer;
110     }
111
112     @Override
113     public void doStop(Object aRuntime) {
114         track("stop." + getName());
115
116         if (random != (Double) aRuntime) {
117             throw new IllegalArgumentException("Wrong runtime: expected " +
118                 random + " but got " + aRuntime);
119         }
120     }
121
122     private void track(String aString) {
123         if (tracker == null) {
124             return;
125         }
126
127         tracker.eventOccurred(aString);
128     }
129 }