|
| 1 | +package edu.wpi.grip.core.serialization; |
| 2 | + |
| 3 | +import com.google.common.base.Throwables; |
| 4 | +import com.google.common.eventbus.EventBus; |
| 5 | +import com.google.common.eventbus.Subscribe; |
| 6 | +import com.google.inject.Guice; |
| 7 | +import com.google.inject.Injector; |
| 8 | +import com.google.inject.Key; |
| 9 | +import com.google.inject.TypeLiteral; |
| 10 | +import edu.wpi.grip.core.*; |
| 11 | +import edu.wpi.grip.core.events.OperationAddedEvent; |
| 12 | +import edu.wpi.grip.core.operations.Operations; |
| 13 | +import edu.wpi.grip.core.sources.ImageFileSource; |
| 14 | +import edu.wpi.grip.core.util.MockExceptionWitness; |
| 15 | +import edu.wpi.grip.generated.CVOperations; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import java.io.*; |
| 21 | +import java.net.URL; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import static junit.framework.TestCase.assertEquals; |
| 28 | + |
| 29 | +public class CompatibilityTest { |
| 30 | + |
| 31 | + private Connection.Factory<Object> connectionFactory; |
| 32 | + private ImageFileSource.Factory imageSourceFactory; |
| 33 | + private Step.Factory stepFactory; |
| 34 | + private Pipeline pipeline; |
| 35 | + private Project project; |
| 36 | + |
| 37 | + |
| 38 | + private List<Operation> operationList; |
| 39 | + private Optional<Throwable> throwableOptional; |
| 40 | + private EventBus eventBus; |
| 41 | + |
| 42 | + private class OperationGrabber { |
| 43 | + @Subscribe |
| 44 | + public void onOperationAddedEvent(OperationAddedEvent event) { |
| 45 | + operationList.add(event.getOperation()); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() throws Exception { |
| 51 | + |
| 52 | + //Set up the stuff we need for the core functionality for GRIP |
| 53 | + final Injector injector = Guice.createInjector(new GRIPCoreModule()); |
| 54 | + connectionFactory = injector |
| 55 | + .getInstance(Key.get(new TypeLiteral<Connection.Factory<Object>>() { |
| 56 | + })); |
| 57 | + imageSourceFactory = injector |
| 58 | + .getInstance(ImageFileSource.Factory.class); |
| 59 | + eventBus = injector.getInstance(EventBus.class); |
| 60 | + pipeline = injector.getInstance(Pipeline.class); |
| 61 | + project = injector.getInstance(Project.class); |
| 62 | + stepFactory = injector.getInstance(Step.Factory.class); |
| 63 | + |
| 64 | + //Set up stuff we need to add the operations |
| 65 | + this.operationList = new ArrayList<>(); |
| 66 | + this.throwableOptional = Optional.empty(); |
| 67 | + this.eventBus = new EventBus((exception, context) -> throwableOptional = Optional.of(exception)); |
| 68 | + |
| 69 | + //Set up the operation grabber to help us register the operations |
| 70 | + this.eventBus.register(new OperationGrabber()); |
| 71 | + |
| 72 | + //Set up the test project file to work with this machine |
| 73 | + URL location = CompatibilityTest.class.getProtectionDomain().getCodeSource().getLocation(); |
| 74 | + |
| 75 | + int numbOfFolders = Paths.get(location.toURI()).getNameCount(); |
| 76 | + |
| 77 | + |
| 78 | + String fileName = "/"+ Paths.get(location.toURI()).subpath(0,(numbOfFolders-4)).toString()+ "/testALL.grip"; |
| 79 | + String photoFileName = "/" + Paths.get(location.toURI()).subpath(0,(numbOfFolders-4)).toString()+ "/testphoto.png"; |
| 80 | + |
| 81 | + File file = new File(fileName); |
| 82 | + |
| 83 | + Reader temp = new FileReader(file); |
| 84 | + BufferedReader reader = new BufferedReader(temp); |
| 85 | + String line = "", oldtext = ""; |
| 86 | + while ((line = reader.readLine()) != null) { |
| 87 | + oldtext += line + "\r\n"; |
| 88 | + } |
| 89 | + reader.close(); |
| 90 | + String newtext = oldtext.replaceAll("REPLACEME", photoFileName); |
| 91 | + //TODO: replace "preferences" section to work with this machine |
| 92 | + FileWriter writer2 = new FileWriter(file); |
| 93 | + writer2.write(newtext); |
| 94 | + writer2.close(); |
| 95 | + |
| 96 | + //Add/register/create the operations |
| 97 | + CVOperations.addOperations(eventBus); |
| 98 | + for (Operation operation : operationList) { |
| 99 | + new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(operation); |
| 100 | + } |
| 101 | + |
| 102 | + Operations.addOperations(eventBus); |
| 103 | + for (Operation operation : operationList) { |
| 104 | + new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(operation); |
| 105 | + } |
| 106 | + |
| 107 | + //Open the test file as a project |
| 108 | + project.open(file); |
| 109 | + } |
| 110 | + |
| 111 | + @After |
| 112 | + public void afterTest() { |
| 113 | + //Throw any exceptions that weren't handled during the test |
| 114 | + if (throwableOptional.isPresent()) { |
| 115 | + throw Throwables.propagate(throwableOptional.get()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testFoo() throws Exception { |
| 121 | + |
| 122 | + assertEquals("blarg", |
| 123 | + 3, 3); |
| 124 | + |
| 125 | + } |
| 126 | +} |
0 commit comments