org.alfresco.service.cmr.repository.MLText Java Examples

The following examples show how to use org.alfresco.service.cmr.repository.MLText. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMultiValueMLTextProperties() throws Exception
{
    NodeRef nodeRef = nodeService.createNode(
            rootNodeRef,
            ASSOC_TYPE_QNAME_TEST_CHILDREN,
            QName.createQName("pathA"),
            TYPE_QNAME_TEST_MANY_ML_PROPERTIES).getChildRef();
    
    // Create MLText properties and add to a collection
    List<MLText> mlTextCollection = new ArrayList<MLText>(2);
    MLText mlText0 = new MLText();
    mlText0.addValue(Locale.ENGLISH, "Hello");
    mlText0.addValue(Locale.FRENCH, "Bonjour");
    mlTextCollection.add(mlText0);
    MLText mlText1 = new MLText();
    mlText1.addValue(Locale.ENGLISH, "Bye bye");
    mlText1.addValue(Locale.FRENCH, "Au revoir");
    mlTextCollection.add(mlText1);
    
    nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE, (Serializable) mlTextCollection);
    Collection<MLText> mlTextCollectionCheck = (Collection<MLText>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", mlTextCollection, mlTextCollectionCheck);
}
 
Example #2
Source File: MLPropertyInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @param outboundValue Serializable
 * @return boolean
 */
private boolean isCollectionOfMLText(Serializable outboundValue)
{
    if(outboundValue instanceof Collection<?>)
    {
        for(Object o : (Collection<?>)outboundValue)
        {
            if(!(o instanceof MLText))
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        return false;
    }
}
 
Example #3
Source File: MLPropertyInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Replace any text in mlText having the same language (but any variant) as contentLocale
 * with updatedText keyed by the language of contentLocale. This ensures that the mlText
 * will have no more than one entry for the particular language.
 * 
 * @param contentLocale Locale
 * @param updatedText String
 * @param mlText MLText
 */
private void replaceTextForLanguage(Locale contentLocale, String updatedText, MLText mlText)
{
    String language = contentLocale.getLanguage();
    // Remove all text entries having the same language as the chosen contentLocale
    // (e.g. if contentLocale is en_GB, then remove text for en, en_GB, en_US etc.
    Iterator<Locale> locales = mlText.getLocales().iterator();
    while (locales.hasNext())
    {
        Locale locale = locales.next();
        if (locale.getLanguage().equals(language))
        {
            locales.remove();
        }
    }
    
    // Add the new value for the specific language
    mlText.addValue(new Locale(language), updatedText);
}
 
Example #4
Source File: NodeResourceHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Map<String, Serializable> mapToNodeProperties(Map<QName, Serializable> props)
{
    Map<String, Serializable> filteredProps = new HashMap<>(props.size());

    props.forEach((k, v) -> {
        if (!nodePropertyFilter.isExcluded(k) && v != null)
        {
            if (v instanceof MLText)
            {
                //TODO - should we send all of the values if multiple locales exist?
                v = ((MLText) v).getDefaultValue();
            }

            if (isNotEmptyString(v))
            {
                filteredProps.put(getQNamePrefixString(k), v);
            }
        }
    });

    return filteredProps;
}
 
Example #5
Source File: DBQueryTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @return properties
 */
public Map<QName, Serializable> getOrderProperties()
{
    Map<QName, Serializable> testProperties = new HashMap<QName, Serializable>();
    testProperties.put(CREATED_DATE, orderDate);
    testProperties.put(ORDER_DOUBLE, orderDoubleCount);
    testProperties.put(ORDER_FLOAT, orderFloatCount);
    testProperties.put(ORDER_LONG, orderLongCount);
    testProperties.put(ORDER_INT, orderIntCount);
    testProperties.put(ORDER_TEXT, new String(new char[] { (char) ('a' + orderTextCount) }) + " cabbage");

    MLText mlText = new MLText();
    mlText.addValue(Locale.ENGLISH, new String(new char[] { (char) ('a' + orderTextCount) }) + " banana");
    mlText.addValue(Locale.FRENCH, new String(new char[] { (char) ('Z' - orderTextCount) }) + " banane");
    mlText.addValue(Locale.CHINESE, new String(new char[] { (char) ('香' + orderTextCount) }) + " 香蕉");
    testProperties.put(ORDER_ML_TEXT, mlText);

    orderDate = Duration.subtract(orderDate, new Duration("P1D"));
    orderDoubleCount += 0.1d;
    orderFloatCount += 0.82f;
    orderLongCount += 299999999999999l;
    orderIntCount += 8576457;
    orderTextCount++;
    return testProperties;
}
 
Example #6
Source File: FileFolderLoaderTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * One file
 */
@Test
public void testLoad_OneFile() throws Exception
{
    try
    {
        AuthenticationUtil.pushAuthentication();
        AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
        int created = fileFolderLoader.createFiles(
                writeFolderPath,
                1, 256, 1024L, 1024L, Long.MAX_VALUE, false,
                10, 256L);
        assertEquals("Incorrect number of files generated.", 1, created);
        // Check the descriptions
        RetryingTransactionCallback<Void> checkCallback = new RetryingTransactionCallback<Void>()
        {
            @Override
            public Void execute() throws Throwable
            {
                MLPropertyInterceptor.setMLAware(true);
                List<ChildAssociationRef> childAssocs = nodeService.getChildAssocs(writeFolderNodeRef);
                // Count
                assertEquals(1, childAssocs.size());
                NodeRef fileNodeRef = childAssocs.get(0).getChildRef();
                MLText descriptions = (MLText) nodeService.getProperty(fileNodeRef, ContentModel.PROP_DESCRIPTION);
                assertNotNull("No descriptions added", descriptions);
                assertEquals("Incorrect number of unique descriptions added: ", 10, descriptions.size());
                assertTrue("Expect the default language to be present. ",
                        descriptions.containsKey(new Locale(Locale.getDefault().getLanguage())));
                return null;
            }
        };
        transactionService.getRetryingTransactionHelper().doInTransaction(checkCallback, true);
    }
    finally
    {
        AuthenticationUtil.popAuthentication();
    }
}
 
Example #7
Source File: DbNodeServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testMLTextValues() throws Exception
{
    // Set the server default locale
    Locale.setDefault(Locale.ENGLISH);
    
    MLText mlTextProperty = new MLText();
    mlTextProperty.addValue(Locale.ENGLISH, "Very good!");
    mlTextProperty.addValue(Locale.FRENCH, "Très bon!");
    mlTextProperty.addValue(Locale.GERMAN, "Sehr gut!");

    nodeService.setProperty(
            rootNodeRef,
            BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE,
            mlTextProperty);
    
    // Check unfiltered property retrieval
    Serializable textValueDirect = nodeService.getProperty(
            rootNodeRef,
            BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE);
    assertEquals(
            "MLText type not returned direct",
            mlTextProperty,
            textValueDirect);
    
    // Check unfiltered mass property retrieval
    Map<QName, Serializable> propertiesDirect = nodeService.getProperties(rootNodeRef);
    assertEquals(
            "MLText type not returned direct in Map",
            mlTextProperty,
            propertiesDirect.get(BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE));
}
 
Example #8
Source File: DbNodeServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Ensure that plain strings going into MLText properties is handled
 */
@SuppressWarnings("unchecked")
public void testStringIntoMLTextProperty() throws Exception
{
    String text = "Hello";
    nodeService.setProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE, text);
    Serializable mlTextCheck = nodeService.getProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE);
    assertTrue("Plain string insertion should be returned as MLText", mlTextCheck instanceof MLText);
    Locale defaultLocale = I18NUtil.getLocale();
    MLText mlTextCheck2 = (MLText) mlTextCheck;
    String mlTextDefaultCheck = mlTextCheck2.getDefaultValue();
    assertEquals("Default MLText value was not set correctly", text, mlTextDefaultCheck);
    
    // Reset the property
    nodeService.setProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE, null);
    Serializable nullValueCheck = nodeService.getProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE);
    
    // Now, just pass a String in
    nodeService.setProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE, text);
    // Now update the property with some MLText
    MLText mlText = new MLText();
    mlText.addValue(Locale.ENGLISH, "Very good!");
    mlText.addValue(Locale.FRENCH, "Très bon!");
    mlText.addValue(Locale.GERMAN, "Sehr gut!");
    nodeService.setProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE, mlText);
    // Get it back and check
    mlTextCheck = nodeService.getProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE);
    assertEquals("Setting of MLText over String failed.", mlText, mlTextCheck);
}
 
