1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.hadoop.hbase.mapreduce;
17
18 import org.apache.hadoop.conf.Configuration;
19 import org.apache.hadoop.fs.Path;
20 import org.apache.hadoop.hbase.HBaseTestingUtility;
21 import org.apache.hadoop.hbase.LargeTests;
22 import org.apache.hadoop.hbase.client.Put;
23 import org.apache.hadoop.hbase.client.Result;
24 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
25 import org.apache.hadoop.hbase.mapreduce.IndexBuilder.Map;
26 import org.apache.hadoop.hbase.mapreduce.SampleUploader.Uploader;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.apache.hadoop.hbase.util.LauncherSecurityManager;
29 import org.apache.hadoop.io.LongWritable;
30 import org.apache.hadoop.io.Text;
31 import org.apache.hadoop.mapreduce.Job;
32 import org.apache.hadoop.mapreduce.Mapper;
33 import org.apache.hadoop.mapreduce.Mapper.Context;
34 import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39
40 import java.io.ByteArrayOutputStream;
41 import java.io.PrintStream;
42
43 import static org.junit.Assert.*;
44 import static org.mockito.Mockito.*;
45
46 @Category(LargeTests.class)
47 public class TestMapReduceExamples {
48 private static HBaseTestingUtility util = new HBaseTestingUtility();
49
50
51
52
53
54 @SuppressWarnings("unchecked")
55 @Test
56 public void testSampleUploader() throws Exception {
57
58 Configuration configuration = new Configuration();
59 Uploader uploader = new Uploader();
60 Mapper<LongWritable, Text, ImmutableBytesWritable, Put>.Context ctx = mock(Context.class);
61 doAnswer(new Answer<Void>() {
62
63 @Override
64 public Void answer(InvocationOnMock invocation) throws Throwable {
65 ImmutableBytesWritable writer = (ImmutableBytesWritable) invocation.getArguments()[0];
66 Put put = (Put) invocation.getArguments()[1];
67 assertEquals("row", Bytes.toString(writer.get()));
68 assertEquals("row", Bytes.toString(put.getRow()));
69 return null;
70 }
71 }).when(ctx).write(any(ImmutableBytesWritable.class), any(Put.class));
72
73 uploader.map(null, new Text("row,family,qualifier,value"), ctx);
74
75 Path dir = util.getDataTestDirOnTestFS("testSampleUploader");
76
77 String[] args = { dir.toString(), "simpleTable" };
78 Job job = SampleUploader.configureJob(configuration, args);
79 assertEquals(SequenceFileInputFormat.class, job.getInputFormatClass());
80
81 }
82
83
84
85
86 @Test
87 public void testMainSampleUploader() throws Exception {
88 PrintStream oldPrintStream = System.err;
89 SecurityManager SECURITY_MANAGER = System.getSecurityManager();
90 LauncherSecurityManager newSecurityManager= new LauncherSecurityManager();
91 System.setSecurityManager(newSecurityManager);
92 ByteArrayOutputStream data = new ByteArrayOutputStream();
93 String[] args = {};
94 System.setErr(new PrintStream(data));
95 try {
96 System.setErr(new PrintStream(data));
97
98 try {
99 SampleUploader.main(args);
100 fail("should be SecurityException");
101 } catch (SecurityException e) {
102 assertEquals(-1, newSecurityManager.getExitCode());
103 assertTrue(data.toString().contains("Wrong number of arguments:"));
104 assertTrue(data.toString().contains("Usage: SampleUploader <input> <tablename>"));
105 }
106
107 } finally {
108 System.setErr(oldPrintStream);
109 System.setSecurityManager(SECURITY_MANAGER);
110 }
111
112 }
113
114
115
116
117 @SuppressWarnings("unchecked")
118 @Test
119 public void testIndexBuilder() throws Exception {
120 Configuration configuration = new Configuration();
121 String[] args = { "tableName", "columnFamily", "column1", "column2" };
122 IndexBuilder.configureJob(configuration, args);
123 assertEquals("tableName", configuration.get("index.tablename"));
124 assertEquals("tableName", configuration.get(TableInputFormat.INPUT_TABLE));
125 assertEquals("column1,column2", configuration.get("index.fields"));
126
127 Map map = new Map();
128 ImmutableBytesWritable rowKey = new ImmutableBytesWritable(Bytes.toBytes("test"));
129 Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put>.Context ctx =
130 mock(Context.class);
131 when(ctx.getConfiguration()).thenReturn(configuration);
132 doAnswer(new Answer<Void>() {
133
134 @Override
135 public Void answer(InvocationOnMock invocation) throws Throwable {
136 ImmutableBytesWritable writer = (ImmutableBytesWritable) invocation.getArguments()[0];
137 Put put = (Put) invocation.getArguments()[1];
138 assertEquals("tableName-column1", Bytes.toString(writer.get()));
139 assertEquals("test", Bytes.toString(put.getRow()));
140 return null;
141 }
142 }).when(ctx).write(any(ImmutableBytesWritable.class), any(Put.class));
143 Result result = mock(Result.class);
144 when(result.getValue(Bytes.toBytes("columnFamily"), Bytes.toBytes("column1"))).thenReturn(
145 Bytes.toBytes("test"));
146 map.setup(ctx);
147 map.map(rowKey, result, ctx);
148 }
149
150
151
152
153 @Test
154 public void testMainIndexBuilder() throws Exception {
155 PrintStream oldPrintStream = System.err;
156 SecurityManager SECURITY_MANAGER = System.getSecurityManager();
157 LauncherSecurityManager newSecurityManager= new LauncherSecurityManager();
158 System.setSecurityManager(newSecurityManager);
159 ByteArrayOutputStream data = new ByteArrayOutputStream();
160 String[] args = {};
161 System.setErr(new PrintStream(data));
162 try {
163 System.setErr(new PrintStream(data));
164 try {
165 IndexBuilder.main(args);
166 fail("should be SecurityException");
167 } catch (SecurityException e) {
168 assertEquals(-1, newSecurityManager.getExitCode());
169 assertTrue(data.toString().contains("arguments supplied, required: 3"));
170 assertTrue(data.toString().contains(
171 "Usage: IndexBuilder <TABLE_NAME> <COLUMN_FAMILY> <ATTR> [<ATTR> ...]"));
172 }
173
174 } finally {
175 System.setErr(oldPrintStream);
176 System.setSecurityManager(SECURITY_MANAGER);
177 }
178
179 }
180 }