package de.holisticon.util.tracee;

import de.holisticon.util.tracee.spi.TraceeBackendProvider;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.internal.util.collections.Sets;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

import java.lang.ref.SoftReference;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;

/**
 * @author Sven Bunge, Holisticon AG
 */
@RunWith(PowerMockRunner.class)
public class BackendProviderSetTest {
	
	@Test
	public void shouldReturnSize() {
		final Set<TraceeBackendProvider> providers = Sets.newSet(mock(TraceeBackendProvider.class), mock(TraceeBackendProvider.class));
		final BackendProviderSet out = new BackendProviderSet(providers);
		
		assertThat(out.size(), equalTo(2));
	}

	@Test
	public void shouldReturnMyValues() {
		final TraceeBackendProvider mockedBackend1 = mock(TraceeBackendProvider.class);
		final TraceeBackendProvider mockedBackend2 = mock(TraceeBackendProvider.class);
		final Set<TraceeBackendProvider> providers = Sets.newSet(mockedBackend1, mockedBackend2);
		final BackendProviderSet out = new BackendProviderSet(providers);

		assertThat(out, containsInAnyOrder(mockedBackend1, mockedBackend2));
	}
	
	@Test
	public void shouldHandleEmptySet() {
		final Set<TraceeBackendProvider> providers = Sets.newSet();
		final BackendProviderSet out = new BackendProviderSet(providers);

		assertThat(out.size(), equalTo(0));
	}
	
	@Test
	@Ignore
	public void shouldReturnEmptySetIfOneElementGetPurched() {
		final TraceeBackendProvider mockedBackend1 = mock(TraceeBackendProvider.class);
		final TraceeBackendProvider mockedBackend2 = mock(TraceeBackendProvider.class);
		final Set<TraceeBackendProvider> providers = Sets.newSet(mockedBackend1, mockedBackend2);
		PowerMockito.mock(SoftReference.class);
		final BackendProviderSet out = new BackendProviderSet(providers);

		assertThat(out, containsInAnyOrder(mockedBackend1, mockedBackend2));
	}
}

@@ -93,33 +93,8 @@
 er;%0A
-import org.junit.Ignore;%0A
 impo
@@ -221,32 +221,51 @@
 ock.
-api.mockito.PowerMockito
+core.classloader.annotations.PrepareForTest
 ;%0Aim
@@ -314,16 +314,55 @@
 kRunner;
+%0Aimport org.powermock.reflect.Whitebox;
 %0A%0Aimport
@@ -391,16 +391,48 @@
 erence;%0A
+import java.lang.reflect.Field;%0A
 import j
@@ -622,28 +622,130 @@
 org.
-mockito.Mockito.mock
+hamcrest.Matchers.is;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.powermock.api.mockito.PowerMockito.whenNew
 ;%0A%0A/
@@ -820,16 +820,60 @@
 .class)%0A
+@PrepareForTest(%7BBackendProviderSet.class%7D)%0A
 public c
@@ -1864,17 +1864,8 @@
 est%0A
-%09@Ignore%0A
 %09pub
@@ -1909,20 +1909,38 @@
 ment
-GetPurched()
+WasDetached() throws Exception
  %7B%0A%09
@@ -2108,139 +2108,494 @@
 ;%0A%09%09
-final Set%3CTraceeBackendProvider%3E providers = Sets.newSet(mockedBackend1, mockedBackend2);%0A%09%09PowerMockito.mock(SoftReference.class);
+%0A%09%09// Simulate a SoftReference with an empty value. (The powermock-method withArgument(..) don't work here!)%0A%09%09final SoftReference backendRef1 = new SoftReference%3CTraceeBackendProvider%3E(mockedBackend1);%0A%09%09final SoftReference backendRef2 = new SoftReference%3CTraceeBackendProvider%3E(null);%0A%09%09whenNew(SoftReference.class).withAnyArguments().thenReturn(backendRef1, backendRef2);%0A%0A%09%09// Setup Set and ask for size - then we extract the 'valid' field to check that the set is marked as invalid
 %0A%09%09f
@@ -2610,35 +2610,54 @@
 kendProviderSet 
-out
+traceeBackendProviders
  = new BackendPr
@@ -2662,37 +2662,151 @@
 ProviderSet(
-providers
+Sets.newSet(mockedBackend1, mockedBackend2));%0A%09%09final Field validField = Whitebox.getField(BackendProviderSet.class, %22valid%22
 );%0A
-%0A
 %09%09assertThat
@@ -2810,62 +2810,120 @@
 hat(
-out, containsInAnyOrder(mockedBackend1, mockedBackend2
+traceeBackendProviders.size(), equalTo(0));%0A%09%09assertThat(validField.getBoolean(traceeBackendProviders), is(false
 ));%0A

Add test for BackendProviderSet that checks the handling when a SoftReference loose the value due garbage collection.