Example #9
Source File: DbNodeServiceImplTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Ensure that plain strings going into MLText properties is handled
 */
@SuppressWarnings("unchecked")
public void testSingleStringMLTextProperty() throws Exception
{
    // Set the property with single-value MLText
    MLText mlText = new MLText();
    mlText.addValue(Locale.GERMAN, "Sehr gut!");
    nodeService.setProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE, mlText);
    // Get it back and check
    MLText mlTextCheck = (MLText) nodeService.getProperty(rootNodeRef, PROP_QNAME_ML_TEXT_VALUE);
    assertEquals("Setting of MLText over String failed.", mlText, mlTextCheck);
}
 
Example #10
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * This instance modifies the ML text value to be just the default locale string.
 */
protected void getExpectedPropertyValues(Map<QName, Serializable> checkProperties)
{
    MLText mlTextValue = (MLText) checkProperties.get(PROP_QNAME_ML_TEXT_VALUE);
    String strValue = mlTextValue.getDefaultValue();
    checkProperties.put(PROP_QNAME_ML_TEXT_VALUE, strValue);
}
 
Example #11
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testLongMLTextValues() throws Exception
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 4096; i++)
    {
        sb.append(" ").append(i);
    }
    String longString = sb.toString();
    // Set the server default locale
    Locale.setDefault(Locale.ENGLISH);

    // Set it as a normal string
    nodeService.setProperty(
            rootNodeRef,
            BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE,
            longString);
    
    MLText mlTextProperty = new MLText();
    mlTextProperty.addValue(Locale.ENGLISH, longString);
    mlTextProperty.addValue(Locale.FRENCH, longString);
    mlTextProperty.addValue(Locale.GERMAN, longString);

    // Set it as MLText
    nodeService.setProperty(
            rootNodeRef,
            BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE,
            mlTextProperty);
}
 
Example #12
Source File: PropertyValueDAOTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * See MNT-20992
 *
 * @throws Exception
 */
@Test
public void testPropertyValue_EmptyMaps() throws Exception
{
    HashMap emptyMap = new HashMap();
    // Set a real cache, the problem happens in the cache
    ((AbstractPropertyValueDAOImpl)propertyValueDAO).setPropertyValueCache(new DefaultSimpleCache<Serializable, Object>());

    // Create an empty property of type MLText, this will also cache the new property
    Pair<Long, Serializable> emptyMLTextPair = txnHelper.doInTransaction(()
            -> propertyValueDAO.getOrCreatePropertyValue(new MLText()), false);

    // Create an empty HashMap property
    Pair<Long, Serializable> emptyHashMapPair = txnHelper.doInTransaction(()
            -> propertyValueDAO.getOrCreatePropertyValue(emptyMap), false);

    // Check that the returned pair representing the empty HashMap is actually a HashMap
    assertEquals("Incorrect type persisted for an empty HashMap.", HashMap.class, emptyHashMapPair.getSecond().getClass());

    // Check again by retrieving the value by value
    Pair<Long, Serializable> emptyHashMapPairRetrievedByValye = txnHelper.doInTransaction(()
            -> propertyValueDAO.getPropertyValue(emptyMap), false);
    assertEquals("Incorrect type persisted for an empty HashMap.", HashMap.class, emptyHashMapPairRetrievedByValye.getSecond().getClass());

    // Check again by retrieving the value by id
    Pair<Long, Serializable> emptyHashMapPairRetrievedByID = txnHelper.doInTransaction(()
            -> propertyValueDAO.getPropertyValueById(emptyHashMapPair.getFirst()), false);
    assertEquals("Incorrect type persisted for an empty HashMap.", HashMap.class, emptyHashMapPairRetrievedByID.getSecond().getClass());
}
 
