QueryGenUtil { public static void validatePath(String path, Model model) { int posOfOrder = path.indexOf(" ORDER BY "); if (posOfOrder != -1) { path = path.substring(0, posOfOrder); } String[] queryBits = path.split("[ \t]"); if (!(queryBits.length > 1) || (queryBits.length % 2 == 0)) { throw new IllegalArgumentException("Construct query path does not have valid " + " number of elements: " + path); } for (int i = 0; i + 2 < queryBits.length; i += 2) { String start = model.getPackageName() + "." + queryBits[i]; String refName = queryBits[i + 1]; String end = model.getPackageName() + "." + queryBits[i + 2]; if (!model.hasClassDescriptor(start)) { throw new IllegalArgumentException("Class not found in model: " + start); } else if (!model.hasClassDescriptor(end)) { throw new IllegalArgumentException("Class not found in model: " + end); } ClassDescriptor startCld = model.getClassDescriptorByName(start); ClassDescriptor endCld = model.getClassDescriptorByName(end); int dotPos = refName.indexOf('.'); if (dotPos != -1) { String prefix = refName.substring(0, dotPos); if (!("reverse".equals(prefix))) { int replacementStartNo; try { replacementStartNo = Integer.parseInt(prefix) - 1; } catch (NumberFormatException e) { throw new IllegalArgumentException("Prefix " + prefix + " is not a number " + "or \"reverse\"", e); } if (replacementStartNo > (i / 2) - 1) { throw new IllegalArgumentException("Prefix " + prefix + " refers to future"); } startCld = model.getClassDescriptorByName(model.getPackageName() + "." + queryBits[replacementStartNo * 2]); refName = refName.substring(dotPos + 1); } } if (refName.startsWith("reverse.")) { ClassDescriptor tempCld = startCld; startCld = endCld; endCld = tempCld; refName = refName.substring(8); } if ((startCld.getReferenceDescriptorByName(refName, true) == null) && (startCld.getCollectionDescriptorByName(refName, true) == null)) { throw new IllegalArgumentException("Cannot find descriptor for " + refName + " in " + startCld.getName()); } } } private  QueryGenUtil(); static void validatePath(String path, Model model); static Set<String> expandPath(ObjectStore os, String path); static Query constructQuery(Model model, String path);  }
@Test public void testValidatePath() throws Exception { try { QueryGenUtil.validatePath("Company", os.getModel()); Assert.fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { QueryGenUtil.validatePath("Company departments Department manager", os.getModel()); Assert.fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { QueryGenUtil.validatePath("Department manager Monkey", os.getModel()); Assert.fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { QueryGenUtil.validatePath("Department teaboy Employee", os.getModel()); Assert.fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { QueryGenUtil.validatePath("Company reverse.departments Department", os.getModel()); Assert.fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } try { QueryGenUtil.validatePath("Company 1.departments Department", os.getModel()); Assert.fail("expected IllegalArgumentException"); } catch (IllegalArgumentException e) { } QueryGenUtil.validatePath("Company departments Department", os.getModel()); QueryGenUtil.validatePath("Company address Address", os.getModel()); QueryGenUtil.validatePath("Company departments Department manager Manager", os.getModel()); QueryGenUtil.validatePath("Company reverse.company Department", os.getModel()); QueryGenUtil.validatePath("Company departments Department 1.address Address", os.getModel()); }