WordMapper extends Mapper<LongWritable, Text, Text, IntWritable> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer lineTokenizer = new StringTokenizer(line); while (lineTokenizer.hasMoreTokens()) { String cleaned = removeNonLettersOrNumbers(lineTokenizer.nextToken()); word.set(cleaned); context.write(word, new IntWritable(1)); } }    }
@Test public void MapEmpty() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text(""), contextMock); verifyZeroInteractions(contextMock); }
@Test public void MapWithApostrophe() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("I'"), contextMock); ArgumentCaptor<Text> keyArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(keyArgument.capture(), valueArgument.capture()); assertEquals("I", keyArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }
@Test public void mapWithComma() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("I,"), contextMock); ArgumentCaptor<Text> keyArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(keyArgument.capture(), valueArgument.capture()); assertEquals("I", keyArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }
@Test public void mapWithDot() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("I."), contextMock); ArgumentCaptor<Text> keyArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(keyArgument.capture(), valueArgument.capture()); assertEquals("I", keyArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }
@Test public void mapWithDoubleLine() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("I--"), contextMock); ArgumentCaptor<Text> keyArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(keyArgument.capture(), valueArgument.capture()); assertEquals("I", keyArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }
@Test public void mapWithExclamationMark() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("I!"), contextMock); ArgumentCaptor<Text> keyArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(keyArgument.capture(), valueArgument.capture()); assertEquals("I", keyArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }
@Test public void mapWithQuestionMark() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("I?"), contextMock); ArgumentCaptor<Text> keyArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(keyArgument.capture(), valueArgument.capture()); assertEquals("I", keyArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }
@Test public void mapWithQuationMark() throws IOException, InterruptedException { LongWritable key = new LongWritable(1); mapper.map(key, new Text("\"I"), contextMock); ArgumentCaptor<Text> wordArgument = ArgumentCaptor.forClass(Text.class); ArgumentCaptor<IntWritable> valueArgument = ArgumentCaptor.forClass(IntWritable.class); verify(contextMock, times(1)).write(wordArgument.capture(), valueArgument.capture()); assertEquals("I", wordArgument.getValue().toString()); assertEquals(1, valueArgument.getValue().get()); verifyNoMoreInteractions(contextMock); }