Example #13
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testMLTextValues() throws Exception
{
    // Set the server default locale
    Locale.setDefault(Locale.ENGLISH);
    
    MLText mlTextProperty = new MLText();
    mlTextProperty.addValue(Locale.ENGLISH, "Very good!");
    mlTextProperty.addValue(Locale.FRENCH, "Très bon!");
    mlTextProperty.addValue(Locale.GERMAN, "Sehr gut!");

    nodeService.setProperty(
            rootNodeRef,
            BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE,
            mlTextProperty);
    
    // Check filtered property retrieval
    Serializable textValueFiltered = nodeService.getProperty(
            rootNodeRef,
            BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE);
    assertEquals(
            "Default locale value not taken for ML text",
            mlTextProperty.getValue(Locale.ENGLISH),
            textValueFiltered);
    
    // Check filtered mass property retrieval
    Map<QName, Serializable> propertiesFiltered = nodeService.getProperties(rootNodeRef);
    assertEquals(
            "Default locale value not taken for ML text in Map",
            mlTextProperty.getValue(Locale.ENGLISH),
            propertiesFiltered.get(BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE));
}
 
Example #14
Source File: NodePropertyHelperTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tests simple, well-typed values
 */
public void testSimpleKnownValues() throws Throwable
{
    Map<QName, Serializable> in = new HashMap<QName, Serializable>(17);
    in.put(ContentModel.PROP_AUTO_VERSION, Boolean.TRUE);
    in.put(ContentModel.PROP_HITS, new Integer(1));
    in.put(ContentModel.PROP_SIZE_CURRENT, new Long(2L));
    in.put(ContentModel.PROP_RATING_SCORE, new Float(3.0));
    in.put(ContentModel.PROP_NAME, "four");
    in.put(ContentModel.PROP_TITLE, new MLText("five"));
    in.put(ContentModel.PROP_REFERENCE, new NodeRef("protocol://identifier/six"));
    in.put(VersionModel.PROP_QNAME_VALUE, Locale.CANADA);
    
    marshallAndUnmarshall(in, true);
}
 
Example #15
Source File: NodePropertyHelperTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tests simple, well-typed values that need conversion
 */
public void testConvertableKnownValues() throws Throwable
{
    Map<QName, Serializable> in = new HashMap<QName, Serializable>(17);
    in.put(ContentModel.PROP_AUTO_VERSION, "TRUE");
    in.put(ContentModel.PROP_HITS, "1");
    in.put(ContentModel.PROP_SIZE_CURRENT, "2");
    in.put(ContentModel.PROP_RATING_SCORE, "3.0");
    in.put(ContentModel.PROP_NAME, new MLText("four"));
    in.put(ContentModel.PROP_TITLE, "five");
    in.put(ContentModel.PROP_REFERENCE, "protocol://identifier/six");
    in.put(VersionModel.PROP_QNAME_VALUE, "en_CA_");
    
    marshallAndUnmarshall(in, false);
}
 
Example #16
Source File: XmlMetadataExtracterTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String getPropertyValue(PropertyMap properties, QName qname)
{
    Object val = properties.get(qname);
    assertNotNull("Property " + qname + " missing, properties are " + properties.keySet(), val);
    
    if(val instanceof String)
        return (String)val;
    if(val instanceof MLText)
        return ((MLText)val).getDefaultValue();
    return val.toString();
}
 
Example #17
Source File: AuditComponentTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testAuditTruncatedValues()
{
    final String rootPath = "/test/one.one/two.one";

    // String value with length grater then the DB supported threshold.
    final String stringValue = RandomStringUtils.randomAlphanumeric(SchemaBootstrap.DEFAULT_MAX_STRING_LENGTH + 1);
    final MLText mlTextValue = new MLText();
    mlTextValue.put(Locale.ENGLISH, stringValue);

    final RetryingTransactionCallback<Map<String, Serializable>> testCallback = new RetryingTransactionCallback<Map<String, Serializable>>()
    {
        public Map<String, Serializable> execute() throws Throwable
        {
            final Map<String, Serializable> values = new HashMap<>();
            values.put("/3.1/4.1", stringValue);
            values.put("/3.1/4.2", mlTextValue);

            return auditComponent.recordAuditValues(rootPath, values);
        }
    };
    RunAsWork<Map<String, Serializable>> testRunAs = new RunAsWork< Map<String, Serializable>>()
    {
        public Map<String, Serializable> doWork() throws Exception
        {
            return transactionService.getRetryingTransactionHelper().doInTransaction(testCallback);
        }
    };

    Map<String, Serializable> result = AuthenticationUtil.runAs(testRunAs, "SomeOtherUser");

    // Check that the values aren't truncated.
    assertEquals(stringValue, result.get("/test/1.1/2.1/3.1/4.1/value.1"));
    assertEquals(mlTextValue, result.get("/test/1.1/2.1/3.1/4.2/value.2"));
}
 
Example #18
Source File: NodePropertyHelperTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Tests simple, residual values
 */
public void testSimpleResidualValues() throws Throwable
{
    Map<QName, Serializable> in = new HashMap<QName, Serializable>(17);
    in.put(QN_BOOLEAN, Boolean.TRUE);
    in.put(QN_INTEGER, new Integer(1));
    in.put(QN_LONG, new Long(2L));
    in.put(QN_FLOAT, new Float(3.0));
    in.put(QN_TEXT, "four");
    in.put(QN_MLTEXT, new MLText("five"));
    in.put(QN_REF, new NodeRef("protocol://identifier/six"));
    in.put(QN_ANY, Locale.CANADA);
    
    marshallAndUnmarshall(in, true);
}
 
