Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ReflectionPermissionModule extends PermissionModule {
public ReflectionPermissionModule(Main main, SecurityManager securityManager) {
super(main, securityManager);
// Security package
//Leander: I don't think this is a good idea
forbiddenReflections = new ArrayList<>();
forbiddenReflections.add(AudioPermissionModule.class);
forbiddenReflections.add(FilePermissionModule.class);
Expand Down
98 changes: 98 additions & 0 deletions src/test/java/classLoaderUtil/DifferentClassLoaderUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package classLoaderUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.function.Supplier;

/**
* @author LeanderK
* @version 1.0
*/
public class DifferentClassLoaderUtils {
/**
* do not use this method! It does nothing....but i don't want to mess with access checks since this class
* can be used for testing reflection.
* @param supplier the supplier
* @param <A> the return type
* @return returns the result from the supplier
*/
public <A> A doMethod(Supplier<A> supplier) {
return supplier.get();
}

public <A> SupplierExcpts<A> doMethodWithDiffClassloader(Supplier<A> supplier) throws Exception{
URL url = this.getClass().getClassLoader().getResource(this.getClass().getName());
URL[] urls = {url};
URLClassLoader classLoader = new URLClassLoader(urls, this.getClass().getClassLoader()) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.findClass(name);
}
};
Class<?> aClass = classLoader.loadClass(this.getClass().getName());
Object classObj = aClass.newInstance();
Method doMethod = aClass.getMethod("doMethod", Supplier.class);
Object[] param = {supplier};
return () -> {
Object result = doMethod.invoke(classObj, param);
return (A) result;
};
}

public <A> SupplierExcpts<A> doMethodDiffClassloader(Class<?> clazz, String methodName, Object param) throws Exception{
URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
URL[] urls = {url};
URLClassLoader classLoader = new URLClassLoader(urls, clazz.getClassLoader()) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.equals(clazz.getName())) {
return super.findClass(name);
} else {
return super.loadClass(name);
}
}
};
Class<?> aClass = classLoader.loadClass(clazz.getName());
Object classObj = aClass.getDeclaredConstructors()[0].newInstance();
Method doMethod;
if (param == null) {
doMethod = aClass.getMethod(methodName);
} else {
doMethod = aClass.getMethod(methodName, param.getClass());
}
Object[] params = param == null? null : new Object[] {param};
//no lambda because it uses reflection
return new SupplierExcpts<A>() {
@Override
public A supply() throws IllegalAccessException, InvocationTargetException {
Object result = doMethod.invoke(classObj, params);
return (A) result;
}
};
}

public <A> SupplierExcpts<A> doMethodSameClassloader(Class<?> clazz, String methodName, Object param) throws Exception{
Object classObj = clazz.getDeclaredConstructors()[0].newInstance();
Method doMethod;
if (param == null) {
doMethod = clazz.getMethod(methodName);
} else {
doMethod = clazz.getMethod(methodName, param.getClass());
}
Object[] params = param == null? null : new Object[] {param};
//no lambda because it uses reflection
return new SupplierExcpts<A>() {
@Override
public A supply() throws IllegalAccessException, InvocationTargetException {
Object result = doMethod.invoke(classObj, params);
return (A) result;
}
};
}

