<commit_before>/**

   Copyright 2010 OpenEngSB Division, Vienna University of Technology

   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 org.openengsb.core;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.annotation.Resource;
import javax.jbi.servicedesc.ServiceEndpoint;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.openengsb.contextcommon.ContextHelperImpl;
import org.openengsb.core.endpoints.OpenEngSBEndpoint;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:../test-classes/SUProperties.xml" })
public class ContextRegistrationTest {
    private ContextHelperImpl contextHelper;

    @Resource
    private HashMap<String, HashMap<String, String>> properties;

    @Before
    public void setUp() {
        contextHelper = Mockito.mock(ContextHelperImpl.class);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testNoRegistering() throws Exception {
        OpenEngSBEndpoint endpoint = new OpenEngSBEndpoint(new OpenEngSBComponent(), Mockito
                .mock(ServiceEndpoint.class));
        setContextHelper(endpoint);
        callMethod(endpoint, "register");
        callMethod(endpoint, "unregister");

        Mockito.verify(contextHelper, Mockito.never()).store(Mockito.anyMap());
    }

    @Test
    public void testSULifecycle() throws Exception {
        OpenEngSBEndpoint endpoint = new OpenEngSBEndpoint(new OpenEngSBComponent(), Mockito
                .mock(ServiceEndpoint.class));
        endpoint.setEndpoint("testendpoint");
        endpoint.setContextProperties(properties);
        setContextHelper(endpoint);
        callMethod(endpoint, "register");

        for (String key : properties.keySet()) {
            Mockito.verify(contextHelper, Mockito.times(1)).setContext(Mockito.eq(key));
            Mockito.verify(contextHelper, Mockito.times(1)).store(
                    Mockito.eq(addSource(properties.get(key), "SU/testendpoint")));
        }

        clearContextHelper(endpoint);
        callMethod(endpoint, "unregister");
<commit_msg>Small test improvement<commit_after>/**

   Copyright 2010 OpenEngSB Division, Vienna University of Technology

   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 org.openengsb.core;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.annotation.Resource;
import javax.jbi.servicedesc.ServiceEndpoint;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.openengsb.contextcommon.ContextHelperImpl;
import org.openengsb.core.endpoints.OpenEngSBEndpoint;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:../test-classes/SUProperties.xml" })
public class ContextRegistrationTest {
    private ContextHelperImpl contextHelper;

    @Resource
    private HashMap<String, HashMap<String, String>> properties;

    @Before
    public void setUp() {
        contextHelper = Mockito.mock(ContextHelperImpl.class);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testNoRegistering() throws Exception {
        OpenEngSBEndpoint endpoint = new OpenEngSBEndpoint(new OpenEngSBComponent(), Mockito
                .mock(ServiceEndpoint.class));
        setContextHelper(endpoint);
        callMethod(endpoint, "register");
        callMethod(endpoint, "unregister");

        Mockito.verify(contextHelper, Mockito.never()).store(Mockito.anyMap());
        Mockito.verify(contextHelper, Mockito.never()).remove(Mockito.anyList());
    }

    @Test
    public void testSULifecycle() throws Exception {
        OpenEngSBEndpoint endpoint = new OpenEngSBEndpoint(new OpenEngSBComponent(), Mockito
                .mock(ServiceEndpoint.class));
        endpoint.setEndpoint("testendpoint");
        endpoint.setContextProperties(properties);
        setContextHelper(endpoint);
        callMethod(endpoint, "register");

        for (String key : properties.keySet()) {
            Mockito.verify(contextHelper, Mockito.times(1)).setContext(Mockito.eq(key));
            Mockito.verify(contextHelper, Mockito.times(1)).store(
                    Mockito.eq(addSource(properties.get(key), "SU/testendpoint")));
        }

        clearContextHelper(endpoint);
        callMethod(endpoint, "unregister");

{'fraction_non_alphanumeric': 0.09486963835155593, 'fraction_numerical': 0.004373423044575274, 'mean_word_length': 3.893827160493827, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680296', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1543, 'n_words': 392}