Skip to content

Commit

Permalink
#167 Allow EMF Tests to run in p2 build
Browse files Browse the repository at this point in the history
Due to the update of the mockito versions, we are now able to run the emf tests again also in the p2 build

If emf.tests are running via p2 the test resources are saved differently, hence let's execute tests on resources in a .temp directory to avoid having git changes on each p2 build.

Resolves #167
  • Loading branch information
ndoschek committed Jan 10, 2023
1 parent 2599866 commit 7dc599c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.temp/
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019 EclipseSource and others.
* Copyright (c) 2019-2023 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -10,7 +10,12 @@
********************************************************************************/
package org.eclipse.emfcloud.modelserver.emf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;

import org.eclipse.emf.common.util.URI;
Expand All @@ -21,12 +26,22 @@
import org.eclipse.emfcloud.modelserver.common.codecs.EMFJsonConverter;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;

public abstract class AbstractResourceTest {
public static final String RESOURCE_PATH = "resources/";
public static final String TEST_RESOURCES_PATH = "resources/";
public static final String RESOURCE_PATH = ".temp/";
@SuppressWarnings({ "checkstyle:VisibilityModifier" })
protected ResourceSetImpl resourceSet; // needed in ResourceManagerTest.java

@BeforeClass
public static void setupTestResources() throws IOException {
// copy resources to a temporary location to avoid git changes if executing tests on the resources
File sourceDirectory = new File(TEST_RESOURCES_PATH);
File destinationDirectory = new File(RESOURCE_PATH);
copyDirectory(sourceDirectory, destinationDirectory);
}

@Before
public void initializeResourceSet() {
resourceSet = new ResourceSetImpl();
Expand All @@ -35,6 +50,35 @@ public void initializeResourceSet() {
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
}

public static void copyDirectoryCompatibityMode(final File source, final File destination) throws IOException {
if (source.isDirectory()) {
copyDirectory(source, destination);
} else {
copyFile(source, destination);
}
}

private static void copyDirectory(final File sourceDirectory, final File destinationDirectory) throws IOException {
if (!destinationDirectory.exists()) {
destinationDirectory.mkdir();
}
for (String f : sourceDirectory.list()) {
copyDirectoryCompatibityMode(new File(sourceDirectory, f), new File(destinationDirectory, f));
}
}

private static void copyFile(final File sourceFile, final File destinationFile)
throws IOException {
try (InputStream in = new FileInputStream(sourceFile);
OutputStream out = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}

protected Resource loadResource(final String file) throws IOException {
Resource resource = resourceSet.createResource(URI.createFileURI(toFullPath(file)));
resource.load(Collections.EMPTY_MAP);
Expand Down
6 changes: 1 addition & 5 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@
<module>org.eclipse.emfcloud.modelserver.client.tests</module>
<module>org.eclipse.emfcloud.modelserver.common.tests</module>
<module>org.eclipse.emfcloud.modelserver.edit.tests</module>
<module>org.eclipse.emfcloud.modelserver.emf.tests</module>
<module>org.eclipse.emfcloud.modelserver.tests</module>
</modules>

<profiles>
<profile>
<id>m2</id>

<!-- mockito cannot properly mock some classes so tests fail in p2 -->
<modules>
<module>org.eclipse.emfcloud.modelserver.emf.tests</module>
</modules>

<build>
<plugins>
<plugin>
Expand Down

0 comments on commit 7dc599c

Please sign in to comment.