001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Anahide Tchertchian 018 */ 019package org.nuxeo.ecm.platform.test; 020 021import java.util.ArrayList; 022import java.util.HashSet; 023import java.util.List; 024import java.util.Properties; 025import java.util.Set; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029 030/** 031 * Properties implementation detecting duplicate keys, that does *not* handle properly removal of items. 032 * 033 * @since 7.3 034 */ 035public class TranslationProperties extends Properties { 036 037 private static final long serialVersionUID = 1L; 038 039 private static final Log log = LogFactory.getLog(TranslationProperties.class); 040 041 protected Properties duplicates; 042 043 protected Set<String> singleLabels; 044 045 public TranslationProperties() { 046 super(); 047 duplicates = new Properties(); 048 singleLabels = new HashSet<String>(); 049 } 050 051 /** 052 * Override to detect duplicate keys 053 */ 054 @SuppressWarnings("unchecked") 055 @Override 056 public synchronized Object put(Object key, Object value) { 057 if (key instanceof String && !((String) key).contains(".")) { 058 singleLabels.add((String) key); 059 } 060 if (containsKey(key)) { 061 List<Object> values; 062 if (duplicates.containsKey(key)) { 063 values = (List<Object>) duplicates.get(key); 064 } else { 065 values = new ArrayList<>(); 066 duplicates.put(key, values); 067 values.add(get(key)); 068 } 069 values.add(value); 070 if (log.isDebugEnabled()) { 071 log.debug(String.format("Detected duplicate key '%s', values=%s", key, values)); 072 } 073 } 074 return super.put(key, value); 075 } 076 077 public Set<String> getDuplicatePropertyNames() { 078 return duplicates.stringPropertyNames(); 079 } 080 081 public Properties getDuplicates() { 082 return duplicates; 083 } 084 085 public Set<String> getSingleLabels() { 086 return singleLabels; 087 } 088 089 @Override 090 public synchronized void clear() { 091 super.clear(); 092 duplicates.clear(); 093 } 094 095 // other removal methods not handled 096 097}