/**
* 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.hadoop.zebra.types;
import java.io.StringReader;
import junit.framework.Assert;
import org.apache.hadoop.zebra.types.ColumnType;
import org.apache.hadoop.zebra.types.ParseException;
import org.apache.hadoop.zebra.types.Schema;
import org.apache.hadoop.zebra.types.TableSchemaParser;
import org.apache.hadoop.zebra.types.Schema.ColumnSchema;
import org.junit.Test;
public class TestSchemaPrimitive {
@Test
public void testSchemaValid1() throws ParseException {
String strSch = "f1:int, f2:long, f3:float, f4:bool, f5:string, f6:bytes";
TableSchemaParser parser;
Schema schema;
parser = new TableSchemaParser(new StringReader(strSch));
schema = parser.RecordSchema(null);
System.out.println(schema);
ColumnSchema f2 = schema.getColumn(1);
Assert.assertEquals("f2", f2.name);
Assert.assertEquals(ColumnType.LONG, f2.type);
}
@Test
public void testSchemaValid2() throws ParseException {
String strSch = "f1:int, f2, f3:float, f4, f5:string, f6:bytes";
TableSchemaParser parser;
Schema schema;
parser = new TableSchemaParser(new StringReader(strSch));
schema = parser.RecordSchema(null);
System.out.println(schema);
ColumnSchema f2 = schema.getColumn(1);
Assert.assertEquals("f2", f2.name);
Assert.assertEquals(ColumnType.BYTES, f2.type);
ColumnSchema f4 = schema.getColumn(3);
Assert.assertEquals("f4", f4.name);
Assert.assertEquals(ColumnType.BYTES, f4.type);
}
/*
* @Test public void testSchemaValid3() throws ParseException { try { String
* strSch = ",f1:int"; TableSchemaParser parser; Schema schema;
*
* parser = new TableSchemaParser(new StringReader(strSch)); schema =
* parser.RecordSchema(null); System.out.println(schema);
*
* ColumnSchema f1 = schema.getColumn(0); //Assert.assertEquals("f2",
* f2.name); //Assert.assertEquals(ColumnType.BYTES, f2.type);
*
* ColumnSchema f2 = schema.getColumn(1); //Assert.assertEquals("f1",
* f4.name); //Assert.assertEquals(ColumnType.BYTES, f4.type); } catch
* (Exception e) { System.out.println(e.getMessage()); } }
*/
@Test
public void testSchemaInvalid1() {
try {
String strSch = "f1:int, f2:xyz, f3:float";
TableSchemaParser parser;
Schema schema;
parser = new TableSchemaParser(new StringReader(strSch));
schema = parser.RecordSchema(null);
System.out.println(schema);
} catch (Exception e) {
String errMsg = e.getMessage();
String str = "Encountered \" <IDENTIFIER> \"xyz \"\" at line 1, column 12.";
System.out.println(errMsg);
System.out.println(str);
Assert.assertEquals(errMsg.startsWith(str), true);
}
}
@Test
public void testSchemaInvalid2() {
try {
String strSch = "f1:";
TableSchemaParser parser;
Schema schema;
parser = new TableSchemaParser(new StringReader(strSch));
schema = parser.RecordSchema(null);
System.out.println(schema);
} catch (Exception e) {
String errMsg = e.getMessage();
String str = "Encountered \"<EOF>\" at line 1, column 3.";
System.out.println(errMsg);
System.out.println(str);
Assert.assertEquals(errMsg.startsWith(str), true);
}
}
@Test
public void testSchemaInvalid3() {
try {
String strSch = ":";
TableSchemaParser parser;
Schema schema;
parser = new TableSchemaParser(new StringReader(strSch));
schema = parser.RecordSchema(null);
System.out.println(schema);
} catch (Exception e) {
String errMsg = e.getMessage();
String str = "Encountered \" \":\" \": \"\" at line 1, column 1.";
System.out.println(errMsg);
System.out.println(str);
Assert.assertEquals(errMsg.startsWith(str), true);
}
}
@Test
public void testSchemaInvalid4() {
try {
String strSch = "f1:int abc";
TableSchemaParser parser;
Schema schema;
parser = new TableSchemaParser(new StringReader(strSch));
schema = parser.RecordSchema(null);
System.out.println(schema);
} catch (Exception e) {
String errMsg = e.getMessage();
String str = "Encountered \" <IDENTIFIER> \"abc \"\" at line 1, column 8.";
System.out.println(errMsg);
System.out.println(str);
Assert.assertEquals(errMsg.startsWith(str), true);
}
}
}