public interface SupplierExcpts<A> {
A supply() throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package old.org.intellimate.izou.activator;

import old.org.intellimate.izou.testHelper.IzouTest;
import org.intellimate.izou.activator.ActivatorModel;
import org.intellimate.izou.events.EventModel;
import org.intellimate.izou.events.MultipleEventsException;
import org.junit.Test;

import java.util.LinkedList;
import java.util.Optional;
import java.util.concurrent.Future;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ActivatorModelManagerTest extends IzouTest {
// private static final class Lock { }
// private final Object lock = new Lock();
//
// public ActivatorModelManagerTest() {
// super(false, ActivatorModelManagerTest.class.getCanonicalName());
// }
//
// @Test
// public void testAddActivator() throws Exception {
// final boolean[] isWorking = {false};
// Optional<EventModel> event = getEvent(id + 1);
// ActivatorModel activatorModel = new ActivatorModel(getContext()) {
// /**
// * An ID must always be unique.
// * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
// * If you have to implement this interface multiple times, just concatenate unique Strings to
// * .class.getCanonicalName()
// *
// * @return A String containing an ID
// */
// @Override
// public String getID() {
// return "TEST";
// }
//
// @Override
// public void activatorStarts() throws InterruptedException {
// try {
// fireEvent(event.get());
// } catch (MultipleEventsException e) {
// e.printStackTrace();
// }
// }
//
// @Override
// public boolean terminated(Exception e) {
// fail();
// return false;
// }
// };
// LinkedList<String> listenerList = new LinkedList<String>();
// listenerList.add(event.get().getType());
// main.getEventDistributor().registerEventListener(listenerList, event1 -> isWorking[0] = true);
//
// Future<?> future = main.getActivatorManager().addActivator(activatorModel);
//
//
// synchronized (lock) {
// while (!future.isDone())
// {
// lock.wait(10);
// }
// for (int i = 0; i <= 10; i++) {
// lock.wait(10);
// }
// }
// assertTrue(isWorking[0]);
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package old.org.intellimate.izou.activator;

//import intellimate.izouSDK.events.EventImpl;
import old.org.intellimate.izou.testHelper.IzouTest;
import org.intellimate.izou.activator.ActivatorModel;
import org.intellimate.izou.events.EventModel;
import org.intellimate.izou.identification.Identification;
import org.intellimate.izou.identification.IdentificationManager;
import org.intellimate.izou.system.Context;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class ActivatorModelTest extends IzouTest {
// static ActivatorModel activatorModel;
// Identification id;
//
// public ActivatorModelTest() {
// super(false, ActivatorModelTest.class.getCanonicalName());
// Context context = getContext();
// activatorModel = new ActivatorModel(getContext()) {
// @Override
// public String getID() {
// return "unbelievable activator id";
// }
//
// @Override
// public void activatorStarts() throws InterruptedException {}
//
// @Override
// public boolean terminated(Exception e) {return false;}
// };
// main.getActivatorManager().addActivator(activatorModel);
// id = IdentificationManager.getInstance().getIdentification(activatorModel).get();
// }
//
// @Test
// public void testFireEvent() throws Exception {
// EventModel event2 = EventImpl.createEvent(ActivatorModelTest.class.getCanonicalName() + 2, id).get();
// final boolean[] isWorking = {false};
// main.getEventDistributor().registerEventListener(event2, id -> isWorking[0] = true);
// activatorModel.fireEvent(event2);
// waitForMultith(event2);
// assertTrue(isWorking[0]);
// }
//

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package old.org.intellimate.izou.addon;

import junit.framework.TestCase;

public class PropertiesManagerTest extends TestCase {
//
// public void testRegisterProperty() throws Exception {
// LinkedList<AddOnModel> addOns = new LinkedList<>();
// TestAddOn testAddOn = new TestAddOn("TestID");
// addOns.add(testAddOn);
// Main main = new Main(addOns, false, true);
// //for(;;){}
// //assertTrue(Files.exists(Paths.get("." + File.separator + "properties" + File.separator + "TestID.properties")));
// assertTrue(true);
// }
//
// public void testRun() throws Exception {
// }
//
// @SuppressWarnings("SameParameterValue")
// private class TestAddOn extends AddOnImpl {
// /**
// * the default constructor for AddOns
// *
// * @param addOnID the ID of the Plugin in the form: package.class
// */
// public TestAddOn(String addOnID) {
// super(addOnID);
// }
//
// @Override
// public void prepare() {
//
// }
//
// @Override
// public ActivatorModel[] registerActivator() {
// return new ActivatorModel[0];
// }
//
// @Override
// public ContentGenerator[] registerContentGenerator() {
// return new ContentGenerator[0];
// }
//
// @Override
// public EventsControllerModel[] registerEventController() {
// return new EventsControllerModel[0];
// }
//
// @Override
// public OutputPluginModel[] registerOutputPlugin() {
// return new OutputPluginModel[0];
// }
//
// @Override
// public OutputExtensionModel[] registerOutputExtension() {
// return new OutputExtensionModel[0];
// }
//
// /**
// * An ID must always be unique.
// * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
// * If you have to implement this interface multiple times, just concatenate unique Strings to
// * .class.getCanonicalName()
// *
// * @return A String containing an ID
// */
// @Override
// public String getID() {
// return "POJAO";
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package old.org.intellimate.izou.events;

//import intellimate.izouSDK.events.EventImpl;
import old.org.intellimate.izou.testHelper.IzouTest;
import org.intellimate.izou.events.EventModel;
import org.intellimate.izou.events.EventsControllerModel;
import org.intellimate.izou.identification.Identification;
import org.intellimate.izou.identification.IdentificationManager;
import org.junit.Test;

public class EventsControllerTest extends IzouTest {

// public EventsControllerTest() {
// super(true, EventsControllerTest.class.getCanonicalName());
// }
//
// @Test
// public void testControlEventDispatcher () {
// boolean[] isWorking = {false, true};
// EventsControllerModel eventsController = new EventsControllerModel() {
// /**
// * this method gets called when the task submitted to the ThreadPool crashes
// *
// * @param e the exception catched
// */
// @Override
// public void exceptionThrown(Exception e) {
// System.out.println(getID() + " crashed");
// }
//
// @Override
// public boolean controlEventDispatcher(EventModel event) {
// isWorking[1] = false;
// return false;
// }
// @Override
// public String getID() {
// return EventsControllerTest.class.getCanonicalName()+1;
// }
// };
// main.getEventDistributor().registerEventsController(eventsController);
// try {
// IdentificationManager.getInstance().registerIdentification(eventsController);
// Identification id = IdentificationManager.getInstance().getIdentification(eventsController).get();
// EventModel event = EventImpl.createEvent(super.id + 1, id).get();
// testListenerFalse(isWorking, event);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
Loading