1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.identityconnectors.test.common;
24
25 import static org.testng.AssertJUnit.assertEquals;
26 import org.testng.annotations.Test;
27 import org.testng.AssertJUnit;
28 import java.io.BufferedWriter;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStreamWriter;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import java.net.URLConnection;
37 import java.net.URLStreamHandler;
38 import java.util.Enumeration;
39 import java.util.Map;
40 import java.util.NoSuchElementException;
41 import java.util.Properties;
42
43 import org.identityconnectors.framework.spi.Configuration;
44 import org.identityconnectors.framework.spi.Connector;
45
46 public class TestHelpersTest {
47
48 @Test
49 public void testLoadGroovyConfigFileIssue393() {
50 Map<?, ?> props = TestHelpers.loadGroovyConfigFile(TestHelpersTest.class.getResource("properties.groovy"));
51 assertEquals(Integer.class, props.get("prop.integerclass"));
52 }
53
54 @Test
55 public void testLoadConnectorProperties() {
56 String oldTestConfig = System.getProperty("testConfig");
57 System.setProperty("testConfig", "myconfig");
58 try {
59 PropertyBag properties1 = TestHelpers.getProperties(DummyConnector.class, new ConfigClassLoader());
60 checkProperties(properties1);
61 PropertyBag properties2 = TestHelpers.getProperties(DummyConnector.class, new ConfigClassLoader());
62 AssertJUnit.assertSame("TestHepers must create same PropertyBag for same connector", properties1, properties2);
63 } finally {
64 if (oldTestConfig == null) {
65 System.getProperties().remove("testConfig");
66 } else {
67 System.setProperty("testConfig", oldTestConfig);
68 }
69 }
70 }
71
72 void checkProperties(PropertyBag bag) {
73 Properties valid = new Properties();
74 valid.setProperty("publickey", "value");
75 valid.setProperty("myconfig.publickey", "value");
76 valid.setProperty("privatekey", "value");
77 valid.setProperty("myconfig.privatekey", "value");
78 valid.setProperty("override1", "bar1");
79 valid.setProperty("override2", "bar2");
80 valid.setProperty("override3", "bar3");
81 assertEquals("Expected test properties not equal", valid, bag.toMap());
82 }
83
84 static class DummyConnector implements Connector {
85 public void dispose() {
86 }
87
88 public Configuration getConfiguration() {
89 return null;
90 }
91
92 public void init(Configuration cfg) {
93 }
94 }
95
96 static class ConfigClassLoader extends ClassLoader {
97 @Override
98 public URL getResource(String name) {
99 String prefix = DummyConnector.class.getName();
100 if ((prefix + "/config/config.groovy").equals(name)) {
101 Properties properties = new Properties();
102 properties.setProperty("publickey", "\"value\"");
103 properties.setProperty("override1", "\"foo1\"");
104 properties.setProperty("override2", "\"foo2\"");
105 properties.setProperty("override3", "\"foo3\"");
106 return map2URL(properties);
107 } else if ((prefix + "/config/myconfig/config.groovy").equals(name)) {
108 Properties properties = new Properties();
109 properties.setProperty("myconfig.publickey", "\"value\"");
110 properties.setProperty("override1", "\"bar1\"");
111 return map2URL(properties);
112 } else if ((prefix + "/config-private/config.groovy").equals(name)) {
113 Properties properties = new Properties();
114 properties.setProperty("privatekey", "\"value\"");
115 properties.setProperty("override2", "\"bar2\"");
116 return map2URL(properties);
117 } else if ((prefix + "/config-private/myconfig/config.groovy").equals(name)) {
118 Properties properties = new Properties();
119 properties.setProperty("myconfig.privatekey", "\"value\"");
120 properties.setProperty("override3", "\"bar3\"");
121 return map2URL(properties);
122 }
123 return null;
124 }
125
126 public Enumeration<URL> getResources(String name) throws IOException {
127 return new SingleURLEnumeration(null);
128 }
129 }
130
131 private static class SingleURLEnumeration implements Enumeration<URL> {
132 private URL url;
133
134 private SingleURLEnumeration(URL url) {
135 super();
136 this.url = url;
137 }
138
139 public boolean hasMoreElements() {
140 return url != null;
141 }
142
143 public URL nextElement() {
144 if (url == null) {
145 throw new NoSuchElementException();
146 }
147 URL ret = url;
148 url = null;
149 return ret;
150 }
151 }
152
153 private static URL map2URL(final Properties properties) {
154 URLStreamHandler handler = new URLStreamHandler() {
155 @Override
156 protected URLConnection openConnection(URL u) throws IOException {
157 return new URLConnection(u) {
158 @Override
159 public void connect() throws IOException {
160 }
161
162 @Override
163 public InputStream getInputStream() throws IOException {
164
165
166 ByteArrayOutputStream baos = new ByteArrayOutputStream();
167 BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(baos));
168 for (Map.Entry<Object, Object> entry : properties.entrySet()) {
169 bfw.append(entry.getKey().toString());
170 bfw.append("=");
171 bfw.append(entry.getValue().toString());
172 bfw.newLine();
173 }
174 bfw.close();
175 return new ByteArrayInputStream(baos.toByteArray());
176 }
177 };
178 }
179
180 };
181 try {
182 return new URL(null, "file:///map", handler);
183 } catch (MalformedURLException e) {
184 throw new IllegalStateException("Invalid url", e);
185 }
186 }
187
188 }