static String getShortDescription(Difference diff) { if (diff == null) { return null; } int n; switch (diff.getType()) { case Difference.ADD: n = diff.getSecondEnd() - diff.getSecondStart() + 1; return MessageFormat.format(new ChoiceFormat(NbBundle.getMessage(DiffSidebar.class, "TT_LinesAdded")).format(n), n); // NOI18N case Difference.CHANGE: n = diff.getFirstEnd() - diff.getFirstStart() + 1; return MessageFormat.format(new ChoiceFormat(NbBundle.getMessage(DiffSidebar.class, "TT_LinesChanged")).format(n), n); // NOI18N case Difference.DELETE: n = diff.getFirstEnd() - diff.getFirstStart() + 1; return MessageFormat.format(new ChoiceFormat(NbBundle.getMessage(DiffSidebar.class, "TT_LinesDeleted")).format(n), n); // NOI18N default: throw new IllegalStateException("Unknown difference type: " + diff.getType()); // NOI18N } }
/** * Create a data file for this test. The data file must be corrupted by hand. */ private static void prepTest() { try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("Bug4185732.ser")); final double[] limits = {1,2,3,4,5,6,7}; final String[] formats = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; final ChoiceFormat fmt = new ChoiceFormat(limits, formats); out.writeObject(fmt); out.close(); System.out.println("You must invalidate the output file before running the test"); System.out.println("by modifying the length of one of the array"); } catch (Exception e) { System.out.println(e); } }
public static void main(String[] args) throws Exception { ChoiceFormat choiceFormat1 = new ChoiceFormat(doubles, strings); ChoiceFormat choiceFormat2 = new ChoiceFormat(pattern); if (!choiceFormat1.equals(choiceFormat2)) { System.out.println("choiceFormat1: " + choiceFormat1.toPattern()); System.out.println("choiceFormat2: " + choiceFormat2.toPattern()); throw new RuntimeException(); } for (int i = 0; i < doubles.length; i++) { String result = choiceFormat2.format(doubles[i]); if (!result.equals(strings[i])) { throw new RuntimeException("Wrong format result - expected " + strings[i] + ", got " + result); } } }
private static void writeFormatToFile(final String name) { try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(name)); MessageFormat fmt = new MessageFormat("The disk \"{1}\" contains {0}."); double[] filelimits = {0,1,2}; String[] filepart = {"no files","one file","{0,number} files"}; ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart); fmt.setFormat(1,fileform); // NOT zero, see below out.writeObject(fmt); out.close(); } catch (Exception e) { System.out.println(e); } }
/** * @tests java.text.ChoiceFormat#parse(java.lang.String, * java.text.ParsePosition) */ public void test_parseLjava_lang_StringLjava_text_ParsePosition() { // Test for method java.lang.Number // java.text.ChoiceFormat.parse(java.lang.String, // java.text.ParsePosition) ChoiceFormat format = new ChoiceFormat("1#one|2#two|3#three"); assertTrue("Case insensitive", Double.isNaN(format .parse("One", new ParsePosition(0)).doubleValue())); ParsePosition pos = new ParsePosition(0); Number result = f1.parse("Greater than two", pos); assertTrue("Not a Double1", result instanceof Double); assertTrue("Wrong value ~>2", result.doubleValue() == ChoiceFormat .nextDouble(2)); assertEquals("Wrong position ~16", 16, pos.getIndex()); pos = new ParsePosition(0); assertTrue("Incorrect result", Double.isNaN(f1.parse("12one", pos) .doubleValue())); assertEquals("Wrong position ~0", 0, pos.getIndex()); pos = new ParsePosition(2); result = f1.parse("12one and two", pos); assertTrue("Not a Double2", result instanceof Double); assertEquals("Ignored parse position", 1.0D, result.doubleValue(), 0.0D); assertEquals("Wrong position ~5", 5, pos.getIndex()); }
/** * @tests java.text.ChoiceFormat#format(double) */ public void test_formatD() { ChoiceFormat fmt = new ChoiceFormat( "-1#NEGATIVE_ONE|0#ZERO|1#ONE|1<GREATER_THAN_ONE"); assertEquals("NEGATIVE_ONE", fmt.format(Double.NEGATIVE_INFINITY)); assertEquals("NEGATIVE_ONE", fmt.format(-999999999D)); assertEquals("NEGATIVE_ONE", fmt.format(-1.1)); assertEquals("NEGATIVE_ONE", fmt.format(-1.0)); assertEquals("NEGATIVE_ONE", fmt.format(-0.9)); assertEquals("ZERO", fmt.format(0.0)); assertEquals("ZERO", fmt.format(0.9)); assertEquals("ONE", fmt.format(1.0)); assertEquals("GREATER_THAN_ONE", fmt.format(1.1)); assertEquals("GREATER_THAN_ONE", fmt.format(999999999D)); assertEquals("GREATER_THAN_ONE", fmt.format(Double.POSITIVE_INFINITY)); }
/** * Test the built in choice format. */ public void testBuiltInChoiceFormat() { Object[] values = new Number[] {new Integer(1), new Double("2.2"), new Double("1234.5")}; String choicePattern = null; Locale[] availableLocales = ChoiceFormat.getAvailableLocales(); choicePattern = "{0,choice,1#One|2#Two|3#Many {0,number}}"; for (int i = 0; i < values.length; i++) { checkBuiltInFormat(values[i] + ": " + choicePattern, new Object[] {values[i]}, availableLocales); } choicePattern = "{0,choice,1#''One''|2#\"Two\"|3#''{Many}'' {0,number}}"; for (int i = 0; i < values.length; i++) { checkBuiltInFormat(values[i] + ": " + choicePattern, new Object[] {values[i]}, availableLocales); } }
@Test public void testParseBigDecimal () { final BigDecimal aBD1M = StringParser.parseBigDecimal ("1000000"); assertEquals (aBD1M, LocaleParser.parseBigDecimal ("1.000.000", L_DE, CGlobal.BIGDEC_MINUS_ONE)); assertEquals (aBD1M, LocaleParser.parseBigDecimal ("1,000,000", L_EN, CGlobal.BIGDEC_MINUS_ONE)); assertEquals (aBD1M, LocaleParser.parseBigDecimal ("1,000,000", (DecimalFormat) NumberFormat.getInstance (L_EN))); assertEquals (new BigDecimal ("1234567.8901"), LocaleParser.parseBigDecimal ("1.234.567,8901", L_DE, CGlobal.BIGDEC_MINUS_ONE)); assertEquals (CGlobal.BIGDEC_MINUS_ONE, LocaleParser.parseBigDecimal ("... und denken", L_EN, CGlobal.BIGDEC_MINUS_ONE)); final ChoiceFormat aCF = new ChoiceFormat ("-1#negative|0#zero|1.0#one"); assertEquals (BigDecimal.valueOf (0.0), LocaleParser.parseBigDecimal ("zero", aCF, CGlobal.BIGDEC_MINUS_ONE)); try { LocaleParser.parseBigDecimal ("0", (DecimalFormat) null); fail (); } catch (final NullPointerException ex) {} }
/** * @tests java.text.NumberFormat#getCurrency() */ public void test_getCurrency() { // Test for method java.util.Currency getCurrency() // a subclass that supports currency formatting Currency currH = Currency.getInstance("HUF"); NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); assertSame("Returned incorrect currency", currH, format.getCurrency()); // a subclass that doesn't support currency formatting ChoiceFormat cformat = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); try { ((NumberFormat) cformat).getCurrency(); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } }
/** * @tests java.text.NumberFormat#setCurrency(java.util.Currency) */ public void test_setCurrencyLjava_util_Currency() { // Test for method void setCurrency(java.util.Currency) // a subclass that supports currency formatting Currency currA = Currency.getInstance("ARS"); NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU")); format.setCurrency(currA); assertSame("Returned incorrect currency", currA, format.getCurrency()); // a subclass that doesn't support currency formatting ChoiceFormat cformat = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); try { ((NumberFormat) cformat).setCurrency(currA); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException e) { } }
/** * @tests java.text.ChoiceFormat#parse(java.lang.String, * java.text.ParsePosition) */ public void test_parseLjava_lang_StringLjava_text_ParsePosition() { // Test for method java.lang.Number // java.text.ChoiceFormat.parse(java.lang.String, // java.text.ParsePosition) ChoiceFormat format = new ChoiceFormat("1#one|2#two|3#three"); assertEquals("Case insensitive", 0, format .parse("One", new ParsePosition(0)).intValue()); ParsePosition pos = new ParsePosition(0); Number result = f1.parse("Greater than two", pos); assertTrue("Not a Double1", result instanceof Double); assertTrue("Wrong value ~>2", result.doubleValue() == ChoiceFormat .nextDouble(2)); assertEquals("Wrong position ~16", 16, pos.getIndex()); pos = new ParsePosition(0); assertTrue("Incorrect result", Double.isNaN(f1.parse("12one", pos) .doubleValue())); assertEquals("Wrong position ~0", 0, pos.getIndex()); pos = new ParsePosition(2); result = f1.parse("12one and two", pos); assertTrue("Not a Double2", result instanceof Double); assertEquals("Ignored parse position", 1.0D, result.doubleValue(), 0.0D); assertEquals("Wrong position ~5", 5, pos.getIndex()); }
private String describeEverySecond(final int second) { final double[] secondsLimit = {1, 2}; final String[] secondsStrings = { resourceBundle.getString("oneSecond"), resourceBundle.getString("multipleSeconds") }; final double[] everyLimit = {1,2}; final String[] everyStrings = { resourceBundle.getString("every_one"), resourceBundle.getString("every_multi") }; final ChoiceFormat secondsChoiceFormat = new ChoiceFormat(secondsLimit, secondsStrings); final ChoiceFormat everyChoiceFormat = new ChoiceFormat(everyLimit, everyStrings); final String pattern = resourceBundle.getString("pattern_every_seconds"); final MessageFormat messageFormat = new MessageFormat(pattern, Locale.UK); final Format[] formats = { everyChoiceFormat, secondsChoiceFormat, NumberFormat.getInstance() }; messageFormat.setFormats(formats); final Object[] messageArguments = {second, second, second }; final String result = messageFormat.format(messageArguments); return result; }
/** * Uitility for createing the searchable dialog message format. * * @return reuseable message format. */ public MessageFormat setupSearchResultMessageForm() { MessageFormat messageForm = new MessageFormat(messageBundle.getString( "viewer.utilityPane.search.result.msg")); double[] pageLimits = {0, 1, 2}; String[] resultsStrings = { messageBundle.getString( "viewer.utilityPane.search.result.moreFile.msg"), messageBundle.getString( "viewer.utilityPane.search.result.oneFile.msg"), messageBundle.getString( "viewer.utilityPane.search.result.moreFile.msg") }; ChoiceFormat resultsChoiceForm = new ChoiceFormat(pageLimits, resultsStrings); Format[] formats = {null, resultsChoiceForm}; messageForm.setFormats(formats); return messageForm; }
/** * Uitility for createing the searching message format. * * @return reuseable message format. */ public MessageFormat setupSearchingMessageForm() { // Build Internationalized plural phrase. MessageFormat messageForm = new MessageFormat(messageBundle.getString( "viewer.utilityPane.search.searching1.msg")); double[] fileLimits = {0, 1, 2}; String[] fileStrings = { messageBundle.getString( "viewer.utilityPane.search.searching1.moreFile.msg"), messageBundle.getString( "viewer.utilityPane.search.searching1.oneFile.msg"), messageBundle.getString( "viewer.utilityPane.search.searching1.moreFile.msg"), }; ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings); Format[] formats = {null, choiceForm, null}; messageForm.setFormats(formats); return messageForm; }
/** * Count the maximum fields that are used in a message format * note that a ChoiceFormat can have sub-formats: {0,choice,0#{1}|1#{2}} * has three parameters, yet the basic messageFormat.getFormatsByArgumentIndex().length * call will have the value of one. This is because the sub-formats are * handled in a recursive way. Since this is the case, this method needs * to do something similar to count the fields used. * @param messageFormat message format instance to count the maximum field number used. * @return the maximum number of fields used in */ private int countFields(MessageFormat messageFormat) { Format[] formats = messageFormat.getFormatsByArgumentIndex(); int count = messageFormat.getFormatsByArgumentIndex().length; for (Format ff : formats) { if (ff instanceof ChoiceFormat) { ChoiceFormat cf = (ChoiceFormat) ff; for (Object o : cf.getFormats()) { // gets a list of the format choice values. the "foo" and "bar" parts of {0,choice,0#foo|1#bar} if (o != null) { MessageFormat subFormat = new MessageFormat(o.toString()); count = Math.max(count, countFields(subFormat)); } } } } return count; }
public int test() { try { String pattern = "-1#is negative| 0#is zero or fraction | 1#is one " + "|1.0<is 1+ |2#is two |2<is more than 2."; ChoiceFormat f1 = new ChoiceFormat(pattern); testFormat(f1, pattern); ChoiceFormat f2 = new ChoiceFormat(""); f2.applyPattern(pattern); testFormat(f2, pattern); } catch (Throwable e) { return fail(e.getMessage()); } return pass(); }
private void testFormat(ChoiceFormat f, String pattern) throws Throwable { double[] numbers = { Double.NEGATIVE_INFINITY, -1.0, 0, 0.9, 1, 1.5, 2, 2.1, Double.POSITIVE_INFINITY}; //Framework fw = new Framework(); String[] values = getValues(); //Object [] values = new Object[numbers.length]; testNextDouble(); for (int i = 0; i < numbers.length; i++) { String format = f.format(numbers[i]); if (!values[i].equals(format)) { throw new Throwable("ChoiceFormat.format(" + numbers[i] + ") ==" + format + " != " + values[i] + "\nPattern: '" + pattern + "'"); } } //System.out.println(fw // .generateGetter("values", values, String.class)); }
/** * Resolve an artifact and return its location in the local repository. Aether performs the normal * Maven resolution process ensuring that the latest update is cached to the local repository. * In addition, if the {@link MavenProperties#resolvePom} flag is <code>true</code>, * the POM is also resolved and cached. * @param resource the {@link MavenResource} representing the artifact * @return a {@link FileSystemResource} representing the resolved artifact in the local repository * @throws IllegalStateException if the artifact does not exist or the resolution fails */ Resource resolve(MavenResource resource) { Assert.notNull(resource, "MavenResource must not be null"); validateCoordinates(resource); RepositorySystemSession session = newRepositorySystemSession(this.repositorySystem, this.properties.getLocalRepository()); ArtifactResult resolvedArtifact; try { List<ArtifactRequest> artifactRequests = new ArrayList<>(2); if (properties.isResolvePom()) { artifactRequests.add(new ArtifactRequest(toPomArtifact(resource), this.remoteRepositories, JavaScopes.RUNTIME)); } artifactRequests.add(new ArtifactRequest(toJarArtifact(resource), this.remoteRepositories, JavaScopes.RUNTIME)); List<ArtifactResult> results = this.repositorySystem.resolveArtifacts(session, artifactRequests); resolvedArtifact = results.get(results.size() - 1); } catch (ArtifactResolutionException e) { ChoiceFormat pluralizer = new ChoiceFormat( new double[] { 0d, 1d, ChoiceFormat.nextDouble(1d) }, new String[] { "repositories: ", "repository: ", "repositories: " }); MessageFormat messageFormat = new MessageFormat( "Failed to resolve MavenResource: {0}. Configured remote {1}: {2}"); messageFormat.setFormat(1, pluralizer); String repos = properties.getRemoteRepositories().isEmpty() ? "none" : StringUtils.collectionToDelimitedString(properties.getRemoteRepositories().keySet(), ",", "[", "]"); throw new IllegalStateException( messageFormat.format(new Object[] { resource, properties.getRemoteRepositories().size(), repos }), e); } return toResource(resolvedArtifact); }
/** * @tests java.text.MessageFormat#getFormats() */ public void test_getFormats() { // Test for method java.text.Format [] // java.text.MessageFormat.getFormats() // test with repeating formats and max argument index < max offset Format[] formats = format1.getFormats(); Format[] correctFormats = new Format[] { NumberFormat.getCurrencyInstance(), DateFormat.getTimeInstance(), NumberFormat.getPercentInstance(), null, new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance(), }; assertEquals("Test1:Returned wrong number of formats:", correctFormats.length, formats.length); for (int i = 0; i < correctFormats.length; i++) { assertEquals("Test1:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]); } // test with max argument index > max offset formats = format2.getFormats(); correctFormats = new Format[] { NumberFormat.getCurrencyInstance(), DateFormat.getTimeInstance(), NumberFormat.getPercentInstance(), null, new ChoiceFormat("0#off|1#on"), DateFormat.getDateInstance() }; assertEquals("Test2:Returned wrong number of formats:", correctFormats.length, formats.length); for (int i = 0; i < correctFormats.length; i++) { assertEquals("Test2:wrong format for pattern index " + i + ":", correctFormats[i], formats[i]); } // test with argument number being zero formats = format3.getFormats(); assertEquals("Test3: Returned wrong number of formats:", 0, formats.length); }
/** * @tests java.text.MessageFormat#getFormatsByArgumentIndex() */ public void test_getFormatsByArgumentIndex() { // Test for method java.text.Format [] test_getFormatsByArgumentIndex() // test with repeating formats and max argument index < max offset Format[] formats = format1.getFormatsByArgumentIndex(); Format[] correctFormats = new Format[] { DateFormat.getDateInstance(), new ChoiceFormat("0#off|1#on"), DateFormat.getTimeInstance(), NumberFormat.getCurrencyInstance(), null }; assertEquals("Test1:Returned wrong number of formats:", correctFormats.length, formats.length); for (int i = 0; i < correctFormats.length; i++) { assertEquals("Test1:wrong format for argument index " + i + ":", correctFormats[i], formats[i]); } // test with max argument index > max offset formats = format2.getFormatsByArgumentIndex(); correctFormats = new Format[] { DateFormat.getDateInstance(), new ChoiceFormat("0#off|1#on"), null, NumberFormat.getCurrencyInstance(), null, null, null, null, DateFormat.getTimeInstance() }; assertEquals("Test2:Returned wrong number of formats:", correctFormats.length, formats.length); for (int i = 0; i < correctFormats.length; i++) { assertEquals("Test2:wrong format for argument index " + i + ":", correctFormats[i], formats[i]); } // test with argument number being zero formats = format3.getFormatsByArgumentIndex(); assertEquals("Test3: Returned wrong number of formats:", 0, formats.length); }
/** * @tests java.text.ChoiceFormat#clone() */ public void test_clone() { // Test for method java.lang.Object java.text.ChoiceFormat.clone() ChoiceFormat f = (ChoiceFormat) f1.clone(); assertTrue("Not equal", f.equals(f1)); f.setChoices(new double[] { 0, 1, 2 }, new String[] { "0", "1", "2" }); assertTrue("Equal", !f.equals(f1)); }
/** * @tests java.text.ChoiceFormat#equals(java.lang.Object) */ public void test_equalsLjava_lang_Object() { // Test for method boolean // java.text.ChoiceFormat.equals(java.lang.Object) String patternString = "-2#Inverted Orange| 0#No Orange| 0<Almost No Orange| 1#Normal Orange| 2#Expensive Orange"; double[] appleLimits = { 1, 2, 3, 4, 5 }; String[] appleFormats = { "Tiny Apple", "Small Apple", "Medium Apple", "Large Apple", "Huge Apple" }; double[] orangeLimits = { -2, 0, ChoiceFormat.nextDouble(0), 1, 2 }; String[] orangeFormats = { "Inverted Orange", "No Orange", "Almost No Orange", "Normal Orange", "Expensive Orange" }; ChoiceFormat appleChoiceFormat = new ChoiceFormat(appleLimits, appleFormats); ChoiceFormat orangeChoiceFormat = new ChoiceFormat(orangeLimits, orangeFormats); ChoiceFormat orangeChoiceFormat2 = new ChoiceFormat(patternString); ChoiceFormat hybridChoiceFormat = new ChoiceFormat(appleLimits, orangeFormats); assertTrue("Apples should not equal oranges", !appleChoiceFormat .equals(orangeChoiceFormat)); assertTrue("Different limit list--should not appear as equal", !orangeChoiceFormat.equals(hybridChoiceFormat)); assertTrue("Different format list--should not appear as equal", !appleChoiceFormat.equals(hybridChoiceFormat)); assertTrue("Should be equal--identical format", appleChoiceFormat .equals(appleChoiceFormat)); assertTrue("Should be equals--same limits, same formats", orangeChoiceFormat.equals(orangeChoiceFormat2)); ChoiceFormat f2 = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); assertTrue("Not equal", f1.equals(f2)); }
/** * @tests java.text.ChoiceFormat#format(double, java.lang.StringBuffer, * java.text.FieldPosition) */ public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition() { // Test for method java.lang.StringBuffer // java.text.ChoiceFormat.format(double, java.lang.StringBuffer, // java.text.FieldPosition) FieldPosition field = new FieldPosition(0); StringBuffer buf = new StringBuffer(); String r = f1.format(-1, buf, field).toString(); assertEquals("Wrong choice for -1", "Less than one", r); buf.setLength(0); r = f1.format(0, buf, field).toString(); assertEquals("Wrong choice for 0", "Less than one", r); buf.setLength(0); r = f1.format(1, buf, field).toString(); assertEquals("Wrong choice for 1", "one", r); buf.setLength(0); r = f1.format(2, buf, field).toString(); assertEquals("Wrong choice for 2", "Between one and two", r); buf.setLength(0); r = f1.format(3, buf, field).toString(); assertEquals("Wrong choice for 3", "Greater than two", r); // Regression test for HARMONY-1081 assertEquals(0, new ChoiceFormat("|").format(Double.NaN, new StringBuffer(), new FieldPosition(6)).length()); assertEquals(0, new ChoiceFormat("|").format(1, new StringBuffer(), new FieldPosition(6)).length()); assertEquals("Less than one", f1.format(Double.NaN, new StringBuffer(), field).toString()); }
/** * @tests java.text.ChoiceFormat#hashCode() */ public void test_hashCode() { // Test for method int java.text.ChoiceFormat.hashCode() ChoiceFormat f2 = new ChoiceFormat( "0#Less than one|1#one|1<Between one and two|2<Greater than two"); assertTrue("Different hash", f1.hashCode() == f2.hashCode()); }
/** * @tests java.text.ChoiceFormat#nextDouble(double) */ public void test_nextDoubleD() { // Test for method double java.text.ChoiceFormat.nextDouble(double) assertTrue("Not greater 5", ChoiceFormat.nextDouble(5) > 5); assertTrue("Not greater 0", ChoiceFormat.nextDouble(0) > 0); assertTrue("Not greater -5", ChoiceFormat.nextDouble(-5) > -5); assertTrue("Not NaN", Double.isNaN(ChoiceFormat.nextDouble(Double.NaN))); }
/** * @tests java.text.ChoiceFormat#nextDouble(double, boolean) */ public void test_nextDoubleDZ() { // Test for method double java.text.ChoiceFormat.nextDouble(double, // boolean) assertTrue("Not greater 0", ChoiceFormat.nextDouble(0, true) > 0); assertTrue("Not less 0", ChoiceFormat.nextDouble(0, false) < 0); }
/** * @tests java.text.ChoiceFormat#previousDouble(double) */ public void test_previousDoubleD() { // Test for method double java.text.ChoiceFormat.previousDouble(double) assertTrue("Not less 5", ChoiceFormat.previousDouble(5) < 5); assertTrue("Not less 0", ChoiceFormat.previousDouble(0) < 0); assertTrue("Not less -5", ChoiceFormat.previousDouble(-5) < -5); assertTrue("Not NaN", Double.isNaN(ChoiceFormat .previousDouble(Double.NaN))); }