Example #19
Source File: NodePropertyValue.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected ValueType getPersistedType(Serializable value)
{
    if (value instanceof MLText)
    {
        throw new IllegalArgumentException("MLText must be split up before persistence.");
    }
    return ValueType.STRING;
}
 
Example #20
Source File: MLPropertyInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Locale getClosestLocale(Collection<?> collection)
{
    if (collection.size() == 0)
    {
        return null;
    }
    // Use the available keys as options
    HashSet<Locale> locales = new HashSet<Locale>();
    for(Object o : collection)
    {
        MLText mlText = (MLText)o;
        locales.addAll(mlText.keySet());
    }
    // Try the content locale
    Locale locale = I18NUtil.getContentLocale();
    Locale match = I18NUtil.getNearestLocale(locale, locales);
    if (match == null)
    {
        // Try just the content locale language
        locale = I18NUtil.getContentLocaleLang();
        match = I18NUtil.getNearestLocale(locale, locales);
        if (match == null)
        {
            // No close matches for the locale - go for the default locale
            locale = I18NUtil.getLocale();
            match = I18NUtil.getNearestLocale(locale, locales);
            if (match == null)
            {
                // just get any locale
                match = I18NUtil.getNearestLocale(null, locales);
            }
        }
    }
    return match;
}
 
Example #21
Source File: MLPropertyInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Serializable getClosestValue(MLText mlText)
{
    Set<Locale> locales = mlText.getLocales();
    Locale contentLocale = I18NUtil.getContentLocale();
    Locale locale = I18NUtil.getNearestLocale(contentLocale, locales);
    if (locale != null)
    {
        return mlText.getValue(locale);
    }

    // If the content locale is too specific, try relaxing it to just language
    Locale contentLocaleLang = I18NUtil.getContentLocaleLang();
    // We do not expect contentLocaleLang to be null
    if (contentLocaleLang != null)
    {
        locale = I18NUtil.getNearestLocale(contentLocaleLang, locales);
        if (locale != null)
        {
            return mlText.getValue(locale);
        }
    }
    else
    {
        logger.warn("contentLocaleLang is null in getClosestValue. This is not expected.");
    }
    
    // Just return the default translation
    return mlText.getDefaultValue();
}
 
Example #22
Source File: EventGenerationBehaviours.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void checkSiteTitlePropertyRenamed(NodeRef nodeRef, Map<QName, Serializable> before,
        Map<QName, Serializable> after)
{
    QName nodeRefType = nodeService.getType(nodeRef);

    if (dictionaryService.isSubClass(nodeRefType, TYPE_SITE)
            && propertyChanged(before, after, ContentModel.PROP_TITLE))
    {
        String oldName = ((MLText) before.get(ContentModel.PROP_TITLE)).getDefaultValue();
        String newName = ((MLText) after.get(ContentModel.PROP_TITLE)).getDefaultValue();

        eventsService.nodeRenamed(nodeRef, oldName, newName);
    }
}
 
Example #23
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * ALF-3756 - original fix didn't cope with existing MLText properties having one or more variants
 * of a particular language. Upgrading to the fix would therefore not solve the problem properly.
 * <p>
 * For example, if a property has en_GB text in it, then 'updating' that property
 * with a locale of en_US will result in the addition of the en_US text rather than a true update (they're both
 * English, and using two slightly differently configured browsers in this way leads to confusion).
 */
@Test
public void testMLTextUpdatedForCorrectLanguage() throws Exception
{
    Locale.setDefault(Locale.UK);
    MLPropertyInterceptor.setMLAware(true);
    MLText mlTextProperty = new MLText();
    mlTextProperty.addValue(Locale.UK, "en_GB String");
    mlTextProperty.addValue(Locale.FRANCE, "fr_FR String");
    
    // Store the MLText property
    nodeService.setProperty(
                rootNodeRef,
                BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE,
                mlTextProperty);
    
    // Pre-test check that an MLText property has been created with the correct locale/text pairs.
    Serializable textValue = nodeService.getProperty(
                rootNodeRef,
                BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE);
    assertEquals(2, ((MLText) textValue).size());
    assertEquals("en_GB String", ((MLText) textValue).getValue(Locale.UK));
    assertEquals("fr_FR String", ((MLText) textValue).getValue(Locale.FRANCE));
    
    // Enable MLText filtering - as this is how the repo will be used.
    MLPropertyInterceptor.setMLAware(false);
    
    // Retrieve the MLText - but it is filtered into an appropriate String
    textValue = nodeService.getProperty(
                rootNodeRef,
                BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE);
    assertEquals("en_GB String", (String) textValue);
    
    // Update the property, only this time using a different English variant
    Locale.setDefault(Locale.US); // en_US
    nodeService.setProperty(
                rootNodeRef,
                BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE,
                "Not using MLText for this part.");
    
    // Check that the text was updated rather than added to
    MLPropertyInterceptor.setMLAware(true); // no filtering - see real MLText
    // Check that there are not too many English strings, we don't want one for en_GB and one for en_US
    textValue = nodeService.getProperty(
                rootNodeRef,
                BaseNodeServiceTest.PROP_QNAME_ML_TEXT_VALUE);
    assertEquals(2, ((MLText) textValue).size());
    assertEquals("Text wasn't updated correctly",
                "Not using MLText for this part.",
                ((MLText) textValue).getValue(Locale.ENGLISH));
    assertEquals("Failed to get text using locale it was added with",
                "Not using MLText for this part.",
                ((MLText) textValue).getClosestValue(Locale.US));
    assertEquals("Failed to get text using original locale",
                "Not using MLText for this part.",
                ((MLText) textValue).getClosestValue(Locale.UK));
    assertEquals("fr_FR String", ((MLText) textValue).getValue(Locale.FRANCE));
}
 
