Index: examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java =================================================================== --- examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java (revision 1226591) +++ examples/src/test/java/org/apache/hama/examples/ShortestPathsTest.java (working copy) @@ -17,23 +17,155 @@ */ package org.apache.hama.examples; -import org.junit.Test; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; -import static org.junit.Assert.fail; +import junit.framework.TestCase; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.io.IntWritable; +import org.apache.hadoop.io.SequenceFile; +import org.apache.hadoop.io.Text; +import org.apache.hama.HamaConfiguration; + /** * Testcase for {@link ShortestPaths} */ -public class ShortestPathsTest { +public class ShortestPathsTest extends TestCase { + private static String INPUT = "/tmp/sssp-tmp.seq"; + private static String OUTPUT = "/tmp/sssp-out"; + private Configuration conf; + private FileSystem fs; - @Test - public void testShortestPathsWithWrongArgs() { - try { - ShortestPaths.main(new String[]{"1", ".", "."}); - fail("ShortestPaths should fail if the arguments list contains wrong items"); - } catch (Exception e) { - // everything ok + public void testShortestPathsWithWrongArgs() throws IOException, + InterruptedException, ClassNotFoundException, InstantiationException, + IllegalAccessException { + conf = new HamaConfiguration(); + fs = FileSystem.get(conf); + + generateTestData(); + ShortestPaths.main(new String[] { "Frankfurt", OUTPUT, INPUT }); + + verifyResult(); + fs.delete(new Path(INPUT), true); + fs.delete(new Path(OUTPUT), true); + } + + private void verifyResult() throws IOException { + Map rs = new HashMap(); + rs.put("Erfurt", 385); + rs.put("Mannheim", 85); + rs.put("Stuttgart", 503); + rs.put("Kassel", 173); + rs.put("Nuernberg", 320); + rs.put("Augsburg", 415); + rs.put("Frankfurt", 0); + rs.put("Muenchen", 487); + rs.put("Wuerzburg", 217); + rs.put("Karlsruhe", 165); + + SequenceFile.Reader reader = new SequenceFile.Reader(fs, new Path(OUTPUT + + "/part-00000"), conf); + Text key = new Text(); + IntWritable value = new IntWritable(); + while (reader.next(key, value)) { + assertEquals(value.get(), (int) rs.get(key.toString())); } } + + private void generateTestData() throws IOException { + Map tmp = new HashMap(); + String[] cities = new String[] { "Frankfurt", "Mannheim", "Wuerzburg", + "Stuttgart", "Kassel", "Karlsruhe", "Erfurt", "Nuernberg", "Augsburg", + "Muenchen" }; + + for (String city : cities) { + if (city.equals("Frankfurt")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[3]; + textArr[0] = new ShortestPathVertex(85, "Mannheim"); + textArr[1] = new ShortestPathVertex(173, "Kassel"); + textArr[2] = new ShortestPathVertex(217, "Wuerzburg"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Stuttgart")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[1]; + textArr[0] = new ShortestPathVertex(183, "Nuernberg"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Kassel")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[2]; + textArr[0] = new ShortestPathVertex(502, "Muenchen"); + textArr[1] = new ShortestPathVertex(173, "Frankfurt"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Erfurt")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[1]; + textArr[0] = new ShortestPathVertex(186, "Wuerzburg"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Wuerzburg")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[3]; + textArr[0] = new ShortestPathVertex(217, "Frankfurt"); + textArr[1] = new ShortestPathVertex(168, "Erfurt"); + textArr[2] = new ShortestPathVertex(103, "Nuernberg"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Mannheim")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[2]; + textArr[0] = new ShortestPathVertex(80, "Karlsruhe"); + textArr[1] = new ShortestPathVertex(85, "Frankfurt"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Karlsruhe")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[2]; + textArr[0] = new ShortestPathVertex(250, "Augsburg"); + textArr[1] = new ShortestPathVertex(80, "Mannheim"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Augsburg")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[2]; + textArr[0] = new ShortestPathVertex(250, "Karlsruhe"); + textArr[1] = new ShortestPathVertex(84, "Muenchen"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Nuernberg")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[3]; + textArr[0] = new ShortestPathVertex(183, "Stuttgart"); + textArr[1] = new ShortestPathVertex(167, "Muenchen"); + textArr[2] = new ShortestPathVertex(103, "Wuerzburg"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } else if (city.equals("Muenchen")) { + ShortestPathVertex[] textArr = new ShortestPathVertex[3]; + textArr[0] = new ShortestPathVertex(167, "Nuernberg"); + textArr[1] = new ShortestPathVertex(173, "Kassel"); + textArr[2] = new ShortestPathVertex(84, "Augsburg"); + ShortestPathVertexArrayWritable arr = new ShortestPathVertexArrayWritable(); + arr.set(textArr); + tmp.put(new ShortestPathVertex(0, city), arr); + } + } + + SequenceFile.Writer writer = SequenceFile + .createWriter(fs, conf, new Path(INPUT), ShortestPathVertex.class, + ShortestPathVertexArrayWritable.class); + for (Map.Entry e : tmp + .entrySet()) { + writer.append(e.getKey(), e.getValue()); + } + writer.close(); + } }