Skip to content

Commit fcee2b6

Browse files
author
jantje
committed
Added junit to test building examples
Seems like some there is a bug in finding the examples as root folders are marked as examples
1 parent 44274b9 commit fcee2b6

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package jUnit;
2+
3+
import static org.junit.Assert.fail;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.HashMap;
9+
import java.util.HashSet;
10+
import java.util.LinkedList;
11+
import java.util.Map;
12+
import java.util.TreeMap;
13+
14+
import org.eclipse.core.resources.IProject;
15+
import org.eclipse.core.resources.IncrementalProjectBuilder;
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.core.runtime.IPath;
18+
import org.eclipse.core.runtime.NullProgressMonitor;
19+
import org.eclipse.core.runtime.Path;
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.Parameterized;
24+
25+
import io.sloeber.core.api.BoardDescriptor;
26+
import io.sloeber.core.api.BoardsManager;
27+
import io.sloeber.core.api.CodeDescriptor;
28+
import io.sloeber.core.api.CompileOptions;
29+
import io.sloeber.core.api.ConfigurationDescriptor;
30+
31+
@SuppressWarnings("nls")
32+
@RunWith(Parameterized.class)
33+
public class CreateAndCompileExamples {
34+
private static int mCounter = 0;
35+
private CodeDescriptor myCodeDescriptor;
36+
37+
public CreateAndCompileExamples(CodeDescriptor codeDescriptor) {
38+
this.myCodeDescriptor = codeDescriptor;
39+
40+
}
41+
42+
@SuppressWarnings("rawtypes")
43+
@Parameterized.Parameters
44+
public static Collection examples() {
45+
LinkedList<CodeDescriptor> examples = new LinkedList<>();
46+
TreeMap<String, IPath> exampleFolders = BoardsManager.getAllExamples(null);
47+
for (Map.Entry<String, IPath> curexample : exampleFolders.entrySet()) {
48+
ArrayList<Path> paths = new ArrayList<>();
49+
50+
paths.add(new Path(curexample.getValue().toString()));
51+
CodeDescriptor codeDescriptor = CodeDescriptor.createExample(false, paths);
52+
53+
examples.add(codeDescriptor);
54+
}
55+
56+
return examples;
57+
58+
}
59+
60+
/*
61+
* In new new installations (of the Sloeber development environment) the
62+
* installer job will trigger downloads These mmust have finished before we
63+
* can start testing
64+
*/
65+
@BeforeClass
66+
public static void WaitForInstallerToFinish() {
67+
installAdditionalBoards();
68+
Shared.waitForAllJobsToFinish();
69+
}
70+
71+
public static void installAdditionalBoards() {
72+
String[] packageUrlsToAdd = { "http://arduino.esp8266.com/stable/package_esp8266com_index.json" };
73+
BoardsManager.addPackageURLs(new HashSet<>(Arrays.asList(packageUrlsToAdd)), true);
74+
BoardsManager.installAllLatestPlatforms();
75+
76+
}
77+
78+
@Test
79+
public void testUno() {
80+
81+
Map<String, String> myOptions = new HashMap<>();
82+
String[] lines = new String("").split("\n"); //$NON-NLS-1$
83+
for (String curLine : lines) {
84+
String[] values = curLine.split("=", 2); //$NON-NLS-1$
85+
if (values.length == 2) {
86+
myOptions.put(values[0], values[1]);
87+
}
88+
}
89+
90+
BoardDescriptor boardid = BoardsManager.getBoardID("package_index.json", "arduino", "Arduino AVR Boards", "uno",
91+
myOptions);
92+
if (boardid == null) {
93+
fail("Uno Board not found");
94+
return;
95+
}
96+
BuildAndVerify(boardid, this.myCodeDescriptor);
97+
98+
}
99+
100+
public static void BuildAndVerify(BoardDescriptor boardid, CodeDescriptor codeDescriptor) {
101+
102+
IProject theTestProject = null;
103+
104+
NullProgressMonitor monitor = new NullProgressMonitor();
105+
String projectName = String.format("%03d_", new Integer(mCounter++)) + boardid.getBoardID()
106+
+ codeDescriptor.getExamples().get(0).lastSegment();
107+
try {
108+
109+
theTestProject = boardid.createProject(projectName, null, ConfigurationDescriptor.getDefaultDescriptors(),
110+
codeDescriptor, new CompileOptions(null), monitor);
111+
Shared.waitForAllJobsToFinish(); // for the indexer
112+
} catch (Exception e) {
113+
e.printStackTrace();
114+
fail("Failed to create the project:" + projectName);
115+
return;
116+
}
117+
try {
118+
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
119+
if (Shared.hasBuildErrors(theTestProject)) {
120+
// try again because the libraries may not yet been added
121+
Shared.waitForAllJobsToFinish(); // for the indexer
122+
theTestProject.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
123+
if (Shared.hasBuildErrors(theTestProject)) {
124+
// give up
125+
fail("Failed to compile the project:" + projectName + " build errors");
126+
}
127+
}
128+
} catch (CoreException e) {
129+
e.printStackTrace();
130+
fail("Failed to compile the project:" + projectName + " exception");
131+
}
132+
}
133+
134+
}

0 commit comments

Comments
 (0)