Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7529f3058053d395fd246d91ec472850798c321a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.util;

import static org.eclipse.osee.framework.core.enums.DeletionFlag.EXCLUDE_DELETED;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.BranchArchivedState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.exception.BranchDoesNotExist;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.PurgeArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;

/**
 * @author Donald G. Dunne
 */
public class FrameworkTestUtil {
   private static final String TEST_PATH_NAME = "../org.eclipse.osee.framework.skynet.core.test/";

   /**
    * Creates a simple artifact and adds it to the root artifact default hierarchical relation
    */
   public static Artifact createSimpleArtifact(IArtifactType artifactType, String name, IOseeBranch branch) throws OseeCoreException {
      Artifact softArt = ArtifactTypeManager.addArtifact(artifactType, branch);
      softArt.setName(name);
      softArt.addAttribute(CoreAttributeTypes.Subsystem, "Electrical");
      Artifact rootArtifact = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(branch);
      rootArtifact.addRelation(CoreRelationTypes.Default_Hierarchical__Child, softArt);
      return softArt;
   }

   public static Collection<Artifact> createSimpleArtifacts(IArtifactType artifactType, int numArts, String name, IOseeBranch branch) throws OseeCoreException {
      List<Artifact> arts = new ArrayList<Artifact>();
      for (int x = 1; x < numArts + 1; x++) {
         arts.add(createSimpleArtifact(artifactType, name + " " + x, branch));
      }
      return arts;
   }

   public static void purgeBranch(Branch branch) throws Exception {
      try {
         BranchManager.purgeBranch(branch);
      } catch (BranchDoesNotExist ex) {
         // do nothing
      }
   }

   public static void purgeWorkingBranches(Collection<String> branchNamesContain) throws Exception {
      try {
         // delete working branches
         for (Branch workingBranch : BranchManager.getBranches(BranchArchivedState.ALL, BranchType.WORKING)) {
            for (String branchName : branchNamesContain) {
               if (workingBranch.getName().contains(branchName)) {
                  BranchManager.purgeBranch(workingBranch);
                  //TestUtil.sleep(2000);
               }
            }
         }
      } catch (BranchDoesNotExist ex) {
         // do nothing
      }
   }

   /**
    * Deletes all artifacts with names that start with any title given
    */
   public static void cleanupSimpleTest(IOseeBranch branch, String... titles) throws Exception {
      List<Artifact> artifacts = new ArrayList<Artifact>();
      for (String title : titles) {
         artifacts.addAll(ArtifactQuery.getArtifactListFromName(title + "%", branch, EXCLUDE_DELETED));
      }
      if (artifacts.size() > 0) {
         Operations.executeWorkAndCheckStatus(new PurgeArtifacts(artifacts));
      }
   }

   private static List<String> executeCommand(List<String> commands) throws IOException, InterruptedException {
      List<String> resultStrings = new ArrayList<String>();
      ProcessBuilder myProcessBuilder = new ProcessBuilder();
      myProcessBuilder.command(commands);
      Process myProcess = myProcessBuilder.start();
      myProcess.waitFor();
      InputStream myInputStream = myProcess.getInputStream();
      BufferedReader myBufferedReader = new BufferedReader(new InputStreamReader(myInputStream));
      String line;
      while ((line = myBufferedReader.readLine()) != null) {
         resultStrings.add(line);
      }
      return resultStrings;
   }

   public static final List<String> killAllOpenWinword() throws IOException, InterruptedException {
      List<String> commands = new ArrayList<String>();
      commands.add("TASKKILL");
      commands.add("/F");
      commands.add("/IM");
      commands.add("wscript.exe");
      commands.add("/IM");
      commands.add("WINWORD.EXE");
      return executeCommand(commands);
   }

   public static final List<String> findAllWinWordRunning() throws IOException, InterruptedException {
      List<String> commands = new ArrayList<String>();
      commands.add("TASKLIST");
      commands.add("/FI");
      commands.add("Imagename eq WINWORD.EXE");
      return executeCommand(commands);
   }

   public static boolean areWinWordsRunning() throws IOException, InterruptedException {
      return findAllWinWordRunning().size() > 0 ? true : false;
   }

   public File getFileFromPlugin(String filename) throws Exception {
      return new File(TEST_PATH_NAME + "/" + filename);
   }
}

Back to the top