001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.mapreduce; 019 020import static org.junit.Assert.*; 021import static org.mockito.Mockito.*; 022 023import java.io.ByteArrayOutputStream; 024import java.io.PrintStream; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.client.Put; 030import org.apache.hadoop.hbase.client.Result; 031import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 032import org.apache.hadoop.hbase.mapreduce.IndexBuilder.Map; 033import org.apache.hadoop.hbase.mapreduce.SampleUploader.Uploader; 034import org.apache.hadoop.hbase.testclassification.LargeTests; 035import org.apache.hadoop.hbase.testclassification.MapReduceTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.hadoop.hbase.util.LauncherSecurityManager; 038import org.apache.hadoop.io.LongWritable; 039import org.apache.hadoop.io.Text; 040import org.apache.hadoop.mapreduce.Job; 041import org.apache.hadoop.mapreduce.Mapper; 042import org.apache.hadoop.mapreduce.Mapper.Context; 043import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.mockito.invocation.InvocationOnMock; 048import org.mockito.stubbing.Answer; 049 050@Category({MapReduceTests.class, LargeTests.class}) 051public class TestMapReduceExamples { 052 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestMapReduceExamples.class); 056 057 private static HBaseTestingUtility util = new HBaseTestingUtility(); 058 059 /** 060 * Test SampleUploader from examples 061 */ 062 063 @SuppressWarnings("unchecked") 064 @Test 065 public void testSampleUploader() throws Exception { 066 067 Configuration configuration = new Configuration(); 068 Uploader uploader = new Uploader(); 069 Mapper<LongWritable, Text, ImmutableBytesWritable, Put>.Context ctx = mock(Context.class); 070 doAnswer(new Answer<Void>() { 071 072 @Override 073 public Void answer(InvocationOnMock invocation) throws Throwable { 074 ImmutableBytesWritable writer = (ImmutableBytesWritable) invocation.getArgument(0); 075 Put put = (Put) invocation.getArgument(1); 076 assertEquals("row", Bytes.toString(writer.get())); 077 assertEquals("row", Bytes.toString(put.getRow())); 078 return null; 079 } 080 }).when(ctx).write(any(), any()); 081 082 uploader.map(null, new Text("row,family,qualifier,value"), ctx); 083 084 Path dir = util.getDataTestDirOnTestFS("testSampleUploader"); 085 086 String[] args = { dir.toString(), "simpleTable" }; 087 Job job = SampleUploader.configureJob(configuration, args); 088 assertEquals(SequenceFileInputFormat.class, job.getInputFormatClass()); 089 090 } 091 092 /** 093 * Test main method of SampleUploader. 094 */ 095 @Test 096 public void testMainSampleUploader() throws Exception { 097 PrintStream oldPrintStream = System.err; 098 SecurityManager SECURITY_MANAGER = System.getSecurityManager(); 099 LauncherSecurityManager newSecurityManager= new LauncherSecurityManager(); 100 System.setSecurityManager(newSecurityManager); 101 ByteArrayOutputStream data = new ByteArrayOutputStream(); 102 String[] args = {}; 103 System.setErr(new PrintStream(data)); 104 try { 105 System.setErr(new PrintStream(data)); 106 107 try { 108 SampleUploader.main(args); 109 fail("should be SecurityException"); 110 } catch (SecurityException e) { 111 assertEquals(-1, newSecurityManager.getExitCode()); 112 assertTrue(data.toString().contains("Wrong number of arguments:")); 113 assertTrue(data.toString().contains("Usage: SampleUploader <input> <tablename>")); 114 } 115 116 } finally { 117 System.setErr(oldPrintStream); 118 System.setSecurityManager(SECURITY_MANAGER); 119 } 120 121 } 122 123 /** 124 * Test IndexBuilder from examples 125 */ 126 @SuppressWarnings("unchecked") 127 @Test 128 public void testIndexBuilder() throws Exception { 129 Configuration configuration = new Configuration(); 130 String[] args = { "tableName", "columnFamily", "column1", "column2" }; 131 IndexBuilder.configureJob(configuration, args); 132 assertEquals("tableName", configuration.get("index.tablename")); 133 assertEquals("tableName", configuration.get(TableInputFormat.INPUT_TABLE)); 134 assertEquals("column1,column2", configuration.get("index.fields")); 135 136 Map map = new Map(); 137 ImmutableBytesWritable rowKey = new ImmutableBytesWritable(Bytes.toBytes("test")); 138 Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put>.Context ctx = 139 mock(Context.class); 140 when(ctx.getConfiguration()).thenReturn(configuration); 141 doAnswer(new Answer<Void>() { 142 143 @Override 144 public Void answer(InvocationOnMock invocation) throws Throwable { 145 ImmutableBytesWritable writer = (ImmutableBytesWritable) invocation.getArgument(0); 146 Put put = (Put) invocation.getArgument(1); 147 assertEquals("tableName-column1", Bytes.toString(writer.get())); 148 assertEquals("test", Bytes.toString(put.getRow())); 149 return null; 150 } 151 }).when(ctx).write(any(), any()); 152 Result result = mock(Result.class); 153 when(result.getValue(Bytes.toBytes("columnFamily"), Bytes.toBytes("column1"))).thenReturn( 154 Bytes.toBytes("test")); 155 map.setup(ctx); 156 map.map(rowKey, result, ctx); 157 } 158 159 /** 160 * Test main method of IndexBuilder 161 */ 162 @Test 163 public void testMainIndexBuilder() throws Exception { 164 PrintStream oldPrintStream = System.err; 165 SecurityManager SECURITY_MANAGER = System.getSecurityManager(); 166 LauncherSecurityManager newSecurityManager= new LauncherSecurityManager(); 167 System.setSecurityManager(newSecurityManager); 168 ByteArrayOutputStream data = new ByteArrayOutputStream(); 169 String[] args = {}; 170 System.setErr(new PrintStream(data)); 171 try { 172 System.setErr(new PrintStream(data)); 173 try { 174 IndexBuilder.main(args); 175 fail("should be SecurityException"); 176 } catch (SecurityException e) { 177 assertEquals(-1, newSecurityManager.getExitCode()); 178 assertTrue(data.toString().contains("arguments supplied, required: 3")); 179 assertTrue(data.toString().contains( 180 "Usage: IndexBuilder <TABLE_NAME> <COLUMN_FAMILY> <ATTR> [<ATTR> ...]")); 181 } 182 183 } finally { 184 System.setErr(oldPrintStream); 185 System.setSecurityManager(SECURITY_MANAGER); 186 } 187 188 } 189}