Example #24
Source File: DefaultTypeConverterTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testToString()
{
    assertEquals("true", DefaultTypeConverter.INSTANCE.convert(String.class, new Boolean(true)));
    assertEquals("false", DefaultTypeConverter.INSTANCE.convert(String.class, new Boolean(false)));
    assertEquals("v", DefaultTypeConverter.INSTANCE.convert(String.class, Character.valueOf('v')));
    assertEquals("3", DefaultTypeConverter.INSTANCE.convert(String.class, Byte.valueOf("3")));
    assertEquals("4", DefaultTypeConverter.INSTANCE.convert(String.class, Short.valueOf("4")));
    assertEquals("5", DefaultTypeConverter.INSTANCE.convert(String.class, Integer.valueOf("5")));
    assertEquals("6", DefaultTypeConverter.INSTANCE.convert(String.class, Long.valueOf("6")));
    assertEquals("7.1", DefaultTypeConverter.INSTANCE.convert(String.class, Float.valueOf("7.1")));
    assertEquals("NaN", DefaultTypeConverter.INSTANCE.convert(String.class, Float.NaN));
    assertEquals("-Infinity", DefaultTypeConverter.INSTANCE.convert(String.class, Float.NEGATIVE_INFINITY));
    assertEquals("Infinity", DefaultTypeConverter.INSTANCE.convert(String.class, Float.POSITIVE_INFINITY));
    assertEquals("123.123", DefaultTypeConverter.INSTANCE.convert(String.class, Double.valueOf("123.123")));
    assertEquals("NaN", DefaultTypeConverter.INSTANCE.convert(String.class, Double.NaN));
    assertEquals("-Infinity", DefaultTypeConverter.INSTANCE.convert(String.class, Double.NEGATIVE_INFINITY));
    assertEquals("Infinity", DefaultTypeConverter.INSTANCE.convert(String.class, Double.POSITIVE_INFINITY));
    assertEquals("1234567890123456789", DefaultTypeConverter.INSTANCE.convert(String.class, new BigInteger("1234567890123456789")));
    assertEquals("12345678901234567890.12345678901234567890", DefaultTypeConverter.INSTANCE.convert(String.class, new BigDecimal("12345678901234567890.12345678901234567890")));
    Date date = new Date();
    assertEquals(ISO8601DateFormat.format(date), DefaultTypeConverter.INSTANCE.convert(String.class, date));
    assertEquals("P0Y25D", DefaultTypeConverter.INSTANCE.convert(String.class, new Duration("P0Y25D")));
    assertEquals("woof", DefaultTypeConverter.INSTANCE.convert(String.class, "woof"));
    // MLText
    MLText mlText = new MLText("woof");
    mlText.addValue(Locale.SIMPLIFIED_CHINESE, "缂");
    assertEquals("woof", DefaultTypeConverter.INSTANCE.convert(String.class, mlText));
    // Locale
    assertEquals("fr_FR_", DefaultTypeConverter.INSTANCE.convert(String.class, Locale.FRANCE));
    // VersionNumber
    assertEquals("1.2.3", DefaultTypeConverter.INSTANCE.convert(String.class, new VersionNumber("1.2.3")));
    // Period
    assertEquals("period", DefaultTypeConverter.INSTANCE.convert(String.class, new Period("period")));
    assertEquals("period|12", DefaultTypeConverter.INSTANCE.convert(String.class, new Period("period|12")));
    Map<String,String> periodMap = new HashMap<>();
    periodMap.put("periodType","month");
    periodMap.put("expression","1");
    assertEquals(new Period("month|1"), DefaultTypeConverter.INSTANCE.convert(Period.class, new Period(periodMap)));
    // Java Class
    assertEquals(this.getClass(), DefaultTypeConverter.INSTANCE.convert(Class.class, this.getClass().getName()));
}
 
Example #25
Source File: DefaultTypeConverterTest.java    From alfresco-data-model with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testFromString()
{
    assertEquals(Boolean.valueOf(true), DefaultTypeConverter.INSTANCE.convert(Boolean.class, "True"));
    assertEquals(Boolean.valueOf(false), DefaultTypeConverter.INSTANCE.convert(Boolean.class, "woof"));
    assertEquals(Character.valueOf('w'), DefaultTypeConverter.INSTANCE.convert(Character.class, "w"));
    assertEquals(Byte.valueOf("3"), DefaultTypeConverter.INSTANCE.convert(Byte.class, "3"));
    assertEquals(Short.valueOf("4"), DefaultTypeConverter.INSTANCE.convert(Short.class, "4"));
    assertEquals(Integer.valueOf("5"), DefaultTypeConverter.INSTANCE.convert(Integer.class, "5"));
    assertEquals(Long.valueOf("6"), DefaultTypeConverter.INSTANCE.convert(Long.class, "6"));
    assertEquals(Float.valueOf("7.1"), DefaultTypeConverter.INSTANCE.convert(Float.class, "7.1"));
    assertEquals(Float.NaN, DefaultTypeConverter.INSTANCE.convert(Float.class, "NaN"));
    assertEquals(Float.NEGATIVE_INFINITY, DefaultTypeConverter.INSTANCE.convert(Float.class, "-Infinity"));
    assertEquals(Float.POSITIVE_INFINITY, DefaultTypeConverter.INSTANCE.convert(Float.class, "Infinity"));
    assertEquals(Double.valueOf("123.123"), DefaultTypeConverter.INSTANCE.convert(Double.class, "123.123"));
    assertEquals(Double.NaN, DefaultTypeConverter.INSTANCE.convert(Double.class, "NaN"));
    assertEquals(Double.NEGATIVE_INFINITY, DefaultTypeConverter.INSTANCE.convert(Double.class, "-Infinity"));
    assertEquals(Double.POSITIVE_INFINITY, DefaultTypeConverter.INSTANCE.convert(Double.class, "Infinity"));
    assertEquals(new BigInteger("1234567890123456789"), DefaultTypeConverter.INSTANCE.convert(BigInteger.class, "1234567890123456789"));
    assertEquals(new BigDecimal("12345678901234567890.12345678901234567890"), DefaultTypeConverter.INSTANCE.convert(BigDecimal.class, "12345678901234567890.12345678901234567890"));
    GregorianCalendar cal = new GregorianCalendar();
    cal.set(Calendar.YEAR, 2004);
    cal.set(Calendar.MONTH, 3);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    String isoDate = ISO8601DateFormat.format(cal.getTime());
    assertEquals(isoDate, ISO8601DateFormat.format(DefaultTypeConverter.INSTANCE.convert(Date.class, isoDate)));
    assertEquals(new Duration("P25D"), DefaultTypeConverter.INSTANCE.convert(Duration.class, "P25D"));
    assertEquals("woof", DefaultTypeConverter.INSTANCE.convert(String.class, "woof"));
    
    MLText converted = DefaultTypeConverter.INSTANCE.convert(MLText.class, "woof");
    assertEquals("woof", converted.getValue(Locale.getDefault()));
    
    assertEquals(Locale.FRANCE, DefaultTypeConverter.INSTANCE.convert(Locale.class, "fr_FR"));
    assertEquals(Locale.FRANCE, DefaultTypeConverter.INSTANCE.convert(Locale.class, "fr_FR_"));
    
    assertEquals(new VersionNumber("1.2.3"), DefaultTypeConverter.INSTANCE.convert(VersionNumber.class, "1.2.3"));
    assertEquals(new Period("period"), DefaultTypeConverter.INSTANCE.convert(Period.class, "period"));
    assertEquals(new Period("period|12"), DefaultTypeConverter.INSTANCE.convert(Period.class, "period|12"));
    Map<String,String> periodMap = new HashMap<String, String>();
    periodMap.put("periodType","month");
    periodMap.put("expression","1");
    assertEquals(new Period(periodMap), DefaultTypeConverter.INSTANCE.convert(Period.class, periodMap));
    // Java Class
    assertEquals(this.getClass().getName(), DefaultTypeConverter.INSTANCE.convert(String.class, this.getClass()));
}
 
