@SuppressWarnings("deprecation")
@Test
public void testPigStorageSchemaSearch() throws Exception {
String globtestdir = "build/test/tmpglobbingdata/";
ResourceSchema testSchema = new ResourceSchema(Utils.parseSchema("a0:chararray"));
PigStorage pigStorage = new PigStorage();
pigContext.connect();
try{
Util.deleteDirectory(new File(datadir));
pig.mkdirs(globtestdir+"a");
pig.mkdirs(globtestdir+"a/a0");
putInputFile(globtestdir+"a/a0/input");
pig.mkdirs(globtestdir+"a/b0");
putInputFile(globtestdir+"a/b0/input");
pig.mkdirs(globtestdir+"b");
} catch (IOException e) {};
Configuration conf = ConfigurationUtil.toConfiguration(pigContext.getProperties());
// if schema file is not found, schema is null
ResourceSchema schema = pigStorage.getSchema(globtestdir, new Job(conf));
Assert.assertTrue(schema==null);
// if .pig_schema is in the input directory
putSchemaFile(globtestdir+"a/a0/.pig_schema", testSchema);
schema = pigStorage.getSchema(globtestdir+"a/a0", new Job(conf));
Assert.assertTrue(ResourceSchema.equals(schema, testSchema));
new File(globtestdir+"a/a0/.pig_schema").delete();
// .pig_schema in one of globStatus returned directory
putSchemaFile(globtestdir+"a/.pig_schema", testSchema);
schema = pigStorage.getSchema(globtestdir+"*", new Job(conf));
Assert.assertTrue(ResourceSchema.equals(schema, testSchema));
new File(globtestdir+"a/.pig_schema").delete();
putSchemaFile(globtestdir+"b/.pig_schema", testSchema);
schema = pigStorage.getSchema(globtestdir+"*", new Job(conf));
Assert.assertTrue(ResourceSchema.equals(schema, testSchema));
new File(globtestdir+"b/.pig_schema").delete();
// if .pig_schema is deep in the globbing, it will not get used
putSchemaFile(globtestdir+"a/a0/.pig_schema", testSchema);
schema = pigStorage.getSchema(globtestdir+"*", new Job(conf));
Assert.assertTrue(schema==null);
putSchemaFile(globtestdir+"a/.pig_schema", testSchema);
schema = pigStorage.getSchema(globtestdir+"*", new Job(conf));
Assert.assertTrue(ResourceSchema.equals(schema, testSchema));
new File(globtestdir+"a/a0/.pig_schema").delete();
new File(globtestdir+"a/.pig_schema").delete();
pigStorage = new PigStorage("\t", "-schema");
putSchemaFile(globtestdir+"a/.pig_schema", testSchema);
schema = pigStorage.getSchema(globtestdir+"{a,b}", new Job(conf));
Assert.assertTrue(ResourceSchema.equals(schema, testSchema));
}