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 11, 2023
1 parent 1a7a73f commit d2aeee2
Show file tree
Hide file tree
Showing 3 changed files with 47 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,23 @@
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 test resources to a temporary resource location to avoid
// git changes if executing tests due to saving resources
File sourceDirectory = new File(TEST_RESOURCES_PATH);
File destinationDirectory = new File(RESOURCE_PATH);
copyDirectoryFiles(sourceDirectory, destinationDirectory);
}

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

private static void copyDirectoryFiles(final File sourceDirectory, final File destinationDirectory)
throws IOException {
if (!destinationDirectory.exists()) {
destinationDirectory.mkdir();
}
for (String f : sourceDirectory.list()) {
File source = new File(sourceDirectory, f);
File destination = new File(sourceDirectory, f);
// our src directory only contains files
if (source.isFile()) {
copyFile(source, destination);
}
}
}

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 d2aeee2

Please sign in to comment.