Example #26
Source File: SOLRSerializer.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public PropertyValue serialize(QName propName, Serializable value) throws IOException, JSONException
{
    if(value == null)
    {
        return new PropertyValue(false, "null");
    }

    PropertyDefinition propertyDef = dictionaryService.getProperty(propName);
    if (propertyDef == null)
    {
        // Treat it as text
        return new PropertyValue(true, serializeToJSONString(value));
    }
    DataTypeDefinition dataType = propertyDef.getDataType();
    QName dataTypeName = dataType.getName();
    if (propertyDef.isMultiValued())
    {
        if(!(value instanceof Collection))
        {
            throw new IllegalArgumentException("Multi value: expected a collection, got " + value.getClass().getName());
        }

        Collection<Serializable> c = (Collection<Serializable>)value;

        JSONArray body = new JSONArray();
        for(Serializable o : c)
        {
            if(dataTypeName.equals(DataTypeDefinition.MLTEXT))
            {
                MLText source = (MLText)o;
                JSONArray array = new JSONArray();
                for(Locale locale : source.getLocales())
                {
                    JSONObject json = new JSONObject();
                    json.put("locale", DefaultTypeConverter.INSTANCE.convert(String.class, locale));
                    json.put("value", source.getValue(locale));
                    array.put(json);
                }
                body.put(array);
            }
            else if(dataTypeName.equals(DataTypeDefinition.CONTENT))
            {
                throw new RuntimeException("Multi-valued content properties are not supported");
            }
            else
            {
                body.put(serializeToJSONString(o));
            }
            
        }
        
        return new PropertyValue(false, body.toString());
    }
    else
    {
        boolean encodeString = true;
        if(dataTypeName.equals(DataTypeDefinition.MLTEXT))
        {
            encodeString = false;
        }
        else if(dataTypeName.equals(DataTypeDefinition.CONTENT))
        {
            encodeString = false;
        }
        else
        {
            encodeString = true;
        }

        String sValue = null;
        if (value instanceof String && encodeString) {
        	sValue = (String)jsonUtils.encodeJSONString(value);
        } else {
        	sValue = serializeToJSONString(value);
        }

        return new PropertyValue(encodeString, sValue);
    }
}
 
Example #27
Source File: BaseNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Commit
@Test
public void testPropertyLocaleBehaviour() throws Exception
{
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>(17);
    properties.put(PROP_QNAME_BOOLEAN_VALUE, true);
    properties.put(PROP_QNAME_INTEGER_VALUE, 123);
    properties.put(PROP_QNAME_LONG_VALUE, 123L);
    properties.put(PROP_QNAME_FLOAT_VALUE, 123.0F);
    properties.put(PROP_QNAME_DOUBLE_VALUE, 123.0);
    properties.put(PROP_QNAME_STRING_VALUE, "123.0");
    properties.put(PROP_QNAME_ML_TEXT_VALUE, new MLText("This is ML text in the default language"));
    properties.put(PROP_QNAME_DATE_VALUE, new Date());
    // Get the check values
    Map<QName, Serializable> expectedProperties = new HashMap<QName, Serializable>(properties);
    getExpectedPropertyValues(expectedProperties);

    Locale.setDefault(Locale.JAPANESE);
    
    // create a new node
    NodeRef nodeRef = nodeService.createNode(
            rootNodeRef,
            ASSOC_TYPE_QNAME_TEST_CHILDREN,
            QName.createQName("pathA"),
            TYPE_QNAME_TEST_MANY_PROPERTIES,
            properties).getChildRef();
    
    // Check the properties again
    Map<QName, Serializable> checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
    
    // Change the locale and set the properties again
    I18NUtil.setLocale(Locale.US);
    nodeService.setProperties(nodeRef, properties);

    // Check the properties again
    checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
    
    // Change the locale and set the properties again
    I18NUtil.setLocale(Locale.UK);
    nodeService.setProperties(nodeRef, properties);

    // Check the properties again
    checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
    
    // Change the locale and set the properties again
    I18NUtil.setLocale(Locale.US);
    nodeService.addProperties(nodeRef, properties);

    // Check the properties again
    checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
    
    // Change the locale and set the properties again
    I18NUtil.setLocale(Locale.UK);
    nodeService.addProperties(nodeRef, properties);

    // Check the properties again
    checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
    
    // Change the locale and set the properties again
    I18NUtil.setLocale(Locale.US);
    nodeService.setProperty(nodeRef, PROP_QNAME_DATE_VALUE, properties.get(PROP_QNAME_DATE_VALUE));

    // Check the properties again
    checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
    
    // Change the locale and set the properties again
    I18NUtil.setLocale(Locale.UK);
    nodeService.setProperty(nodeRef, PROP_QNAME_DATE_VALUE, properties.get(PROP_QNAME_DATE_VALUE));

    // Check the properties again
    checkProperties = nodeService.getProperties(nodeRef);
    checkProperties(checkProperties, expectedProperties);
}
 
