/** * Introduce a new declaration which will mask any declaration with the same name in the {@link * #getParentContext} context. */ public VarInfo<NAME, BINDING> declare(NAME origName, FilePosition declSite) throws RedeclarationException { VarInfo<NAME, BINDING> d = vars.get(origName); if (d == null) { String newName = nameGenerator.next(); VarInfo<NAME, BINDING> vi = new VarInfo<NAME, BINDING>(origName, newName, declSite); vars.put(origName, vi); return vi; } else { FilePosition dPos = d.declaredAt; throw new RedeclarationException( new Message( RewriterMessageType.CANNOT_REDECLARE_VAR, declSite, MessagePart.Factory.valueOf(origName.toString()), dPos)); } }
@Override public SiteSet deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Builder builder = Site.builder(); // check if we have a site list if (json.isJsonArray()) { Type type = new TypeToken<List<Site>>() {}.getType(); List<Site> sites = context.deserialize(json, type); return new SiteSet(sites); } // or a region JsonObject jRegion = json.getAsJsonObject().getAsJsonObject(REGION); if (jRegion.has(VS30.toString())) { double vs30 = jRegion.get(VS30.toString()).getAsDouble(); builder.vs30(vs30); } if (jRegion.has(VS_INF.toString())) { boolean vsInf = jRegion.get(VS_INF.toString()).getAsBoolean(); builder.vsInferred(vsInf); } if (jRegion.has(Z1P0.toString())) { double z1p0 = jRegion.get(Z1P0.toString()).getAsDouble(); builder.z1p0(z1p0); } if (jRegion.has(Z2P5.toString())) { double z2p5 = jRegion.get(Z2P5.toString()).getAsDouble(); builder.z2p5(z2p5); } checkState(jRegion.has(BORDER), "Site region must define a border"); checkState(jRegion.has(SPACING), "Site region must define a spacing"); String name = "Unnamed region"; if (jRegion.has(NAME.toString())) { name = jRegion.get(NAME.toString()).getAsString(); } double spacing = jRegion.get(SPACING).getAsDouble(); JsonArray coords = jRegion.getAsJsonArray(BORDER); LocationList.Builder borderBuilder = LocationList.builder(); for (JsonElement jElem : coords) { JsonArray coord = jElem.getAsJsonArray(); borderBuilder.add(coord.get(1).getAsDouble(), coord.get(0).getAsDouble()); } LocationList border = borderBuilder.build(); checkState(border.size() >= 2, "Site region border must define at " + "least 2 coordinates"); GriddedRegion region = (border.size() == 2) ? Regions.createRectangularGridded( name, border.get(0), border.get(1), spacing, spacing, GriddedRegion.ANCHOR_0_0) : Regions.createGridded( name, border, MERCATOR_LINEAR, spacing, spacing, GriddedRegion.ANCHOR_0_0); return new SiteSet(region, builder); }
/** Tests if exception is thrown when we try to save without mandatory properties */ @Test public void createInvalidTest() throws CompanyServiceValidationException { Company company = new Company(); Owner o = new Owner(); o.setCompany(company); // test missing companyid try { companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), COMPANYID.toString()); } // test missing address try { company.setCompanyId("1"); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), ADDRESS.toString()); } // test missing city try { company.setAddress(PREFIX_ADDRESS); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), CITY.toString()); } // test missing country try { company.setCity(PREFIX_CITY); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), COUNTRY.toString()); } // test missing name try { company.setCountry(PREFIX_COUNTRY); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), NAME.toString()); } // test if at least 1 owner is added try { company.setName(PREFIX_NAME); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_OWNER); } // test missing owner firstname try { company.getOwners().add(o); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), OWNER_FIRSTNAME.toString()); } // test missing owner lastname try { o.setFirstName(PREFIX_FIRSTNAME); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), MISSING_FIELD); assertEquals(e.getInfo(), OWNER_LASTNAME.toString()); } // just save o.setLastName(PREFIX_LASTNAME); companyService.save(company); final String INVALID = "invalid"; // test invalid phone try { company.setPhone(INVALID); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), INVALID_PHONE_NUMBER); } // test invalid email try { company.setPhone(null); company.setEmail(INVALID); companyService.save(company); } catch (CompanyServiceValidationException e) { assertEquals(e.getError(), INVALID_EMAIL); } }