commit | author | age
|
5200b3
|
1 |
/* |
JM |
2 |
* Copyright 2013 gitblit.com. |
|
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 com.gitblit.tests.mock; |
|
17 |
|
|
18 |
import java.io.File; |
|
19 |
import java.util.Date; |
a8100b
|
20 |
import java.util.Locale; |
5200b3
|
21 |
import java.util.Map; |
JM |
22 |
import java.util.TimeZone; |
|
23 |
|
|
24 |
import com.gitblit.Constants; |
|
25 |
import com.gitblit.IStoredSettings; |
e24670
|
26 |
import com.gitblit.manager.IManager; |
5200b3
|
27 |
import com.gitblit.manager.IRuntimeManager; |
JM |
28 |
import com.gitblit.models.ServerSettings; |
|
29 |
import com.gitblit.models.ServerStatus; |
|
30 |
import com.gitblit.models.SettingModel; |
fc3a39
|
31 |
import com.gitblit.utils.XssFilter; |
JM |
32 |
import com.gitblit.utils.XssFilter.AllowXssFilter; |
c828cf
|
33 |
import com.google.inject.Injector; |
5200b3
|
34 |
|
JM |
35 |
public class MockRuntimeManager implements IRuntimeManager { |
|
36 |
|
|
37 |
File baseFolder; |
|
38 |
|
|
39 |
IStoredSettings settings; |
|
40 |
|
|
41 |
ServerStatus serverStatus; |
|
42 |
|
|
43 |
ServerSettings serverSettings; |
|
44 |
|
|
45 |
public MockRuntimeManager() { |
|
46 |
this(new MemorySettings()); |
|
47 |
} |
|
48 |
|
|
49 |
public MockRuntimeManager(Map<String, Object> settings) { |
|
50 |
this(new MemorySettings(settings)); |
|
51 |
} |
|
52 |
|
|
53 |
public MockRuntimeManager(IStoredSettings settings) { |
|
54 |
this.settings = settings; |
|
55 |
|
e24670
|
56 |
this.serverStatus = new ServerStatus(); |
5200b3
|
57 |
this.serverStatus.servletContainer = "MockServer"; |
JM |
58 |
|
|
59 |
this.serverSettings = new ServerSettings(); |
|
60 |
} |
|
61 |
|
|
62 |
@Override |
c828cf
|
63 |
public Injector getInjector() { |
JM |
64 |
return null; |
|
65 |
} |
|
66 |
|
|
67 |
@Override |
5200b3
|
68 |
public void setBaseFolder(File folder) { |
JM |
69 |
this.baseFolder = folder; |
|
70 |
} |
|
71 |
|
|
72 |
@Override |
|
73 |
public File getBaseFolder() { |
|
74 |
return baseFolder; |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public TimeZone getTimezone() { |
|
79 |
return TimeZone.getDefault(); |
|
80 |
} |
|
81 |
|
|
82 |
@Override |
a8100b
|
83 |
public Locale getLocale() { |
JM |
84 |
return Locale.getDefault(); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
5200b3
|
88 |
public boolean isDebugMode() { |
JM |
89 |
return true; |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public Date getBootDate() { |
|
94 |
return serverStatus.bootDate; |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public ServerStatus getStatus() { |
|
99 |
// update heap memory status |
|
100 |
serverStatus.heapAllocated = Runtime.getRuntime().totalMemory(); |
|
101 |
serverStatus.heapFree = Runtime.getRuntime().freeMemory(); |
|
102 |
return serverStatus; |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public ServerSettings getSettingsModel() { |
|
107 |
// ensure that the current values are updated in the setting models |
|
108 |
for (String key : settings.getAllKeys(null)) { |
|
109 |
SettingModel setting = serverSettings.get(key); |
|
110 |
if (setting == null) { |
|
111 |
// unreferenced setting, create a setting model |
|
112 |
setting = new SettingModel(); |
|
113 |
setting.name = key; |
|
114 |
serverSettings.add(setting); |
|
115 |
} |
|
116 |
setting.currentValue = settings.getString(key, ""); |
|
117 |
} |
|
118 |
return serverSettings; |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
|
122 |
public File getFileOrFolder(String key, String defaultFileOrFolder) { |
|
123 |
String fileOrFolder = settings.getString(key, defaultFileOrFolder); |
|
124 |
return getFileOrFolder(fileOrFolder); |
|
125 |
} |
|
126 |
|
|
127 |
@Override |
|
128 |
public File getFileOrFolder(String fileOrFolder) { |
|
129 |
return com.gitblit.utils.FileUtils.resolveParameter(Constants.baseFolder$, |
|
130 |
baseFolder, fileOrFolder); |
|
131 |
} |
|
132 |
|
|
133 |
@Override |
|
134 |
public IStoredSettings getSettings() { |
|
135 |
return settings; |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
fc3a39
|
139 |
public XssFilter getXssFilter() { |
JM |
140 |
return new AllowXssFilter(); |
|
141 |
} |
|
142 |
|
|
143 |
@Override |
5200b3
|
144 |
public boolean updateSettings(Map<String, String> updatedSettings) { |
JM |
145 |
return settings.saveSettings(updatedSettings); |
|
146 |
} |
|
147 |
|
e24670
|
148 |
@Override |
JM |
149 |
public IManager stop() { |
|
150 |
return this; |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
269c50
|
154 |
public IRuntimeManager start() { |
e24670
|
155 |
return this; |
JM |
156 |
} |
5200b3
|
157 |
} |