Added: incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockServer.java URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockServer.java?rev=1365722&view=auto ============================================================================== --- incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockServer.java (added) +++ incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockServer.java Wed Jul 25 20:29:44 2012 @@ -0,0 +1,39 @@ +/* + * 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.hcatalog.templeton.mock; + +import org.apache.hcatalog.templeton.Server; + +/* + * Test that the server code exists. + */ +public class MockServer extends Server { + public String user; + public MockServer() { + execService = new MockExecService(); + resetUser(); + } + + public void resetUser() { + user = System.getenv("USER"); + } + + public String getUser() { + return user; + } +}
Added: incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockUriInfo.java URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockUriInfo.java?rev=1365722&view=auto ============================================================================== --- incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockUriInfo.java (added) +++ incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/mock/MockUriInfo.java Wed Jul 25 20:29:44 2012 @@ -0,0 +1,138 @@ +/* + * 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.hcatalog.templeton.mock; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; + +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; + +public class MockUriInfo implements UriInfo { + + @Override + public URI getAbsolutePath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public UriBuilder getAbsolutePathBuilder() { + // TODO Auto-generated method stub + return null; + } + + @Override + public URI getBaseUri() { + try { + return new URI("http://fakeuri/templeton"); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public UriBuilder getBaseUriBuilder() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<Object> getMatchedResources() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<String> getMatchedURIs() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<String> getMatchedURIs(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPath(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap<String, String> getPathParameters() { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap<String, String> getPathParameters(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<PathSegment> getPathSegments() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List<PathSegment> getPathSegments(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap<String, String> getQueryParameters() { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap<String, String> getQueryParameters(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public URI getRequestUri() { + // TODO Auto-generated method stub + return null; + } + + @Override + public UriBuilder getRequestUriBuilder() { + // TODO Auto-generated method stub + return null; + } + +} Added: incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTempletonUtils.java URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTempletonUtils.java?rev=1365722&view=auto ============================================================================== --- incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTempletonUtils.java (added) +++ incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTempletonUtils.java Wed Jul 25 20:29:44 2012 @@ -0,0 +1,209 @@ +/* + * 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.hcatalog.templeton.tool; + +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.util.StringUtils; +import org.apache.hcatalog.templeton.tool.TempletonUtils; +import org.junit.Test; + +public class TestTempletonUtils { + public static final String[] CONTROLLER_LINES = { + "2011-12-15 18:12:21,758 [main] INFO org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher - More information at: http://localhost:50030/jobdetails.jsp?jobid=job_201112140012_0047", + "2011-12-15 18:12:46,907 [main] INFO org.apache.pig.tools.pigstats.SimplePigStats - Script Statistics: " + }; + + @Test + public void testIssetString() { + assertFalse(TempletonUtils.isset((String)null)); + assertFalse(TempletonUtils.isset("")); + assertTrue(TempletonUtils.isset("hello")); + } + + @Test + public void testIssetTArray() { + assertFalse(TempletonUtils.isset((Long[]) null)); + assertFalse(TempletonUtils.isset(new String[0])); + String[] parts = new String("hello.world").split("\\."); + assertTrue(TempletonUtils.isset(parts)); + } + + @Test + public void testPrintTaggedJobID() { + //JobID job = new JobID(); + // TODO -- capture System.out? + } + + + @Test + public void testExtractPercentComplete() { + assertNull(TempletonUtils.extractPercentComplete("fred")); + for (String line : CONTROLLER_LINES) + assertNull(TempletonUtils.extractPercentComplete(line)); + + String fifty = "2011-12-15 18:12:36,333 [main] INFO org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher - 50% complete"; + assertEquals("50% complete", TempletonUtils.extractPercentComplete(fifty)); + } + + @Test + public void testEncodeArray() { + assertEquals(null, TempletonUtils.encodeArray((String []) null)); + String[] tmp = new String[0]; + assertTrue(TempletonUtils.encodeArray(new String[0]).length() == 0); + tmp = new String[3]; + tmp[0] = "fred"; + tmp[1] = null; + tmp[2] = "peter,lisa,, barney"; + assertEquals("fred,,peter" + + StringUtils.ESCAPE_CHAR + ",lisa" + StringUtils.ESCAPE_CHAR + "," + + StringUtils.ESCAPE_CHAR + ", barney", + TempletonUtils.encodeArray(tmp)); + } + + @Test + public void testDecodeArray() { + assertTrue(TempletonUtils.encodeArray((String[]) null) == null); + String[] tmp = new String[3]; + tmp[0] = "fred"; + tmp[1] = null; + tmp[2] = "peter,lisa,, barney"; + String[] tmp2 = TempletonUtils.decodeArray(TempletonUtils.encodeArray(tmp)); + try { + for (int i=0; i< tmp.length; i++) { + assertEquals((String) tmp[i], (String)tmp2[i]); + } + } catch (Exception e) { + fail("Arrays were not equal" + e.getMessage()); + } + } + + @Test + public void testHadoopFsPath() { + try { + TempletonUtils.hadoopFsPath(null, null, null); + TempletonUtils.hadoopFsPath("/tmp", null, null); + TempletonUtils.hadoopFsPath("/tmp", new Configuration(), null); + } catch (FileNotFoundException e) { + fail("Couldn't find /tmp"); + } catch (Exception e) { + // This is our problem -- it means the configuration was wrong. + e.printStackTrace(); + } + try { + TempletonUtils.hadoopFsPath("/scoobydoo/teddybear", + new Configuration(), null); + fail("Should not have found /scoobydoo/teddybear"); + } catch (FileNotFoundException e) { + // Should go here. + } catch (Exception e) { + // This is our problem -- it means the configuration was wrong. + e.printStackTrace(); + } + } + + @Test + public void testHadoopFsFilename() { + try { + assertEquals(null, TempletonUtils.hadoopFsFilename(null, null, null)); + assertEquals(null, TempletonUtils.hadoopFsFilename("/tmp", null, null)); + assertEquals("file:/tmp", + TempletonUtils.hadoopFsFilename("/tmp", + new Configuration(), + null)); + } catch (FileNotFoundException e) { + fail("Couldn't find name for /tmp"); + } catch (Exception e) { + // Something else is wrong + e.printStackTrace(); + } + try { + TempletonUtils.hadoopFsFilename("/scoobydoo/teddybear", + new Configuration(), null); + fail("Should not have found /scoobydoo/teddybear"); + } catch (FileNotFoundException e) { + // Should go here. + } catch (Exception e) { + // Something else is wrong. + e.printStackTrace(); + } + } + + @Test + public void testHadoopFsListAsArray() { + try { + assertTrue(TempletonUtils.hadoopFsListAsArray(null, null, null) == null); + assertTrue(TempletonUtils.hadoopFsListAsArray("/tmp, /usr", + null, null) == null); + String[] tmp2 + = TempletonUtils.hadoopFsListAsArray("/tmp,/usr", + new Configuration(), null); + assertEquals("file:/tmp", tmp2[0]); + assertEquals("file:/usr", tmp2[1]); + } catch (FileNotFoundException e) { + fail("Couldn't find name for /tmp"); + } catch (Exception e) { + // Something else is wrong + e.printStackTrace(); + } + try { + TempletonUtils.hadoopFsListAsArray("/scoobydoo/teddybear,joe", + new Configuration(), + null); + fail("Should not have found /scoobydoo/teddybear"); + } catch (FileNotFoundException e) { + // Should go here. + } catch (Exception e) { + // Something else is wrong. + e.printStackTrace(); + } + } + + @Test + public void testHadoopFsListAsString() { + try { + assertTrue(TempletonUtils.hadoopFsListAsString(null, null, null) == null); + assertTrue(TempletonUtils.hadoopFsListAsString("/tmp,/usr", + null, null) == null); + assertEquals("file:/tmp,file:/usr", TempletonUtils.hadoopFsListAsString + ("/tmp,/usr", new Configuration(), null)); + } catch (FileNotFoundException e) { + fail("Couldn't find name for /tmp"); + } catch (Exception e) { + // Something else is wrong + e.printStackTrace(); + } + try { + TempletonUtils.hadoopFsListAsString("/scoobydoo/teddybear,joe", + new Configuration(), + null); + fail("Should not have found /scoobydoo/teddybear"); + } catch (FileNotFoundException e) { + // Should go here. + } catch (Exception e) { + // Something else is wrong. + e.printStackTrace(); + } + } + +} Added: incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTrivialExecService.java URL: http://svn.apache.org/viewvc/incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTrivialExecService.java?rev=1365722&view=auto ============================================================================== --- incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTrivialExecService.java (added) +++ incubator/hcatalog/trunk/webhcat/svr/src/test/java/org/apache/hcatalog/templeton/tool/TestTrivialExecService.java Wed Jul 25 20:29:44 2012 @@ -0,0 +1,68 @@ +/* + * 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.hcatalog.templeton.tool; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import org.junit.Test; + +public class TestTrivialExecService { + @Test + public void test() { + ArrayList<String> list = new ArrayList<String>(); + list.add("echo"); + list.add("success"); + BufferedReader out = null; + BufferedReader err = null; + try { + Process process = TrivialExecService.getInstance() + .run(list, + new ArrayList<String>(), + new HashMap<String, String>()); + out = new BufferedReader(new InputStreamReader( + process.getInputStream())); + err = new BufferedReader(new InputStreamReader( + process.getErrorStream())); + assertEquals("success", out.readLine()); + out.close(); + String line; + while ((line = err.readLine()) != null) { + fail(line); + } + process.waitFor(); + } catch (Exception e) { + e.printStackTrace(); + fail("Process caused exception."); + } finally { + try { + out.close(); + } catch (Exception ex) { + // Whatever. + } + try { + err.close(); + } catch (Exception ex) { + // Whatever + } + } + } +}
