File inputFile1 = null;
File inputFile2 = null;
File outputFile = null;
File jarFile = new File(ServerTestUtils.getTempDir() + File.separator + "unionWithEmptyInput.jar");
JobClient jobClient = null;
try {
inputFile1 = ServerTestUtils.createInputFile(limit);
inputFile2 = ServerTestUtils.createInputFile(limit);
outputFile = new File(ServerTestUtils.getTempDir() + File.separator + ServerTestUtils.getRandomFilename());
// Create required jar file
JarFileCreator jfc = new JarFileCreator(jarFile);
jfc.addClass(UnionTask.class);
jfc.createJarFile();
// Create job graph
final JobGraph jg = new JobGraph("Union job " + limit);
// input vertex 1
final JobFileInputVertex i1 = new JobFileInputVertex("Input 1", jg);
i1.setFileInputClass(FileLineReader.class);
i1.setFilePath(new Path(inputFile1.toURI()));
// input vertex 2
final JobFileInputVertex i2 = new JobFileInputVertex("Input 2", jg);
i2.setFileInputClass(FileLineReader.class);
i2.setFilePath(new Path(inputFile2.toURI()));
// union task
final JobTaskVertex u1 = new JobTaskVertex("Union", jg);
u1.setTaskClass(UnionTask.class);
// output vertex
JobFileOutputVertex o1 = new JobFileOutputVertex("Output", jg);
o1.setFileOutputClass(FileLineWriter.class);
o1.setFilePath(new Path(outputFile.toURI()));
i1.setVertexToShareInstancesWith(o1);
i2.setVertexToShareInstancesWith(o1);
u1.setVertexToShareInstancesWith(o1);
// connect vertices
i1.connectTo(u1, ChannelType.IN_MEMORY, DistributionPattern.POINTWISE);
i2.connectTo(u1, ChannelType.IN_MEMORY);
u1.connectTo(o1, ChannelType.IN_MEMORY);
// add jar
jg.addJar(new Path(jarFile.toURI()));
// Create job client and launch job
jobClient = new JobClient(jg, configuration);
try {
jobClient.submitJobAndWait();
} catch (JobExecutionException e) {
fail(e.getMessage());
}
// Finally, check the output
final Map<Integer, Integer> expectedNumbers = new HashMap<Integer, Integer>();
final Integer two = Integer.valueOf(2);
for (int i = 0; i < limit; ++i) {
expectedNumbers.put(Integer.valueOf(i), two);
}
final BufferedReader bufferedReader = new BufferedReader(new FileReader(outputFile));
String line = bufferedReader.readLine();
while (line != null) {
final Integer number = Integer.valueOf(Integer.parseInt(line));
Integer val = expectedNumbers.get(number);
if (val == null) {
fail("Found unexpected number in union output: " + number);
} else {
val = Integer.valueOf(val.intValue() - 1);
if (val.intValue() < 0) {
fail(val + " occurred more than twice in union output");
}
if (val.intValue() == 0) {
expectedNumbers.remove(number);
} else {
expectedNumbers.put(number, val);
}
}
line = bufferedReader.readLine();
}
bufferedReader.close();
if (!expectedNumbers.isEmpty()) {
final StringBuilder str = new StringBuilder();
str.append("The following numbers have not been found in the union output:\n");
final Iterator<Map.Entry<Integer, Integer>> it = expectedNumbers.entrySet().iterator();
while (it.hasNext()) {
final Map.Entry<Integer, Integer> entry = it.next();
str.append(entry.getKey().toString());
str.append(" (");
str.append(entry.getValue().toString());
str.append("x)\n");
}
fail(str.toString());
}
} catch (JobGraphDefinitionException jgde) {
fail(jgde.getMessage());
} catch (IOException ioe) {
fail(ioe.getMessage());
} finally {
// Remove temporary files
if (inputFile1 != null) {
inputFile1.delete();
}
if (inputFile2 != null) {
inputFile2.delete();
}
if (outputFile != null) {
outputFile.delete();
}
if (jarFile != null) {
jarFile.delete();
}
if (jobClient != null) {
jobClient.close();
}
}
}