blob: 451cd7d7370d9554a9e05924b30934a274c4b9d2 [file] [log] [blame]
package org.eclipse.stem.tests.grapheditor;
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.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.stem.core.graph.Graph;
import org.eclipse.stem.core.model.Model;
import org.eclipse.stem.ui.grapheditor.GraphCanvas;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
/**
* @author WiggerJ
*
*/
public class GraphCanvasTest extends TestCase {
private static final String XML_TEST_FILE = "test1_MAP.xml";
private static final String GRAPH_TEST_FILE = "test1.graph";
private static final String MODEL_TEST_FILE_MODEL = "test2.model";
private static final String MODEL_TEST_FILE_GRAPH_1 = "test2.graph";
private static final String MODEL_TEST_FILE_GRAPH_2 = "test2_area.graph";
private static final String RUNTIME_WORKSPACE_PATH = Platform.getLocation().toOSString();
private final static String REFERENCE_DIR = "./resources/"; //$NON-NLS-1$
private final static String sep = File.separator;
protected GraphCanvas fixture = null;
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public static void main(String[] args) {
TestRunner.run(GraphCanvasTest.class);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see junit.framework.TestCase#setUp()
* @generated NOT
*/
@Override
protected void setUp() throws Exception {
setFixture(createFixture());
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see junit.framework.TestCase#tearDown()
* @generated
*/
@Override
protected void tearDown() throws Exception {
setFixture(null);
}
/**
* Sets the fixture for this Pipe Transport Edge test case.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
protected void setFixture(GraphCanvas fixture) {
this.fixture = fixture;
}
/**
* Returns the fixture for this Pipe Transport Edge test case.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
protected GraphCanvas getFixture() {
return fixture;
}
/**
* create the fixture
**/
protected static GraphCanvas createFixture() {
GraphCanvas graphCanvas = new GraphCanvas();
return graphCanvas;
}
/**
* @generated NOT
*/
public void testGraphCanvas() {
String XML_FILE_PATH_GRAPH = getTestFile(XML_TEST_FILE);
String TEST_FILE_PATH_GRAPH = getTestFile(GRAPH_TEST_FILE);
ResourceSet resourceSet = new ResourceSetImpl();
// Register the appropriate resource factory to handle all file extensions.
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put
(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
List<Graph> graphList = new ArrayList<Graph>();
File file = new File(TEST_FILE_PATH_GRAPH);
URI uri = URI.createFileURI(file.getAbsolutePath());
Resource resource = resourceSet.getResource(uri, true);
Graph graph = (Graph) resource.getContents().get(0);
/*
System.out.println(XML_FILE_PATH_GRAPH);
System.out.println(file.getAbsolutePath());
System.out.println(graph == null);
System.out.println(graph.getNumNodes());
System.out.println(graph.getNumNodeLabels());
System.out.println(graph.getDublinCore().getSpatial());
*/
graphList.add(graph);
fixture.display(graphList, null, null, null);
graphList = new ArrayList<Graph>();
String TEST_FILE_PATH_MODEL_MODEL = getTestFile(MODEL_TEST_FILE_MODEL);
String TEST_FILE_PATH_MODEL_GRAPH_1 = getTestFile(MODEL_TEST_FILE_GRAPH_1);
String TEST_FILE_PATH_MODEL_GRAPH_2 = getTestFile(MODEL_TEST_FILE_GRAPH_2);
file = new File(TEST_FILE_PATH_MODEL_GRAPH_1);
uri = URI.createFileURI(file.getAbsolutePath());
resource = resourceSet.getResource(uri, true);
graph = (Graph) resource.getContents().get(0);
graphList.add(graph);
file = new File(TEST_FILE_PATH_MODEL_GRAPH_2);
uri = URI.createFileURI(file.getAbsolutePath());
resource = resourceSet.getResource(uri, true);
graph = (Graph) resource.getContents().get(0);
graphList.add(graph);
file = new File(TEST_FILE_PATH_MODEL_MODEL);
uri = URI.createFileURI(file.getAbsolutePath());
resource = resourceSet.getResource(uri, true);
Model model = (Model) resource.getContents().get(0);
model.getGraphs().clear();
model.getGraphs().addAll(graphList);
fixture.display(graphList, null, null, model);
//assertTrue(getFixture().sane());
}
private static String getTestFile(String testFilename) {
// 1. copy the test file FROM the source directory TO the runtime workspace where ever that is
File refDirectory = new File(REFERENCE_DIR);
if (refDirectory.isDirectory()) {
// should always be true
File[] files = refDirectory.listFiles();
if (files==null) fail("Error:GraphCanvasTest Example File not found !!");//$NON-NLS-1$
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equalsIgnoreCase(testFilename)) {
String resFile = RUNTIME_WORKSPACE_PATH + sep + files[i].getName();
try {
copyFile(files[i], new File(resFile));
return resFile;
} catch (Exception ioe) {
System.out.println("Copy failed "+ioe.getMessage());
ioe.printStackTrace();
fail("Error:GraphCanvasTest failed to copy test file !!");//$NON-NLS-1$
}
break;
}
}//for
}// if
// else we faile
fail("Error:GraphCanvasTest failed to find test file !!");//$NON-NLS-1$
return "";
}
private static void copyFile(File sourceFile, File targetFile) throws IOException, CoreException {
if (targetFile.exists()) return; // already exists
InputStream inputStream = new FileInputStream(sourceFile);
OutputStream outputStream = new FileOutputStream(targetFile);
// Copy the bits from instream to outstream
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
}
}