|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2019 IBM Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * IBM Corporation - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.codewind.openapi.test.util; |
| 14 | + |
| 15 | +import java.io.BufferedInputStream; |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.FileNotFoundException; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.net.URL; |
| 21 | +import java.util.StringTokenizer; |
| 22 | + |
| 23 | +import org.eclipse.codewind.openapi.test.Activator; |
| 24 | +import org.eclipse.core.resources.IFile; |
| 25 | +import org.eclipse.core.resources.IFolder; |
| 26 | +import org.eclipse.core.resources.IProject; |
| 27 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 28 | +import org.eclipse.core.runtime.CoreException; |
| 29 | +import org.eclipse.core.runtime.FileLocator; |
| 30 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 31 | +import org.eclipse.core.runtime.NullProgressMonitor; |
| 32 | +import org.eclipse.core.runtime.Path; |
| 33 | +import org.junit.Assert; |
| 34 | + |
| 35 | +public class TestUtilities { |
| 36 | + |
| 37 | + public TestUtilities() { |
| 38 | + // Empty |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Create a General Project |
| 43 | + * |
| 44 | + * @param projectName |
| 45 | + * @return |
| 46 | + * @throws CoreException |
| 47 | + */ |
| 48 | + public static IProject createGeneralProject(String projectName) throws CoreException { |
| 49 | + IProgressMonitor monitor = new NullProgressMonitor(); |
| 50 | + IProject testProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
| 51 | + try { |
| 52 | + testProject.create(monitor); |
| 53 | + testProject.open(monitor); |
| 54 | + } catch (CoreException e) { |
| 55 | + throw e; |
| 56 | + } |
| 57 | + return testProject; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * |
| 62 | + * @param sourceFile - source OpenAPI definition in the test plugin resources folder |
| 63 | + * @param destinationFile - path of target OpenAPI definition in the test project but excludes |
| 64 | + * the project name |
| 65 | + */ |
| 66 | + public static IFile copyDefinitionToProject(String sourceFile, String destinationFile, IProject testProject) throws CoreException, FileNotFoundException, IOException { |
| 67 | + NullProgressMonitor nullProgressMonitor = new NullProgressMonitor(); |
| 68 | + try { |
| 69 | + URL url = Activator.getDefault().getBundle().getEntry(sourceFile); |
| 70 | + URL fileURL = FileLocator.toFileURL(url); |
| 71 | + |
| 72 | + StringTokenizer st = new StringTokenizer(destinationFile, "/"); |
| 73 | + StringBuffer sb = new StringBuffer(); |
| 74 | + IFolder aFolder = null; |
| 75 | + while (st.hasMoreTokens()) { |
| 76 | + String nextToken = st.nextToken(); |
| 77 | + if (!st.hasMoreTokens()) { |
| 78 | + break; |
| 79 | + } |
| 80 | + aFolder = testProject.getFolder(new Path(sb.toString() + "/" + nextToken)); |
| 81 | + aFolder.create(true, true, nullProgressMonitor); |
| 82 | + sb.append("/" + nextToken); |
| 83 | + } |
| 84 | + IFile destFile = testProject.getFile(destinationFile); |
| 85 | + InputStream in = new BufferedInputStream(new FileInputStream(fileURL.getFile())); |
| 86 | + destFile.create(in, true, nullProgressMonitor); |
| 87 | + |
| 88 | + testProject.refreshLocal(IProject.DEPTH_ONE, nullProgressMonitor); |
| 89 | + IFile defFile = testProject.getFile(destinationFile); |
| 90 | + Assert.assertTrue("Copied file exists in workspace", defFile.exists()); |
| 91 | + Assert.assertEquals("Definition is in the test project", testProject, defFile.getProject()); |
| 92 | + return defFile; |
| 93 | + } catch (CoreException e) { |
| 94 | + throw e; |
| 95 | + } catch (FileNotFoundException e) { |
| 96 | + throw e; |
| 97 | + } catch (IOException e) { |
| 98 | + throw e; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments