| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.hadoop.mrunit; |
| |
| import org.apache.hadoop.io.Text; |
| import org.apache.hadoop.mrunit.types.Pair; |
| import org.junit.Before; |
| import org.junit.Test; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import static org.apache.hadoop.mrunit.ExtendedAssert.assertListEquals; |
| import static org.junit.Assert.assertEquals; |
| |
| public class TestMapOutputShuffler { |
| private MapOutputShuffler<Text, Text> shuffler; |
| |
| @Before |
| public void setUp() { |
| shuffler = new MapOutputShuffler<Text, Text>(null, null, null); |
| } |
| |
| @Test |
| public void testEmptyShuffle() { |
| final List<Pair<Text, Text>> inputs = new ArrayList<Pair<Text, Text>>(); |
| final List<Pair<Text, List<Text>>> outputs = shuffler.shuffle(inputs); |
| assertEquals(0, outputs.size()); |
| } |
| |
| // just shuffle a single (k, v) pair |
| @Test |
| public void testSingleShuffle() { |
| final List<Pair<Text, Text>> inputs = new ArrayList<Pair<Text, Text>>(); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("b"))); |
| |
| final List<Pair<Text, List<Text>>> outputs = shuffler.shuffle(inputs); |
| |
| final List<Pair<Text, List<Text>>> expected = new ArrayList<Pair<Text, List<Text>>>(); |
| final List<Text> sublist = new ArrayList<Text>(); |
| sublist.add(new Text("b")); |
| expected.add(new Pair<Text, List<Text>>(new Text("a"), sublist)); |
| |
| assertListEquals(expected, outputs); |
| } |
| |
| // shuffle multiple values from the same key. |
| @Test |
| public void testShuffleOneKey() { |
| final List<Pair<Text, Text>> inputs = new ArrayList<Pair<Text, Text>>(); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("b"))); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("c"))); |
| |
| final List<Pair<Text, List<Text>>> outputs = shuffler.shuffle(inputs); |
| |
| final List<Pair<Text, List<Text>>> expected = new ArrayList<Pair<Text, List<Text>>>(); |
| final List<Text> sublist = new ArrayList<Text>(); |
| sublist.add(new Text("b")); |
| sublist.add(new Text("c")); |
| expected.add(new Pair<Text, List<Text>>(new Text("a"), sublist)); |
| |
| assertListEquals(expected, outputs); |
| } |
| |
| // shuffle multiple keys |
| @Test |
| public void testMultiShuffle1() { |
| final List<Pair<Text, Text>> inputs = new ArrayList<Pair<Text, Text>>(); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("x"))); |
| inputs.add(new Pair<Text, Text>(new Text("b"), new Text("z"))); |
| inputs.add(new Pair<Text, Text>(new Text("b"), new Text("w"))); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("y"))); |
| |
| final List<Pair<Text, List<Text>>> outputs = shuffler.shuffle(inputs); |
| |
| final List<Pair<Text, List<Text>>> expected = new ArrayList<Pair<Text, List<Text>>>(); |
| final List<Text> sublist1 = new ArrayList<Text>(); |
| sublist1.add(new Text("x")); |
| sublist1.add(new Text("y")); |
| expected.add(new Pair<Text, List<Text>>(new Text("a"), sublist1)); |
| |
| final List<Text> sublist2 = new ArrayList<Text>(); |
| sublist2.add(new Text("z")); |
| sublist2.add(new Text("w")); |
| expected.add(new Pair<Text, List<Text>>(new Text("b"), sublist2)); |
| |
| assertListEquals(expected, outputs); |
| } |
| |
| // shuffle multiple keys that are out-of-order to start. |
| @Test |
| public void testMultiShuffle2() { |
| final List<Pair<Text, Text>> inputs = new ArrayList<Pair<Text, Text>>(); |
| inputs.add(new Pair<Text, Text>(new Text("b"), new Text("z"))); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("x"))); |
| inputs.add(new Pair<Text, Text>(new Text("b"), new Text("w"))); |
| inputs.add(new Pair<Text, Text>(new Text("a"), new Text("y"))); |
| |
| final List<Pair<Text, List<Text>>> outputs = shuffler.shuffle(inputs); |
| |
| final List<Pair<Text, List<Text>>> expected = new ArrayList<Pair<Text, List<Text>>>(); |
| final List<Text> sublist1 = new ArrayList<Text>(); |
| sublist1.add(new Text("x")); |
| sublist1.add(new Text("y")); |
| expected.add(new Pair<Text, List<Text>>(new Text("a"), sublist1)); |
| |
| final List<Text> sublist2 = new ArrayList<Text>(); |
| sublist2.add(new Text("z")); |
| sublist2.add(new Text("w")); |
| expected.add(new Pair<Text, List<Text>>(new Text("b"), sublist2)); |
| |
| assertListEquals(expected, outputs); |
| } |
| } |