forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientUserCodeDeploymentExceptionTest.java
152 lines (128 loc) · 6.44 KB
/
ClientUserCodeDeploymentExceptionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.client.usercodedeployment;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientUserCodeDeploymentConfig;
import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.config.Config;
import com.hazelcast.core.HazelcastException;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.internal.util.FilteringClassLoader;
import com.hazelcast.map.IMap;
import com.hazelcast.nio.serialization.HazelcastSerializationException;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import usercodedeployment.IncrementingEntryProcessor;
import java.io.FileNotFoundException;
import static java.util.Collections.singletonList;
@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
public class ClientUserCodeDeploymentExceptionTest extends HazelcastTestSupport {
private TestHazelcastFactory factory = new TestHazelcastFactory();
@After
public void tearDown() throws Exception {
factory.terminateAll();
}
@Test(expected = HazelcastSerializationException.class)
public void testUserCodeDeploymentIsDisabledByDefaultOnClient() {
// this test also validate the EP is filtered locally and has to be loaded from the other member
ClientConfig clientConfig = new ClientConfig();
Config config = createNodeConfig();
config.getUserCodeDeploymentConfig().setEnabled(true);
IncrementingEntryProcessor incrementingEntryProcessor = new IncrementingEntryProcessor();
factory.newHazelcastInstance(config);
HazelcastInstance client = factory.newHazelcastClient(clientConfig);
IMap<Integer, Integer> map = client.getMap(randomName());
map.executeOnEntries(incrementingEntryProcessor);
}
private Config createNodeConfig() {
Config i2Config = new Config();
FilteringClassLoader filteringCL = new FilteringClassLoader(singletonList("usercodedeployment"), null);
i2Config.setClassLoader(filteringCL);
return i2Config;
}
private ClientConfig createClientConfig() {
ClientConfig config = new ClientConfig();
ClientUserCodeDeploymentConfig clientUserCodeDeploymentConfig = new ClientUserCodeDeploymentConfig();
clientUserCodeDeploymentConfig.addClass("usercodedeployment.IncrementingEntryProcessor");
config.setUserCodeDeploymentConfig(clientUserCodeDeploymentConfig.setEnabled(true));
return config;
}
@Test(expected = IllegalStateException.class)
public void testUserCodeDeployment_serverIsNotEnabled() {
ClientConfig clientConfig = createClientConfig();
clientConfig.getUserCodeDeploymentConfig().setEnabled(true);
Config config = createNodeConfig();
factory.newHazelcastInstance(config);
factory.newHazelcastClient(clientConfig);
}
/**
* The two JARs {@code IncrementingEntryProcessor.jar} and {@code IncrementingEntryProcessorConflicting.jar}
* contain the same class {@link IncrementingEntryProcessor} with different implementations
*/
@Test(expected = IllegalStateException.class)
public void testClientsWithConflictingClassRepresentations() {
Config config = createNodeConfig();
config.getUserCodeDeploymentConfig().setEnabled(true);
factory.newHazelcastInstance(config);
ClientUserCodeDeploymentConfig clientUserCodeDeploymentConfig1 = new ClientUserCodeDeploymentConfig()
.addJar("IncrementingEntryProcessor.jar").setEnabled(true);
ClientConfig clientConfig1 = new ClientConfig()
.setUserCodeDeploymentConfig(clientUserCodeDeploymentConfig1);
factory.newHazelcastClient(clientConfig1);
ClientUserCodeDeploymentConfig clientUserCodeDeploymentConfig2 = new ClientUserCodeDeploymentConfig()
.addJar("IncrementingEntryProcessorConflicting.jar").setEnabled(true);
ClientConfig clientConfig2 = new ClientConfig()
.setUserCodeDeploymentConfig(clientUserCodeDeploymentConfig2);
factory.newHazelcastClient(clientConfig2);
}
@Test(expected = ClassNotFoundException.class)
public void testClientsWith_wrongClassName() throws Throwable {
Config config = createNodeConfig();
config.getUserCodeDeploymentConfig().setEnabled(true);
factory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
ClientUserCodeDeploymentConfig clientUserCodeDeploymentConfig = new ClientUserCodeDeploymentConfig();
clientUserCodeDeploymentConfig.addClass("NonExisting.class").setEnabled(true);
clientConfig.setUserCodeDeploymentConfig(clientUserCodeDeploymentConfig);
try {
factory.newHazelcastClient(clientConfig);
} catch (HazelcastException e) {
throw e.getCause();
}
}
@Test(expected = FileNotFoundException.class)
public void testClientsWith_wrongJarPath() throws Throwable {
Config config = createNodeConfig();
config.getUserCodeDeploymentConfig().setEnabled(true);
factory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
ClientUserCodeDeploymentConfig clientUserCodeDeploymentConfig = new ClientUserCodeDeploymentConfig();
clientUserCodeDeploymentConfig.addJar("NonExisting.jar").setEnabled(true);
clientConfig.setUserCodeDeploymentConfig(clientUserCodeDeploymentConfig);
try {
factory.newHazelcastClient(clientConfig);
} catch (HazelcastException e) {
throw e.getCause();
}
}
}