Example #28
Source File: BaseNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Check that properties go in and come out in the correct format.
 */
@Test
@SuppressWarnings("unchecked")
public void testPropertyTypes() throws Exception
{
    ArrayList<String> listProperty = new ArrayList<String>(2);
    listProperty.add("ABC");
    listProperty.add("DEF");
    
    Path pathProperty = new Path();
    pathProperty.append(new Path.SelfElement()).append(new Path.AttributeElement(TYPE_QNAME_TEST_CONTENT));
    
    Map<QName, Serializable> properties = new HashMap<QName, Serializable>(17);
    properties.put(PROP_QNAME_BOOLEAN_VALUE, true);
    properties.put(PROP_QNAME_INTEGER_VALUE, 123);
    properties.put(PROP_QNAME_LONG_VALUE, 123L);
    properties.put(PROP_QNAME_FLOAT_VALUE, 123.0F);
    properties.put(PROP_QNAME_DOUBLE_VALUE, 123.0);
    properties.put(PROP_QNAME_STRING_VALUE, "123.0");
    properties.put(PROP_QNAME_ML_TEXT_VALUE, new MLText("This is ML text in the default language"));
    properties.put(PROP_QNAME_DATE_VALUE, new Date());
    properties.put(PROP_QNAME_SERIALIZABLE_VALUE, "456");
    properties.put(PROP_QNAME_NODEREF_VALUE, rootNodeRef);
    properties.put(PROP_QNAME_PATH_VALUE, pathProperty);
    properties.put(PROP_QNAME_CATEGORY_VALUE, cat);
    properties.put(PROP_QNAME_LOCALE_VALUE, Locale.CHINESE);
    properties.put(PROP_QNAME_NULL_VALUE, null);
    properties.put(PROP_QNAME_MULTI_VALUE, listProperty);
    properties.put(PROP_QNAME_PERIOD_VALUE, "period|1");
    // Get the check values
    Map<QName, Serializable> expectedProperties = new HashMap<QName, Serializable>(properties);
    getExpectedPropertyValues(expectedProperties);
    
    // create a new node
    NodeRef nodeRef = nodeService.createNode(
            rootNodeRef,
            ASSOC_TYPE_QNAME_TEST_CHILDREN,
            QName.createQName("pathA"),
            TYPE_QNAME_TEST_MANY_PROPERTIES,
            properties).getChildRef();
    
    // get the properties back
    Map<QName, Serializable> checkProperties = nodeService.getProperties(nodeRef);
    // Check
    checkProperties(checkProperties, expectedProperties);
    
    // check multi-valued properties are created where necessary
    nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_VALUE, "GHI");
    Serializable checkProperty = nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_VALUE);
    assertTrue("Property not converted to a Collection", checkProperty instanceof Collection);
    assertTrue("Collection doesn't contain value", ((Collection<?>)checkProperty).contains("GHI"));
    
    // Check special numbers (Double): ALF-16906
    nodeService.setProperty(nodeRef, PROP_QNAME_DOUBLE_VALUE, Double.NaN);
    assertEquals("Double.NaN failed", Double.NaN, nodeService.getProperty(nodeRef, PROP_QNAME_DOUBLE_VALUE));
    nodeService.setProperty(nodeRef, PROP_QNAME_DOUBLE_VALUE, Double.NEGATIVE_INFINITY);
    assertEquals("Double.NEGATIVE_INFINITY failed", Double.NEGATIVE_INFINITY, nodeService.getProperty(nodeRef, PROP_QNAME_DOUBLE_VALUE));
    nodeService.setProperty(nodeRef, PROP_QNAME_DOUBLE_VALUE, Double.POSITIVE_INFINITY);
    assertEquals("Double.POSITIVE_INFINITY failed", Double.POSITIVE_INFINITY, nodeService.getProperty(nodeRef, PROP_QNAME_DOUBLE_VALUE));
    // Check special numbers (Float): ALF-16906
    nodeService.setProperty(nodeRef, PROP_QNAME_FLOAT_VALUE, Float.NaN);
    assertEquals("Float.NaN failed", Float.NaN, nodeService.getProperty(nodeRef, PROP_QNAME_FLOAT_VALUE));
    nodeService.setProperty(nodeRef, PROP_QNAME_FLOAT_VALUE, Float.NEGATIVE_INFINITY);
    assertEquals("Float.NEGATIVE_INFINITY failed", Float.NEGATIVE_INFINITY, nodeService.getProperty(nodeRef, PROP_QNAME_FLOAT_VALUE));
    nodeService.setProperty(nodeRef, PROP_QNAME_FLOAT_VALUE, Float.POSITIVE_INFINITY);
    assertEquals("Float.POSITIVE_INFINITY failed", Float.POSITIVE_INFINITY, nodeService.getProperty(nodeRef, PROP_QNAME_FLOAT_VALUE));
}
 
