Path: blob/master/test/jdk/javax/sound/sampled/AudioFileFormat/Properties.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.HashMap;24import java.util.Map;25import java.util.Set;2627import javax.sound.midi.MidiFileFormat;28import javax.sound.midi.Sequence;29import javax.sound.sampled.AudioFileFormat;30import javax.sound.sampled.AudioFormat;3132/**33* @test34* @bug 466684535* @summary RFE: Add properties to AudioFileFormat and MidiFileFormat36*/37public class Properties {3839static boolean g_failed = false;4041// all of p1 need to be in p242static boolean compare(Map p1, Map p2) {43boolean failed = false;44for(String key: (Set<String>) p1.keySet()) {45out(" testing key: "+key);46if (!p2.containsKey(key)) {47out(" missing property: '"+key+"'. Failed");48failed = true;49}50Object v1 = p1.get(key);51Object v2 = p2.get(key);52if (((v1 == null) && (v2 != null))53|| ((v1 != null) && (v2 == null))54|| !(v1.equals(v2))) {55out(" property '"+key+"' is different: "56+"expected='"+v1+"' "57+"actual='"+v2+"'. Failed");58failed = true;59}60}61// test if we can modify p262try {63int oldSize = p2.size();64p2.clear();65if (oldSize > 0 && p2.size() == 0) {66out(" could clear the properties! Failed.");67failed = true;68}69} catch (Exception e) {70// correct71}72return failed;73}7475public static void main(String argv[]) throws Exception {76// don't need to catch exceptions: any exception is a77// failure of this test7879Map<String, Object> p = new HashMap<String,Object>();80p.put("author", "Florian");81p.put("duration", new Long(1000));82p.put("MyProp", "test");8384out("Testing AudioFileFormat properties:");85// create an AudioFileFormat with properties86AudioFormat format = new AudioFormat( 44100.0f, 16, 2, true, false);87AudioFileFormat aff =88new AudioFileFormat(AudioFileFormat.Type.WAVE,89format, 1000, p);90// test that it has the properties91boolean failed = compare(p, aff.properties());92// test getProperty()93Object o = aff.getProperty("author");94if (o == null || !o.equals("Florian")) {95out(" getProperty did not report an existing property!");96failed = true;97}98o = aff.getProperty("does not exist");99if (o != null) {100out(" getProperty returned something for a non-existing property!");101failed = true;102}103if (!failed) {104out(" OK");105} else {106g_failed = true;107}108109110111out("Testing MidiFileFormat properties:");112// create a MidiFileFormat with properties113MidiFileFormat mff =114new MidiFileFormat(0, Sequence.PPQ, 240,1151000, 100, p);116// test that it has the properties117failed = compare(p, mff.properties());118// test getProperty()119o = mff.getProperty("author");120if (o == null || !o.equals("Florian")) {121out(" getProperty did not report an existing property!");122failed = true;123}124o = mff.getProperty("does not exist");125if (o != null) {126out(" getProperty returned something for a non-existing property!");127failed = true;128}129if (!failed) {130out(" OK");131} else {132g_failed = true;133}134135136if (g_failed) throw new Exception("Test FAILED!");137System.out.println("Test passed.");138}139140static void out(String s) {141System.out.println(s);142}143}144145146