You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ch...@apache.org on 2012/11/08 18:06:50 UTC
svn commit: r1407167 [2/10] - in /pig/trunk: ./ test/org/apache/pig/test/
test/org/apache/pig/test/pigunit/
Modified: pig/trunk/test/org/apache/pig/test/TestBloom.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestBloom.java?rev=1407167&r1=1407166&r2=1407167&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestBloom.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestBloom.java Thu Nov 8 17:06:46 2012
@@ -1,14 +1,12 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * 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
- *
+ * 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.
@@ -17,9 +15,10 @@
*/
package org.apache.pig.test;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
-import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.builtin.Bloom;
import org.apache.pig.builtin.BuildBloom;
import org.apache.pig.data.BagFactory;
@@ -27,31 +26,33 @@ import org.apache.pig.data.DataBag;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
+import org.junit.Test;
/**
* This class unit tests the built in UDFs BuildBloom and Bloom.
*/
-public class TestBloom extends junit.framework.TestCase {
-
+public class TestBloom {
static class TestBuildBloom extends BuildBloom {
-
TestBuildBloom(String numElements, String desiredFalsePositive) {
super("jenkins", numElements, desiredFalsePositive);
}
- int getSize() { return vSize; }
- int getNumHash() { return numHash; }
+ int getSize() {
+ return vSize;
+ }
+ int getNumHash() {
+ return numHash;
+ }
}
@Test
public void testSizeCalc() throws Exception {
-
TestBuildBloom tbb = new TestBuildBloom("1000", "0.01");
assertEquals(9585, tbb.getSize());
assertEquals(6, tbb.getNumHash());
tbb = new TestBuildBloom("1000000", "0.01");
- assertEquals(9585058 , tbb.getSize());
+ assertEquals(9585058, tbb.getSize());
assertEquals(6, tbb.getNumHash());
tbb = new TestBuildBloom("1000", "0.0001");
assertEquals(19170, tbb.getSize());
@@ -61,19 +62,17 @@ public class TestBloom extends junit.fra
assertEquals(16, tbb.getNumHash());
}
- @Test
+ @Test(expected = RuntimeException.class)
public void testBadHash() throws Exception {
String size = "100";
String numHash = "3";
String hashFunc = "nosuchhash";
- boolean caughtException = false;
try {
BuildBloom bb = new BuildBloom(hashFunc, "fixed", size, numHash);
} catch (RuntimeException re) {
assertTrue(re.getMessage().contains("Unknown hash type"));
- caughtException = true;
+ throw re;
}
- assertTrue(caughtException);
}
@Test
@@ -83,11 +82,11 @@ public class TestBloom extends junit.fra
String hashFunc = "JENKINS_HASH";
BuildBloom bb = new BuildBloom(hashFunc, "fixed", size, numHash);
assertEquals("org.apache.pig.builtin.BuildBloom$Initial",
- bb.getInitial());
+ bb.getInitial());
assertEquals("org.apache.pig.builtin.BuildBloom$Intermediate",
- bb.getIntermed());
+ bb.getIntermed());
assertEquals("org.apache.pig.builtin.BuildBloom$Final",
- bb.getFinal());
+ bb.getFinal());
}
@Test
@@ -103,9 +102,9 @@ public class TestBloom extends junit.fra
DataBag b = bf.newDefaultBag();
b.add(t);
Tuple input = tf.newTuple(b);
-
+
BuildBloom.Initial map =
- new BuildBloom.Initial(hashFunc, "fixed", size, numHash);
+ new BuildBloom.Initial(hashFunc, "fixed", size, numHash);
t = map.exec(input);
Bloom bloom = new Bloom("bla");
@@ -140,13 +139,13 @@ public class TestBloom extends junit.fra
mapBag.add(t);
Tuple input = tf.newTuple(mapBag);
BuildBloom.Initial map =
- new BuildBloom.Initial(hashFunc, "fixed", size, numHash);
+ new BuildBloom.Initial(hashFunc, "fixed", size, numHash);
combinerBag.add(map.exec(input));
}
Tuple t = tf.newTuple(1);
t.set(0, combinerBag);
BuildBloom.Intermediate combiner =
- new BuildBloom.Intermediate(hashFunc, "fixed", size, numHash);
+ new BuildBloom.Intermediate(hashFunc, "fixed", size, numHash);
t = combiner.exec(t);
Bloom bloom = new Bloom("bla");
@@ -185,20 +184,20 @@ public class TestBloom extends junit.fra
mapBag.add(t);
Tuple input = tf.newTuple(mapBag);
BuildBloom.Initial map =
- new BuildBloom.Initial(hashFunc, "fixed", size, numHash);
+ new BuildBloom.Initial(hashFunc, "fixed", size, numHash);
combinerBag.add(map.exec(input));
}
Tuple t = tf.newTuple(1);
t.set(0, combinerBag);
BuildBloom.Intermediate combiner =
- new BuildBloom.Intermediate(hashFunc, "fixed", size, numHash);
+ new BuildBloom.Intermediate(hashFunc, "fixed", size, numHash);
reducerBag.add(combiner.exec(t));
}
Tuple t = tf.newTuple(1);
t.set(0, reducerBag);
BuildBloom.Final reducer =
- new BuildBloom.Final(hashFunc, "fixed", size, numHash);
+ new BuildBloom.Final(hashFunc, "fixed", size, numHash);
DataByteArray dba = reducer.exec(t);
Bloom bloom = new Bloom("bla");
@@ -230,9 +229,9 @@ public class TestBloom extends junit.fra
BagFactory bf = BagFactory.getInstance();
String[][] strs = {
- { "fred", "joe", "bob" },
- { "mary", "sally", "jane" },
- { "fido", "spot", "fluffly" }};
+ { "fred", "joe", "bob" },
+ { "mary", "sally", "jane" },
+ { "fido", "spot", "fluffly" } };
DataBag reducerBag = bf.newDefaultBag();
for (int i = 0; i < 3; i++) { // combiner loop
@@ -245,22 +244,22 @@ public class TestBloom extends junit.fra
mapBag.add(t);
Tuple input = tf.newTuple(mapBag);
BuildBloom.Initial map =
- new BuildBloom.Initial(hashFunc, numElements,
- falsePositive);
+ new BuildBloom.Initial(hashFunc, numElements,
+ falsePositive);
combinerBag.add(map.exec(input));
}
Tuple t = tf.newTuple(1);
t.set(0, combinerBag);
BuildBloom.Intermediate combiner =
- new BuildBloom.Intermediate(hashFunc, numElements,
- falsePositive);
+ new BuildBloom.Intermediate(hashFunc, numElements,
+ falsePositive);
reducerBag.add(combiner.exec(t));
}
Tuple t = tf.newTuple(1);
t.set(0, reducerBag);
BuildBloom.Final reducer =
- new BuildBloom.Final(hashFunc, numElements, falsePositive);
+ new BuildBloom.Final(hashFunc, numElements, falsePositive);
DataByteArray dba = reducer.exec(t);
Bloom bloom = new Bloom("bla");
@@ -283,4 +282,5 @@ public class TestBloom extends junit.fra
t1.set(1, "ichabod");
assertFalse(bloom.exec(t1));
}
- }}
+ }
+}
Modified: pig/trunk/test/org/apache/pig/test/TestBoolean.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestBoolean.java?rev=1407167&r1=1407166&r2=1407167&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestBoolean.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestBoolean.java Thu Nov 8 17:06:46 2012
@@ -1,14 +1,12 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * 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
- *
+ * 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.
@@ -17,26 +15,29 @@
*/
package org.apache.pig.test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
-import java.util.Map;
import java.util.Random;
-import junit.framework.TestCase;
-
import org.apache.pig.backend.executionengine.ExecException;
-import org.apache.pig.data.DataType;
-import org.apache.pig.impl.plan.OperatorKey;
import org.apache.pig.backend.hadoop.executionengine.physicalLayer.POStatus;
import org.apache.pig.backend.hadoop.executionengine.physicalLayer.Result;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.BinaryExpressionOperator;
import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.ConstantExpression;
-import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.*;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.POAnd;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.PONot;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.POOr;
+import org.apache.pig.backend.hadoop.executionengine.physicalLayer.expressionOperators.UnaryExpressionOperator;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.plan.OperatorKey;
import org.junit.Before;
import org.junit.Test;
-
-public class TestBoolean extends TestCase{
-
- Random r = new Random();
+public class TestBoolean {
+ Random r = new Random(42L);
ConstantExpression lt, rt;
BinaryExpressionOperator bop;
UnaryExpressionOperator uop;
@@ -44,9 +45,9 @@ public class TestBoolean extends TestCas
@Before
public void setUp() throws Exception {
- lt = new ConstantExpression(new OperatorKey("",r.nextLong()));
+ lt = new ConstantExpression(new OperatorKey("", r.nextLong()));
lt.setResultType(DataType.BOOLEAN);
- rt = new ConstantExpression(new OperatorKey("",r.nextLong()));
+ rt = new ConstantExpression(new OperatorKey("", r.nextLong()));
rt.setResultType(DataType.BOOLEAN);
}
@@ -67,197 +68,195 @@ public class TestBoolean extends TestCas
uop.setExpr(lt);
}
-
@Test
public void testAndNull() throws ExecException {
- setupAnd();
- Boolean[] testWith = new Boolean[] { false, true, null};
-
- // truth table for AND
+ setupAnd();
+ Boolean[] testWith = new Boolean[] { false, true, null };
+
+ // truth table for AND
// t = true, n = null, f = false
- // AND t n f
- // t t n f
- // n n n f
- // f f f f
-
- // test with first operand set to null
- for (int i = 0; i < testWith.length; i++) {
- lt.setValue(null);
- rt.setValue(testWith[i]);
- Result res = bop.getNext(dummy);
- assertEquals(POStatus.STATUS_OK, res.returnStatus);
- if(testWith[i] != null && testWith[i] == false) {
- // if rhs is false, result is false
- assertEquals(new Boolean(false), (Boolean) res.result);
- } else {
- // else result is null
- assertEquals(null, (Boolean)res.result);
- }
- }
-
- // test with second operand set to null
- for (int i = 0; i < testWith.length; i++) {
- lt.setValue(testWith[i]);
- rt.setValue(null);
- Result res = bop.getNext(dummy);
- assertEquals(POStatus.STATUS_OK, res.returnStatus);
- if(testWith[i] != null && testWith[i] == false) {
+ // AND t n f
+ // t t n f
+ // n n n f
+ // f f f f
+
+ // test with first operand set to null
+ for (int i = 0; i < testWith.length; i++) {
+ lt.setValue(null);
+ rt.setValue(testWith[i]);
+ Result res = bop.getNext(dummy);
+ assertEquals(POStatus.STATUS_OK, res.returnStatus);
+ if (testWith[i] != null && testWith[i] == false) {
+ // if rhs is false, result is false
+ assertFalse((Boolean) res.result);
+ } else {
+ // else result is null
+ assertNull(res.result);
+ }
+ }
+
+ // test with second operand set to null
+ for (int i = 0; i < testWith.length; i++) {
+ lt.setValue(testWith[i]);
+ rt.setValue(null);
+ Result res = bop.getNext(dummy);
+ assertEquals(POStatus.STATUS_OK, res.returnStatus);
+ if (testWith[i] != null && testWith[i] == false) {
// if lhs is false, result is false
- assertEquals(new Boolean(false), (Boolean) res.result);
+ assertFalse((Boolean) res.result);
} else {
// else result is null
- assertEquals(null, (Boolean)res.result);
+ assertNull(res.result);
}
- }
+ }
}
-
+
@Test
public void testOrNull() throws ExecException {
- setupOr();
- Boolean[] testWith = new Boolean[] { false, true, null};
- // truth table for OR
+ setupOr();
+ Boolean[] testWith = new Boolean[] { false, true, null };
+ // truth table for OR
// t = true, n = null, f = false
- // OR t n f
- // t t t t
- // n t n n
- // f t n f
-
- // test with first operand set to null
- for (int i = 0; i < testWith.length; i++) {
- lt.setValue(null);
- rt.setValue(testWith[i]);
- Result res = bop.getNext(dummy);
- assertEquals(POStatus.STATUS_OK, res.returnStatus);
- if(testWith[i] != null && testWith[i] == true) {
+ // OR t n f
+ // t t t t
+ // n t n n
+ // f t n f
+
+ // test with first operand set to null
+ for (int i = 0; i < testWith.length; i++) {
+ lt.setValue(null);
+ rt.setValue(testWith[i]);
+ Result res = bop.getNext(dummy);
+ assertEquals(POStatus.STATUS_OK, res.returnStatus);
+ if (testWith[i] != null && testWith[i] == true) {
// if rhs is true, result is true
- assertEquals(new Boolean(true), (Boolean) res.result);
+ assertTrue((Boolean) res.result);
} else {
// else result is null
- assertEquals(null, (Boolean)res.result);
+ assertNull(res.result);
}
- }
-
- // test with second operand set to null
- for (int i = 0; i < testWith.length; i++) {
- lt.setValue(testWith[i]);
- rt.setValue(null);
- Result res = bop.getNext(dummy);
- assertEquals(POStatus.STATUS_OK, res.returnStatus);
- if(testWith[i] != null && testWith[i] == true) {
+ }
+
+ // test with second operand set to null
+ for (int i = 0; i < testWith.length; i++) {
+ lt.setValue(testWith[i]);
+ rt.setValue(null);
+ Result res = bop.getNext(dummy);
+ assertEquals(POStatus.STATUS_OK, res.returnStatus);
+ if (testWith[i] != null && testWith[i] == true) {
// if lhs is true, result is true
- assertEquals(new Boolean(true), (Boolean) res.result);
+ assertTrue((Boolean) res.result);
} else {
// else result is null
- assertEquals(null, (Boolean)res.result);
+ assertNull(res.result);
}
- }
+ }
}
-
+
@Test
- public void testAndFirstFalse() throws ExecException{
+ public void testAndFirstFalse() throws ExecException {
setupAnd();
lt.setValue(new Boolean(false));
rt.setValue(new Boolean(true));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertFalse((Boolean)res.result);
+ assertFalse((Boolean) res.result);
}
@Test
- public void testAndSecondFalse() throws ExecException{
+ public void testAndSecondFalse() throws ExecException {
setupAnd();
lt.setValue(new Boolean(true));
rt.setValue(new Boolean(false));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertFalse((Boolean)res.result);
+ assertFalse((Boolean) res.result);
}
@Test
- public void testAndBothFalse() throws ExecException{
+ public void testAndBothFalse() throws ExecException {
setupAnd();
lt.setValue(new Boolean(false));
rt.setValue(new Boolean(false));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertFalse((Boolean)res.result);
+ assertFalse((Boolean) res.result);
}
@Test
- public void testAndTrue() throws ExecException{
+ public void testAndTrue() throws ExecException {
setupAnd();
lt.setValue(new Boolean(true));
rt.setValue(new Boolean(true));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertTrue((Boolean)res.result);
+ assertTrue((Boolean) res.result);
}
@Test
- public void testOrFirstFalse() throws ExecException{
+ public void testOrFirstFalse() throws ExecException {
setupOr();
lt.setValue(new Boolean(false));
rt.setValue(new Boolean(true));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertTrue((Boolean)res.result);
+ assertTrue((Boolean) res.result);
}
@Test
- public void testOrSecondFalse() throws ExecException{
+ public void testOrSecondFalse() throws ExecException {
setupOr();
lt.setValue(new Boolean(true));
rt.setValue(new Boolean(false));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertTrue((Boolean)res.result);
+ assertTrue((Boolean) res.result);
}
@Test
- public void testOrBothFalse() throws ExecException{
+ public void testOrBothFalse() throws ExecException {
setupOr();
lt.setValue(new Boolean(false));
rt.setValue(new Boolean(false));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertFalse((Boolean)res.result);
+ assertFalse((Boolean) res.result);
}
@Test
- public void testOrTrue() throws ExecException{
+ public void testOrTrue() throws ExecException {
setupOr();
lt.setValue(new Boolean(true));
rt.setValue(new Boolean(true));
Result res = bop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertTrue((Boolean)res.result);
+ assertTrue((Boolean) res.result);
}
@Test
- public void testNotTrue() throws ExecException{
+ public void testNotTrue() throws ExecException {
setupNot();
lt.setValue(new Boolean(true));
Result res = uop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertFalse((Boolean)res.result);
+ assertFalse((Boolean) res.result);
}
@Test
- public void testNotFalse() throws ExecException{
+ public void testNotFalse() throws ExecException {
setupNot();
lt.setValue(new Boolean(false));
Result res = uop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertTrue((Boolean)res.result);
+ assertTrue((Boolean) res.result);
}
-
+
@Test
- public void testNotNull() throws ExecException{
+ public void testNotNull() throws ExecException {
setupNot();
lt.setValue(null);
Result res = uop.getNext(dummy);
assertEquals(POStatus.STATUS_OK, res.returnStatus);
- assertEquals(null, (Boolean)res.result);
+ assertNull(res.result);
}
-}
-
+}
\ No newline at end of file
Modified: pig/trunk/test/org/apache/pig/test/TestCharArrayToNumeric.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestCharArrayToNumeric.java?rev=1407167&r1=1407166&r2=1407167&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestCharArrayToNumeric.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestCharArrayToNumeric.java Thu Nov 8 17:06:46 2012
@@ -1,14 +1,12 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * 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
- *
+ * 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.
@@ -18,10 +16,10 @@
package org.apache.pig.test;
-import java.io.File;
-import java.io.FileOutputStream;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
import java.io.IOException;
-import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -29,8 +27,6 @@ import java.util.List;
import java.util.Map;
import java.util.Random;
-import junit.framework.TestCase;
-
import org.apache.pig.ExecType;
import org.apache.pig.PigServer;
import org.apache.pig.backend.executionengine.ExecException;
@@ -46,261 +42,236 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.*;
-
public class TestCharArrayToNumeric {
-
- private Double dummyDouble = null;
-
- private Float dummyFloat = null;
-
- private Long dummyLong = null;
-
- private Integer dummyInteger = null;
-
- private Double MaxDouble = Double.MIN_VALUE;
-
- private Double MinDouble = Double.MIN_VALUE;
-
- private Float MaxFloat = Float.MAX_VALUE;
-
- private Float MinFloat = Float.MIN_VALUE;
-
- private Long MaxLong = Long.MAX_VALUE;
-
- private Long MinLong = Long.MIN_VALUE;
-
- private Integer MaxInteger = Integer.MAX_VALUE;
-
- private Integer MinInteger = Integer.MIN_VALUE;
-
- static MiniCluster cluster = MiniCluster.buildCluster();
- PigServer pig;
-
-
- @Before
- public void setUp() throws Exception {
- pig = new PigServer(ExecType.MAPREDUCE, cluster.getProperties());
- }
-
- @AfterClass
- public static void oneTimeTearDown() throws Exception {
- cluster.shutDown();
- }
-
- public static OperatorKey newOperatorKey() {
- long newId = NodeIdGenerator.getGenerator().getNextNodeId("scope");
- return new OperatorKey("scope", newId);
- }
-
- @Test
- public void testCast() throws ExecException {
-
- POCast cast = new POCast(newOperatorKey(), -1);
- POProject proj = new POProject(newOperatorKey(), -1, 0);
- proj.setResultType(DataType.CHARARRAY);
- List<PhysicalOperator> inputs = new ArrayList<PhysicalOperator>();
- inputs.add(proj);
- cast.setInputs(inputs);
-
- // cast to double
- String[] items = { "12.0", "-13.2", "0.1f", "1.3e2", "zjf",
- MaxDouble.toString(), MinDouble.toString() };
- Double[] doubleExpected = { 12.0, -13.2, 0.1, 1.3e2, null, MaxDouble,
- MinDouble };
- for (int i = 0; i < items.length; ++i) {
- Tuple tuple = TupleFactory.getInstance().newTuple(1);
- tuple.set(0, items[i]);
- proj.attachInput(tuple);
- Double actual = (Double) cast.getNext(dummyDouble).result;
- if (doubleExpected[i] != null) {
- assertEquals(doubleExpected[i], actual, 1e-6);
- } else {
- assertNull(actual);
- }
- }
-
- // cast to float
- items = new String[] { "12.0", "-13.2", "0.1f", "1.3e2",
- MaxFloat.toString(), MinFloat.toString(), "zjf" };
- Float[] floatExpected = { 12.0f, -13.2f, 0.1f, 1.3e2f, MaxFloat,
- MinFloat, null };
- for (int i = 0; i < items.length; ++i) {
- Tuple tuple = TupleFactory.getInstance().newTuple(1);
- tuple.set(0, items[i]);
- proj.attachInput(tuple);
- Float actual = (Float) cast.getNext(dummyFloat).result;
- if (floatExpected[i] != null) {
- assertEquals(floatExpected[i], actual, 1e-6);
- } else {
- assertNull(actual);
- }
- }
-
- // cast to long
- items = new String[] { "1", "-1", "12.2", "12.8", MaxLong.toString(),
- MinLong.toString(), "df1.2" };
- Long[] longExpected = { 1L, -1L, 12L, 12L, MaxLong, MinLong, null };
- for (int i = 0; i < items.length; ++i) {
- Tuple tuple = TupleFactory.getInstance().newTuple(1);
- tuple.set(0, items[i]);
- proj.attachInput(tuple);
- Long actual = (Long) cast.getNext(dummyLong).result;
- if (longExpected[i] != null) {
- assertEquals(longExpected[i], actual);
- } else {
- assertNull(actual);
- }
- }
-
- // cast to int
- items = new String[] { "1", "-1", "12.2", "12.8",
- MaxInteger.toString(), MinInteger.toString(), "ff4332" };
- Integer[] intExpected = { 1, -1, 12, 12, MaxInteger, MinInteger, null };
- for (int i = 0; i < items.length; ++i) {
- Tuple tuple = TupleFactory.getInstance().newTuple(1);
- tuple.set(0, items[i]);
- proj.attachInput(tuple);
- Integer actual = (Integer) cast.getNext(dummyInteger).result;
- if (intExpected[i] != null) {
- assertEquals(intExpected[i], actual);
- } else {
- assertNull(actual);
- }
- }
- }
-
- @Test
- public void testCharArray2FloatAndDoubleScript() throws IOException {
-
- // create a input file with format (key,value)
- int size = 100;
- String[] numbers = new String[size + 1];
- Random rand = new Random();
- Map<Integer, Double> map = new HashMap<Integer, Double>();
-
- try {
- for (int i = 0; i < numbers.length; ++i) {
- int key = i;
- double value = rand.nextDouble() * 100;
- numbers[i] = (key + "\t" + value);
- map.put(key, value);
- }
- // append a null at the last line, to test string which can not been
- // cast
- numbers[numbers.length - 1] = (numbers.length + "\t" + "null");
- map.put(numbers.length, null);
- Util.createInputFile(cluster, "pig_jira_893-input1.txt", numbers);
- byte[] numericTypes = new byte[] { DataType.DOUBLE, DataType.FLOAT, };
- for (byte type : numericTypes) {
- pig.registerQuery("A = Load 'pig_jira_893-input1.txt' AS " +
- "(key:int,value:chararray);");
- pig.registerQuery("B = FOREACH A GENERATE key,("
- + DataType.findTypeName(type) + ")value;");
- Iterator<Tuple> iter = pig.openIterator("B");
- while (iter.hasNext()) {
- Tuple tuple = iter.next();
- Integer key = (Integer) tuple.get(0);
- String value = null;
- if (tuple.get(1) != null) {
- value = tuple.get(1).toString();
- }
-
- if (type == DataType.DOUBLE) {
- Double expected = map.get(key);
- if (value != null) {
- assertEquals(expected, (Double)Double.parseDouble(value));
- } else {
- assertEquals(expected, null);
- }
-
- }
- if (type == DataType.FLOAT) {
- Float expected = null;
- if (map.get(key) != null) {
- expected = map.get(key).floatValue();
- }
- if (value != null) {
- assertEquals(expected, (Float)Float.parseFloat(value));
- } else {
- assertEquals(expected, null);
- }
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- fail();
- } finally {
- Util.deleteFile(cluster, "pig_jira_893-input1.txt");
- }
- }
-
- @Test
- public void testCharArrayToIntAndLongScript() throws IOException {
-
- // create a input file with format (key,value)
- int size = 100;
- String[] numbers = new String[size + 1];
- Random rand = new Random();
- Map<Integer, Long> map = new HashMap<Integer, Long>();
- File inputFile = null;
-
- try {
- for (int i = 0; i < numbers.length; ++i) {
- int key = i;
- long value = rand.nextInt(100);
- numbers[i] = (key + "\t" + value);
- map.put(key, value);
- }
- // append a null at the last line, to test string which can not been
- // cast
- numbers[numbers.length - 1] = (numbers.length + "\t" + "null");
- map.put(numbers.length, null);
+ private Double dummyDouble = null;
+ private Float dummyFloat = null;
+ private Long dummyLong = null;
+ private Integer dummyInteger = null;
+ private Double MaxDouble = Double.MIN_VALUE;
+ private Double MinDouble = Double.MIN_VALUE;
+ private Float MaxFloat = Float.MAX_VALUE;
+ private Float MinFloat = Float.MIN_VALUE;
+ private Long MaxLong = Long.MAX_VALUE;
+ private Long MinLong = Long.MIN_VALUE;
+ private Integer MaxInteger = Integer.MAX_VALUE;
+ private Integer MinInteger = Integer.MIN_VALUE;
+
+ static MiniCluster cluster = MiniCluster.buildCluster();
+ PigServer pig;
+
+ @Before
+ public void setUp() throws Exception {
+ pig = new PigServer(ExecType.MAPREDUCE, cluster.getProperties());
+ }
+
+ @AfterClass
+ public static void oneTimeTearDown() throws Exception {
+ cluster.shutDown();
+ }
+
+ public static OperatorKey newOperatorKey() {
+ long newId = NodeIdGenerator.getGenerator().getNextNodeId("scope");
+ return new OperatorKey("scope", newId);
+ }
+
+ @Test
+ public void testCast() throws ExecException {
+ POCast cast = new POCast(newOperatorKey(), -1);
+ POProject proj = new POProject(newOperatorKey(), -1, 0);
+ proj.setResultType(DataType.CHARARRAY);
+ List<PhysicalOperator> inputs = new ArrayList<PhysicalOperator>();
+ inputs.add(proj);
+ cast.setInputs(inputs);
+
+ // cast to double
+ String[] items = { "12.0", "-13.2", "0.1f", "1.3e2", "zjf",
+ MaxDouble.toString(), MinDouble.toString() };
+ Double[] doubleExpected = { 12.0, -13.2, 0.1, 1.3e2, null, MaxDouble,
+ MinDouble };
+ for (int i = 0; i < items.length; ++i) {
+ Tuple tuple = TupleFactory.getInstance().newTuple(1);
+ tuple.set(0, items[i]);
+ proj.attachInput(tuple);
+ Double actual = (Double)cast.getNext(dummyDouble).result;
+ if (doubleExpected[i] != null) {
+ assertEquals(doubleExpected[i], actual, 1e-6);
+ } else {
+ assertNull(actual);
+ }
+ }
+
+ // cast to float
+ items = new String[] { "12.0", "-13.2", "0.1f", "1.3e2",
+ MaxFloat.toString(), MinFloat.toString(), "zjf" };
+ Float[] floatExpected = { 12.0f, -13.2f, 0.1f, 1.3e2f, MaxFloat,
+ MinFloat, null };
+ for (int i = 0; i < items.length; ++i) {
+ Tuple tuple = TupleFactory.getInstance().newTuple(1);
+ tuple.set(0, items[i]);
+ proj.attachInput(tuple);
+ Float actual = (Float)cast.getNext(dummyFloat).result;
+ if (floatExpected[i] != null) {
+ assertEquals(floatExpected[i], actual, 1e-6);
+ } else {
+ assertNull(actual);
+ }
+ }
+
+ // cast to long
+ items = new String[] { "1", "-1", "12.2", "12.8", MaxLong.toString(),
+ MinLong.toString(), "df1.2" };
+ Long[] longExpected = { 1L, -1L, 12L, 12L, MaxLong, MinLong, null };
+ for (int i = 0; i < items.length; ++i) {
+ Tuple tuple = TupleFactory.getInstance().newTuple(1);
+ tuple.set(0, items[i]);
+ proj.attachInput(tuple);
+ Long actual = (Long)cast.getNext(dummyLong).result;
+ if (longExpected[i] != null) {
+ assertEquals(longExpected[i], actual);
+ } else {
+ assertNull(actual);
+ }
+ }
+
+ // cast to int
+ items = new String[] { "1", "-1", "12.2", "12.8",
+ MaxInteger.toString(), MinInteger.toString(), "ff4332" };
+ Integer[] intExpected = { 1, -1, 12, 12, MaxInteger, MinInteger, null };
+ for (int i = 0; i < items.length; ++i) {
+ Tuple tuple = TupleFactory.getInstance().newTuple(1);
+ tuple.set(0, items[i]);
+ proj.attachInput(tuple);
+ Integer actual = (Integer)cast.getNext(dummyInteger).result;
+ if (intExpected[i] != null) {
+ assertEquals(intExpected[i], actual);
+ } else {
+ assertNull(actual);
+ }
+ }
+ }
+
+ @Test
+ public void testCharArray2FloatAndDoubleScript() throws IOException {
+ // create a input file with format (key,value)
+ int size = 100;
+ String[] numbers = new String[size + 1];
+ Random rand = new Random();
+ Map<Integer, Double> map = new HashMap<Integer, Double>();
+
+ try {
+ for (int i = 0; i < numbers.length; ++i) {
+ int key = i;
+ double value = rand.nextDouble() * 100;
+ numbers[i] = (key + "\t" + value);
+ map.put(key, value);
+ }
+ // append a null at the last line, to test string which can not been
+ // cast
+ numbers[numbers.length - 1] = (numbers.length + "\t" + "null");
+ map.put(numbers.length, null);
+ Util.createInputFile(cluster, "pig_jira_893-input1.txt", numbers);
+ byte[] numericTypes = new byte[] { DataType.DOUBLE, DataType.FLOAT, };
+ for (byte type : numericTypes) {
+ pig.registerQuery("A = Load 'pig_jira_893-input1.txt' AS " +
+ "(key:int,value:chararray);");
+ pig.registerQuery("B = FOREACH A GENERATE key,("
+ + DataType.findTypeName(type) + ")value;");
+ Iterator<Tuple> iter = pig.openIterator("B");
+ while (iter.hasNext()) {
+ Tuple tuple = iter.next();
+ Integer key = (Integer)tuple.get(0);
+ String value = null;
+ if (tuple.get(1) != null) {
+ value = tuple.get(1).toString();
+ }
+
+ if (type == DataType.DOUBLE) {
+ Double expected = map.get(key);
+ if (value != null) {
+ assertEquals(expected, (Double)Double.parseDouble(value));
+ } else {
+ assertNull(expected);
+ }
+
+ }
+ if (type == DataType.FLOAT) {
+ Float expected = null;
+ if (map.get(key) != null) {
+ expected = map.get(key).floatValue();
+ }
+ if (value != null) {
+ assertEquals(expected, (Float)Float.parseFloat(value));
+ } else {
+ assertNull(expected);
+ }
+ }
+ }
+ }
+ } finally {
+ Util.deleteFile(cluster, "pig_jira_893-input1.txt");
+ }
+ }
+
+ @Test
+ public void testCharArrayToIntAndLongScript() throws IOException {
+ // create a input file with format (key,value)
+ int size = 100;
+ String[] numbers = new String[size + 1];
+ Random rand = new Random();
+ Map<Integer, Long> map = new HashMap<Integer, Long>();
+
+ try {
+ for (int i = 0; i < numbers.length; ++i) {
+ int key = i;
+ long value = rand.nextInt(100);
+ numbers[i] = (key + "\t" + value);
+ map.put(key, value);
+ }
+ // append a null at the last line, to test string which can not been
+ // cast
+ numbers[numbers.length - 1] = (numbers.length + "\t" + "null");
+ map.put(numbers.length, null);
Util.createInputFile(cluster, "pig_jira_893-input2.txt", numbers);
- byte[] numericTypes = new byte[] { DataType.INTEGER, DataType.LONG, };
- for (byte type : numericTypes) {
- pig.registerQuery("A = Load 'pig_jira_893-input2.txt' AS " +
- "(key:int,value:chararray);");
- pig.registerQuery("B = FOREACH A GENERATE key,("
- + DataType.findTypeName(type) + ")value;");
- Iterator<Tuple> iter = pig.openIterator("B");
- while (iter.hasNext()) {
- Tuple tuple = iter.next();
- Integer key = (Integer) tuple.get(0);
- String value = null;
- if (tuple.get(1) != null) {
- value = tuple.get(1).toString();
- }
-
- if (type == DataType.LONG) {
- Long expected = map.get(key);
- if (value != null) {
- Long actual = Long.parseLong(value);
- assertEquals(expected, actual);
- } else {
- assertEquals(expected, null);
- }
- }
- if (type == DataType.INTEGER) {
- Integer expected = null;
- if (map.get(key) != null) {
- expected = map.get(key).intValue();
- }
- if (value != null) {
- Integer actual = Integer.parseInt(value);
- assertEquals(expected, actual);
- } else {
- assertEquals(expected, null);
- }
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- fail();
- } finally {
- Util.deleteFile(cluster, "pig_jira_893-input2.txt");
- }
- }
+ byte[] numericTypes = new byte[] { DataType.INTEGER, DataType.LONG, };
+ for (byte type : numericTypes) {
+ pig.registerQuery("A = Load 'pig_jira_893-input2.txt' AS " +
+ "(key:int,value:chararray);");
+ pig.registerQuery("B = FOREACH A GENERATE key,("
+ + DataType.findTypeName(type) + ")value;");
+ Iterator<Tuple> iter = pig.openIterator("B");
+ while (iter.hasNext()) {
+ Tuple tuple = iter.next();
+ Integer key = (Integer)tuple.get(0);
+ String value = null;
+ if (tuple.get(1) != null) {
+ value = tuple.get(1).toString();
+ }
+
+ if (type == DataType.LONG) {
+ Long expected = map.get(key);
+ if (value != null) {
+ Long actual = Long.parseLong(value);
+ assertEquals(expected, actual);
+ } else {
+ assertEquals(expected, null);
+ }
+ }
+ if (type == DataType.INTEGER) {
+ Integer expected = null;
+ if (map.get(key) != null) {
+ expected = map.get(key).intValue();
+ }
+ if (value != null) {
+ Integer actual = Integer.parseInt(value);
+ assertEquals(expected, actual);
+ } else {
+ assertNull(expected);
+ }
+ }
+ }
+ }
+ } finally {
+ Util.deleteFile(cluster, "pig_jira_893-input2.txt");
+ }
+ }
}
Modified: pig/trunk/test/org/apache/pig/test/TestCmdLineParser.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestCmdLineParser.java?rev=1407167&r1=1407166&r2=1407167&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestCmdLineParser.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestCmdLineParser.java Thu Nov 8 17:06:46 2012
@@ -1,14 +1,12 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
+ * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
+ * 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
- *
+ * 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.
@@ -17,267 +15,244 @@
*/
package org.apache.pig.test;
-import java.text.ParseException;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
+import java.text.ParseException;
+import org.apache.pig.tools.cmdline.CmdLineParser;
import org.junit.Test;
-import org.apache.pig.tools.cmdline.CmdLineParser;
+public class TestCmdLineParser {
-public class TestCmdLineParser extends TestCase
-{
+ @Test(expected = AssertionError.class)
+ public void testRegisterDash() {
+ String[] args = { "a", "b", "c" };
+ CmdLineParser p = new CmdLineParser(args);
+ try {
+ p.registerOpt('-', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ } catch (AssertionError e) {
+ assertEquals(e.getMessage(),
+ "CmdLineParser: '-' is not a legal single character designator.");
+ throw e;
+ }
+ }
-@Test
-public void testRegisterDash()
-{
- String[] args = {"a", "b", "c"};
- CmdLineParser p = new CmdLineParser(args);
- try {
- p.registerOpt('-', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- fail("Should have thrown an AssertionError");
- } catch (AssertionError e) {
- assertEquals(e.getMessage(),
- "CmdLineParser: '-' is not a legal single character designator.");
+ @Test(expected = AssertionError.class)
+ public void testDoubleRegisterShort() {
+ String[] args = { "a", "b", "c" };
+ CmdLineParser p = new CmdLineParser(args);
+ try {
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ p.registerOpt('a', "beta", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ fail("Should have thrown an AssertionError");
+ } catch (AssertionError e) {
+ assertEquals(e.getMessage(),
+ "CmdLineParser: You have already registered option a");
+ throw e;
+ }
}
-}
-@Test
-public void testDoubleRegisterShort()
-{
- String[] args = {"a", "b", "c"};
- CmdLineParser p = new CmdLineParser(args);
- try {
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('a', "beta", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- fail("Should have thrown an AssertionError");
- } catch (AssertionError e) {
- assertEquals(e.getMessage(),
- "CmdLineParser: You have already registered option a");
+ @Test(expected = AssertionError.class)
+ public void testDoubleRegisterLong() {
+ String[] args = { "a", "b", "c" };
+ CmdLineParser p = new CmdLineParser(args);
+ try {
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ p.registerOpt('b', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ } catch (AssertionError e) {
+ assertEquals(e.getMessage(),
+ "CmdLineParser: You have already registered option alpha");
+ throw e;
+ }
}
-}
-@Test
-public void testDoubleRegisterLong()
-{
- String[] args = {"a", "b", "c"};
- CmdLineParser p = new CmdLineParser(args);
- try {
+ @Test
+ public void testRegister() {
+ String[] args = { "a", "b", "c" };
+ CmdLineParser p = new CmdLineParser(args);
p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('b', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- fail("Should have thrown an AssertionError");
- } catch (AssertionError e) {
- assertEquals(e.getMessage(),
- "CmdLineParser: You have already registered option alpha");
+ p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
+ p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
}
-}
-@Test
-public void testRegister()
-{
- String[] args = {"a", "b", "c"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
- p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
-}
-
-@Test
-public void testParseNoArgs() throws ParseException
-{
- String[] args = {};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
-}
+ @Test
+ public void testParseNoArgs() throws ParseException {
+ String[] args = {};
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ assertEquals(CmdLineParser.EndOfOpts, p.getNextOpt());
+ }
-@Test
-public void testParseNoDash() throws ParseException
-{
- String[] args = {"a"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
- String[] remainders = p.getRemainingArgs();
- assertEquals(remainders[0], "a");
-}
+ @Test
+ public void testParseNoDash() throws ParseException {
+ String[] args = { "a" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
+ String[] remainders = p.getRemainingArgs();
+ assertEquals("a", remainders[0]);
+ }
-@Test
-public void testParseLongShortNoLeftover() throws ParseException
-{
- String[] args = {"-a", "--beta", "beth", "--c"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
- p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
- assertEquals(p.getNextOpt(), 'a');
- assertEquals(p.getNextOpt(), 'b');
- assertEquals(p.getValStr(), "beth");
- assertEquals(p.getNextOpt(), 'c');
- assertNull(p.getValStr());
- assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
- assertNull(p.getRemainingArgs());
-}
+ @Test
+ public void testParseLongShortNoLeftover() throws ParseException {
+ String[] args = { "-a", "--beta", "beth", "--c" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
+ p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
+ assertEquals('a', p.getNextOpt());
+ assertEquals('b', p.getNextOpt());
+ assertEquals("beth", p.getValStr());
+ assertEquals('c', p.getNextOpt());
+ assertNull(p.getValStr());
+ assertEquals(CmdLineParser.EndOfOpts, p.getNextOpt());
+ assertNull(p.getRemainingArgs());
+ }
-@Test
-public void testParseLongShortLeftover1() throws ParseException
-{
- String[] args = {"-a", "--beta", "beth", "--c", "gimel", "-", "hi", "i'm", "left",
- "over"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
- p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
- assertEquals(p.getNextOpt(), 'a');
- assertEquals(p.getNextOpt(), 'b');
- assertEquals(p.getValStr(), "beth");
- assertEquals(p.getNextOpt(), 'c');
- assertEquals(p.getValStr(), "gimel");
- assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
- String[] r = p.getRemainingArgs();
- assertEquals(r.length, 4);
-}
+ @Test
+ public void testParseLongShortLeftover1() throws ParseException {
+ String[] args = { "-a", "--beta", "beth", "--c", "gimel", "-", "hi", "i'm", "left",
+ "over" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
+ p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
+ assertEquals('a', p.getNextOpt());
+ assertEquals('b', p.getNextOpt());
+ assertEquals("beth", p.getValStr());
+ assertEquals('c', p.getNextOpt());
+ assertEquals("gimel", p.getValStr());
+ assertEquals(CmdLineParser.EndOfOpts, p.getNextOpt());
+ String[] r = p.getRemainingArgs();
+ assertEquals(4, r.length);
+ }
-// has two dashes instead of one for end of args
-@Test
-public void testParseLongShortLeftover2() throws ParseException
-{
- String[] args = {"-a", "-beta", "beth", "--c", "gimel", "--", "hi", "i'm", "left",
- "over"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
- p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
- assertEquals(p.getNextOpt(), 'a');
- assertEquals(p.getNextOpt(), 'b');
- assertEquals(p.getValStr(), "beth");
- assertEquals(p.getNextOpt(), 'c');
- assertEquals(p.getValStr(), "gimel");
- assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
- String[] r = p.getRemainingArgs();
- assertEquals(r.length, 4);
-}
+ // has two dashes instead of one for end of args
+ @Test
+ public void testParseLongShortLeftover2() throws ParseException {
+ String[] args = { "-a", "-beta", "beth", "--c", "gimel", "--", "hi", "i'm", "left",
+ "over" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
+ p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
+ assertEquals(p.getNextOpt(), 'a');
+ assertEquals(p.getNextOpt(), 'b');
+ assertEquals(p.getValStr(), "beth");
+ assertEquals(p.getNextOpt(), 'c');
+ assertEquals(p.getValStr(), "gimel");
+ assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
+ String[] r = p.getRemainingArgs();
+ assertEquals(r.length, 4);
+ }
-@Test
-public void testParseLongShortLeftover3() throws ParseException
-{
- String[] args = {"-a", "--beta", "5", "--c", "--"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
- p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
- assertEquals(p.getNextOpt(), 'a');
- assertEquals(p.getNextOpt(), 'b');
- Integer ii = p.getValInt();
- assertEquals(ii.intValue(), 5);
- assertEquals(p.getNextOpt(), 'c');
- assertNull(p.getValInt());
- assertEquals(p.getNextOpt(), CmdLineParser.EndOfOpts);
- String[] r = p.getRemainingArgs();
- assertNull(p.getRemainingArgs());
-}
+ @Test
+ public void testParseLongShortLeftover3() throws ParseException {
+ String[] args = { "-a", "--beta", "5", "--c", "--" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ p.registerOpt('b', "beta", CmdLineParser.ValueExpected.REQUIRED);
+ p.registerOpt('c', null, CmdLineParser.ValueExpected.OPTIONAL);
+ assertEquals('a', p.getNextOpt());
+ assertEquals('b', p.getNextOpt());
+ Integer ii = p.getValInt();
+ assertEquals(5, ii.intValue());
+ assertEquals('c', p.getNextOpt());
+ assertNull(p.getValInt());
+ assertEquals(CmdLineParser.EndOfOpts, p.getNextOpt());
+ String[] r = p.getRemainingArgs();
+ assertNull(p.getRemainingArgs());
+ }
-@Test
-public void testParseValueNotAcceptedProvided1() throws ParseException
-{
- String[] args = {"-a", "aleph"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- assertEquals(p.getNextOpt(), 'a');
- String[] r = p.getRemainingArgs();
- assertEquals(r.length, 1);
- assertEquals(r[0], "aleph");
-}
+ @Test
+ public void testParseValueNotAcceptedProvided1() throws ParseException {
+ String[] args = { "-a", "aleph" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ assertEquals('a', p.getNextOpt());
+ String[] r = p.getRemainingArgs();
+ assertEquals(1, r.length);
+ assertEquals("aleph", r[0]);
+ }
-@Test
-public void testParseValueNotAcceptedProvided2() throws ParseException
-{
- String[] args = {"-alpha", "aleph"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- assertEquals(p.getNextOpt(), 'a');
- String[] r = p.getRemainingArgs();
- assertEquals(r.length, 1);
- assertEquals(r[0], "aleph");
-}
+ @Test
+ public void testParseValueNotAcceptedProvided2() throws ParseException {
+ String[] args = { "-alpha", "aleph" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ assertEquals('a', p.getNextOpt());
+ String[] r = p.getRemainingArgs();
+ assertEquals(1, r.length);
+ assertEquals("aleph", r[0]);
+ }
-@Test
-public void testParseValueRequiredNotProvided1()
-{
- String[] args = {"-a"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
- try {
- p.getNextOpt();
- fail("Should have thrown a ParseException");
- } catch (ParseException e) {
- assertEquals(e.getMessage(),
- "Option -a requires a value but you did not provide one.");
+ @Test(expected = ParseException.class)
+ public void testParseValueRequiredNotProvided1() throws Exception {
+ String[] args = { "-a" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
+ try {
+ p.getNextOpt();
+ } catch (ParseException e) {
+ assertEquals(e.getMessage(),
+ "Option -a requires a value but you did not provide one.");
+ throw e;
+ }
}
-}
-@Test
-public void testParseValueRequiredNotProvided2()
-{
- String[] args = {"--alpha", "-b"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
- p.registerOpt('b', "beta", CmdLineParser.ValueExpected.NOT_ACCEPTED);
- try {
- p.getNextOpt();
- fail("Should have thrown a ParseException");
- } catch (ParseException e) {
- assertEquals(e.getMessage(),
- "Option --alpha requires a value but you did not provide one.");
+ @Test(expected = ParseException.class)
+ public void testParseValueRequiredNotProvided2() throws Exception {
+ String[] args = { "--alpha", "-b" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
+ p.registerOpt('b', "beta", CmdLineParser.ValueExpected.NOT_ACCEPTED);
+ try {
+ p.getNextOpt();
+ } catch (ParseException e) {
+ assertEquals(e.getMessage(),
+ "Option --alpha requires a value but you did not provide one.");
+ throw e;
+ }
}
-}
-@Test
-public void testParseValueStrForInt() throws ParseException
-{
- String[] args = {"-alpha", "b"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
- try {
+ @Test(expected = NumberFormatException.class)
+ public void testParseValueStrForInt() throws ParseException {
+ String[] args = { "-alpha", "b" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
p.getNextOpt();
Integer ii = p.getValInt();
- fail("Should have thrown a NumberFormatException");
- } catch (NumberFormatException e) {
}
-}
-@Test
-public void testParseUnknownShort()
-{
- String[] args = {"-alpha", "b", "-z"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
- try {
- p.getNextOpt();
- assertEquals(p.getValStr(), "b");
- p.getNextOpt();
- fail("Should have thrown a ParseException");
- } catch (ParseException e) {
- assertEquals(e.getMessage(),
- "Found unknown option (-z) at position 3");
+ @Test(expected = ParseException.class)
+ public void testParseUnknownShort() throws Exception {
+ String[] args = { "-alpha", "b", "-z" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
+ try {
+ p.getNextOpt();
+ assertEquals("b", p.getValStr());
+ p.getNextOpt();
+ } catch (ParseException e) {
+ assertEquals(e.getMessage(),
+ "Found unknown option (-z) at position 3");
+ throw e;
+ }
}
-}
-@Test
-public void testParseUnknownLong()
-{
- String[] args = {"--zeta"};
- CmdLineParser p = new CmdLineParser(args);
- p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
- try {
- p.getNextOpt();
- fail("Should have thrown a ParseException");
- } catch (ParseException e) {
- assertEquals(e.getMessage(),
- "Found unknown option (--zeta) at position 1");
+ @Test(expected = ParseException.class)
+ public void testParseUnknownLong() throws Exception {
+ String[] args = { "--zeta" };
+ CmdLineParser p = new CmdLineParser(args);
+ p.registerOpt('a', "alpha", CmdLineParser.ValueExpected.REQUIRED);
+ try {
+ p.getNextOpt();
+ } catch (ParseException e) {
+ assertEquals(e.getMessage(),
+ "Found unknown option (--zeta) at position 1");
+ throw e;
+ }
}
}
-
-}
-