Example #29
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMultiValueMLTextProperties() throws Exception
{
    NodeRef nodeRef = nodeService.createNode(
            rootNodeRef,
            ASSOC_TYPE_QNAME_TEST_CHILDREN,
            QName.createQName("pathA"),
            TYPE_QNAME_TEST_MANY_ML_PROPERTIES).getChildRef();
    
    // Create MLText properties and add to a collection
    List<MLText> mlTextCollection = new ArrayList<MLText>(2);
    MLText mlText0 = new MLText();
    mlText0.addValue(Locale.ENGLISH, "Hello");
    mlText0.addValue(Locale.FRENCH, "Bonjour");
    mlTextCollection.add(mlText0);
    MLText mlText1 = new MLText();
    mlText1.addValue(Locale.ENGLISH, "Bye bye");
    mlText1.addValue(Locale.FRENCH, "Au revoir");
    mlTextCollection.add(mlText1);
    
    nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE, (Serializable) mlTextCollection);
    
    I18NUtil.setContentLocale(Locale.ENGLISH);
    Collection<String> mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"Hello", "Bye bye"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.FRENCH);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"Bonjour", "Au revoir"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.GERMAN);
    nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE, (Serializable)Arrays.asList(new String[]{"eins", "zwei", "drie", "vier"}));
    
    I18NUtil.setContentLocale(Locale.ENGLISH);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"Hello", "Bye bye"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.FRENCH);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"Bonjour", "Au revoir"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.GERMAN);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"eins", "zwei", "drie", "vier"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.GERMAN);
    nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE, (Serializable)Arrays.asList(new String[]{"eins"}));
    
    I18NUtil.setContentLocale(Locale.ENGLISH);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"Hello", "Bye bye"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.FRENCH);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"Bonjour", "Au revoir"}), mlTextCollectionCheck);
    
    I18NUtil.setContentLocale(Locale.GERMAN);
    mlTextCollectionCheck = (Collection<String>) nodeService.getProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE);
    assertEquals("MLText collection didn't come back correctly.", Arrays.asList(new String[]{"eins"}), mlTextCollectionCheck);
    
}
 
Example #30
Source File: FullNodeServiceTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMultiProp() throws Exception
{
    QName undeclaredPropQName = QName.createQName(NAMESPACE, getClass().getName());
    // create node
    NodeRef nodeRef = nodeService.createNode(
            rootNodeRef,
            ASSOC_TYPE_QNAME_TEST_CHILDREN,
            QName.createQName("pathA"),
            TYPE_QNAME_TEST_MULTIPLE_TESTER).getChildRef();
    ArrayList<Serializable> values = new ArrayList<Serializable>(1);
    values.add("ABC");
    values.add("DEF");
    // test allowable conditions
    nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_SINGLE, "ABC");
    // nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_SINGLE, values); -- should fail
    nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_MULTIPLE, "ABC");
    nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_MULTIPLE, values);
    nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_SINGLE, "ABC");
    nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_SINGLE, values);
    nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, "ABC");
    nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, values);
    nodeService.setProperty(nodeRef, undeclaredPropQName, "ABC");
    nodeService.setProperty(nodeRef, undeclaredPropQName, values);

    // commit as we will be breaking the transaction in the next test
    TestTransaction.flagForCommit();
    TestTransaction.end();
    TestTransaction.start();
    
    try
    {
        // this should fail as we are passing multiple values into a non-any that is multiple=false
        nodeService.setProperty(nodeRef, PROP_QNAME_STRING_PROP_SINGLE, values);
    }
    catch (DictionaryException e)
    {
        // expected
    }
    finally
    {
        TestTransaction.flagForRollback();
        TestTransaction.end();
    }

    TestTransaction.start();
    try
    {
        // Check that multi-valued d:mltext can be collections of MLText
        values.clear();
        values.add(new MLText("ABC"));
        values.add(new MLText("DEF"));
        nodeService.setProperty(nodeRef, PROP_QNAME_MULTI_ML_VALUE, values);
        List<Serializable> checkValues = (List<Serializable>) nodeService.getProperty(
                nodeRef, PROP_QNAME_MULTI_ML_VALUE);
        assertEquals("Expected 2 MLText values back", 2, checkValues.size());
        assertTrue("Incorrect type in collection", checkValues.get(0) instanceof String);
        assertTrue("Incorrect type in collection", checkValues.get(1) instanceof String);
        
        // Check that multi-valued d:any properties can be collections of collections (empty)
        // We put ArrayLists and HashSets into the Collection of d:any, so that is exactly what should come out
        values.clear();
        ArrayList<Serializable> arrayListVal = new ArrayList<Serializable>(2);
        HashSet<Serializable> hashSetVal = new HashSet<Serializable>(2);
        values.add(arrayListVal);
        values.add(hashSetVal);
        nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, values);
        checkValues = (List<Serializable>) nodeService.getProperty(
                nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE);
        assertEquals("Expected 2 Collection values back", 2, checkValues.size());
        assertTrue("Incorrect type in collection", checkValues.get(0) instanceof ArrayList);  // ArrayList in - ArrayList out
        assertTrue("Incorrect type in collection", checkValues.get(1) instanceof HashSet);  // HashSet in - HashSet out
        
        // Check that multi-valued d:any properties can be collections of collections (with values)
        // We put ArrayLists and HashSets into the Collection of d:any, so that is exactly what should come out
        arrayListVal.add("ONE");
        arrayListVal.add("TWO");
        hashSetVal.add("ONE");
        hashSetVal.add("TWO");
        values.clear();
        values.add(arrayListVal);
        values.add(hashSetVal);
        nodeService.setProperty(nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE, values);
        checkValues = (List<Serializable>) nodeService.getProperty(
                nodeRef, PROP_QNAME_ANY_PROP_MULTIPLE);
        assertEquals("Expected 2 Collection values back", 2, checkValues.size());
        assertTrue("Incorrect type in collection", checkValues.get(0) instanceof ArrayList);  // ArrayList in - ArrayList out
        assertTrue("Incorrect type in collection", checkValues.get(1) instanceof HashSet);  // HashSet in - HashSet out
        assertEquals("First collection incorrect", 2, ((Collection)checkValues.get(0)).size());
        assertEquals("Second collection incorrect", 2, ((Collection)checkValues.get(1)).size());
    }
    finally
    {
        TestTransaction.flagForRollback();
        TestTransaction.end();
    }
}