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